Sei sulla pagina 1di 4

10/5/2008

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];

// elements referred using subscripts


for(int i=0; i<10 ; i++)
array[i]=1;

for(i=0; i<10 ; i++)


printf(“array[%d]=%d\n“, i, array[i] );
}

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];

// elements referred using subscripts


for(int i=0; i<10 ; i++)
array[i]=i;

for(i=0; i<10 ; i++)


printf(“array[%d]=%d\n“, i, array[i] );
}
2

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];

for(int i=0; i<22 ; i++)


TwentyTwo[i]=22;

for(i=0; i<22 ; i++)


{
printf(“%d “, TwentyTwo[i] );
if ((i+1)%5 == 0)
printf(“\n”);
}
} 3

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;

// elements referred using subscripts


for(i=0; i<12 ; i++)
for(j=0; j<10; j++)
zero[i][j] = 0;

for(i=0; i<12 ; i++)


{
for(j=0; j<10; j++)
printf(“%d “, zero[i][j]);
printf(“\n”);
}
} 4

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);

printf("The characters of string are:\n " );


for(i=0; name[i] ; i++) // the '\0' finishes the string;
printf(“%s “, name[i]);
printf(“\n”);

printf("All characters of array are :\n“);


for(i=0; i<20 ; i++)
printf("name[%d] = %c, represented in ASCII cod as: %d\n“, i, name[i],name[i]);
for(i=0 ; name[i]; i++)
copy_name[i]=name[i];
copy_name[i]='\0'; // must insert the terminator of string
printf(“\n\n”);
printf(“The copy of string name is: %s “, copy_name);
} 8

Potrebbero piacerti anche