Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Exercise 1
Write the statement that would declare a 10-element integer array and
initialize all its elements to 1 and print the individual values.
#include <stdio.h>
void main( )
{
int array[10];
Exercise 2
Write a program that initializes an array of 10 elements. Each element
should be equal to its subscript. The program should then print each of
the 10 elements.
#include <stdio.h>
void main( )
{
int array[10];
1
10/5/2008
Exercise 3
• Given the following array, write code to initialize all the array elements to 22:
int TwentyTwo [22];
Print the individual value, every 5 values on a line.
#include <stdio.h>
int main( )
{
int TwentyTwo[22];
Exercise 4
Given the following array, write code to initialize all the array elements to 0:
int zero[12][10];
Print the individual values, every row on a line.
#include <stdio.h>
int main( )
{
int zero[12][10];
int i, j;
2
10/5/2008
Exercise 5
• What is wrong with the following code fragment?
int x, y;
int array[3][10];
main()
{
for ( x = 0; x < 10; x++ )
for ( y = 0; y < 3; y++ )
array[x][y] = 0;
return 0;
}
Exercise 6
• What is wrong with the following?
int array[10];
int x = 1;
main()
{
for ( x = 1; x <= 10; x++ )
array[x] = 99;
return 0;
}
3
10/5/2008
Exercise 7
What is wrong with the following code fragment?
#include <stdio.h>
void main()
{
int a, t1[10],b,t2[10];
printf("%u\n",(unsigned int)&a); // se afiseaza adresa variabilei a ca intreg zecimal fara semn
printf("%u\n",(unsigned int)t1); // se afiseaza adresa tabloului t1 ca intreg zecimal fara semn
printf("%u\n",(unsigned int)&b); // se afiseaza adresa variabilei a ca intreg zecimal fara semn
printf("%u\n",(unsigned int)t2); // se afiseaza adresa tabloului t2 ca intreg zecimal fara semn
/*
obs. Alocarea se face pe stiva in ordine descrescatoare a adreselor, in ordinea declaratiilor,
deci variabila a este alocata la adresa cea mai mare, urmeaza apoi tabloul t1, etc.
Diferenta dintre adresa variabilei a si a tabloului t1 este (sizeof(int)*10)
Diferenta dintre adresa tabloului t1 si a variabilei b este sizeof(int)
Diferenta dintre adresa variabilei b si a tabloului t2 este (sizeof(int)*10)
*/
t1[10]=11; // atribuirea este incorecta, deoarece indexul utilizat este prea mare -
// valorile corecte sunt 0, 1,..,9.
// valoarea 11 se depune in zona de memorie care a fost alocata pentru alta
// variabila decat tabloul t1,
// si anume in variabila a.
t2[10]=99; // situatie similara celei precedente, ca urmare valoarea 99 este preluata de
// variabila b.
printf("a=%d\n",a);
printf("b=%d\n",b);
} 7
Exercise 8
• Write a program that declares two 20-element char arrays named name and
copy_name. Read from keyboard a string. Print every character of string
using both direct access and indirect access. Copy the string name in
copy_name end than print they on the screen.
#include <stdio.h>
int main( )
{ char name[20], copy_name[20];
int i;
printf("Input a string: “);
scanf(“%s”, name);
printf("The readed string is: %s\n”, name);