eeco-i-c6-exercises

Upload: rotaru-ana-maria

Post on 09-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 EECO-I-C6-exercises

    1/4

    10/5/2008

    1

    1

    Exercise 1

    Write the statement that would declare a 10-element integer array andinitialize all its elements to 1 and print the individual values.

    #include

    void main( ){

    int array[10];

    // elements referred using subscriptsfor(int i=0; i

  • 8/8/2019 EECO-I-C6-exercises

    2/4

    10/5/2008

    2

    3

    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

    int main( ){

    int TwentyTwo[22];

    for(int i=0; i

  • 8/8/2019 EECO-I-C6-exercises

    3/4

    10/5/2008

    3

    5

    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;

    }

    6

    Exercise 6

    What is wrong with the following?

    int array[10];int x = 1;

    main(){

    for ( x = 1; x

  • 8/8/2019 EECO-I-C6-exercises

    4/4

    10/5/2008

    4

    7

    Exercise 7What is wrong with the following code fragment?

    #include

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

    }

    8

    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 stringusing both direct access and indirect access. Copy the string name incopy_name end than print they on the screen.

    #include 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