| Main |<Configure And Use Windows PSDK 2 |C & C++ Array Data Type 2 >|Site Index |Download |


 

 

 

 

 

C LAB WORKSHEET 9

C & C++ Array Data Type Part 1

 

 

 

 

 

  1. The C & C++ array data type.

  2. Array declaration and initialization.

  3. Tutorial references that should be used together with this worksheet arearray part 1 andarray part 2.

 

 

 

 

 

 

 

#include<stdio.h>

 

void main()

{

     float score1, score2, score3, score4;

      printf("Enter four floats: \n");

      scanf_s("%f", &score1);

      scanf_s("%f”", &score2);

      scanf_s("%f", &score3);

      scanf_s("%f", &score4);

      printf("The scores in reverse order are: \n");

      printf("%.2f\n", score4);

      printf("%.2f\n", score3);

      printf("%.2f\n", score2);

      printf("%.2f\n", score1);

}

  • In this program, game scores for four individuals are read in and they are printed in reverse order. Imagine how much more we have to repeat the monotonous steps if scores are in thousands. Just reading and writing like that would take thousand lines let alone doing other processing, such as finding the highest score.

  • Instead, consider the solution using arrays shown below. Notice that although the looping structures are slightly more complicated to code, there are fewer lines of coding. Also, if we wanted to extend the program to handle 1000 scores, then only the #define statement would need to be changed, only one statement. The saving in the number of programming lines would also be evident every time we need to process the scores any further.

#include<stdio.h>

#define SIZE 4  // the array’s size

 

void main()

{

     float score[SIZE];

     int i;

      // loop to read in the scores into the array

      printf("Enter %d floats: ", SIZE);

     for(i = 0; i <= (SIZE - 1); i = i + 1)

            scanf_s("%f", &score[i]);

     // loop to write out the scores from the array

      printf("The scores in reverse order are: \n");

     for(i = SIZE - 1; i >= 0; i = i - 1)

            printf("%.2f\t", score[i]);

      printf("\n");

}

  • Arrays are identified easily by the brackets following their names, both in their declaration and in their usage. Here, score[] is an array. The variable score[] has brackets after it in its declaration. The number in the brackets is 4 because the value of SIZE is 4. This means that score has four elements associated with it. We can call them slots. Furthermore, since the array is declared as a float, score can store floating points in each of these slots. Take note that, with strings a null character at the end signals where the last character is stored. In the previous example you can see that a variable (SIZE) may be used to hold the number of elements present in the array. Take note also about the counting of the elements in arrays start at 0 and not 1. For example:

score[4];

  • The counting of the element, the index started at 0. So the score[4] array variable will be something like the following:

score[0],score[1],score[2],score[3]

  • But in typical array representation, the following array statements look similar, so be careful. It can be the array size (normally in array declaration) or array index (when used in program). The first one is the declaration of an array named score with size of 2 and the second one is the array element with index number 3.

int score[2] and score[2]

  • Then you have to be careful with this issue. You must differentiate between array index, that start from 0 and the array size that is the real array size. The formal name for aslot number is index. Its plural is either indices or indexes. Some books usesubscript. The highest index used with an array should always be one less than its size. For the previous example, the highest index used with score should be 3, which is one less than its size (4 – 1 = 3). All the information of an array for the previous example can be depicted in the following simplified Figure.

Searching An Array Element

  • Let take a look at another example of array program, searching an array element. Build and run the following program.

#include<stdio.h>

 

void main()

{

     // an array to store a list of scores

     float score[12],

     // the array is searched for this score

      lookup;

     // used as an index to step through the array

     int i,

     // the index of the last score stored in the array

      max;

     // read in the scores into the array

      printf("Enter up to 10 floats, enter 0 when done: \n");

      scanf_s("%f", &score[0]);

     for(i = 0; score[i] != 0; i = i + 1)

            scanf_s("%f", &score[i + 1]);

      max = i - 1;

     // read a score to be searched in the array

      printf("What score do you want to look up? ");

      scanf_s("%f", &lookup);

     // search the array for this score

     for(i = 0; i <= max; i = i + 1)

           if(score[i] == lookup)

           // abandon the loop if element is found

           break;

     // if it was found, then a break was executed and "i" was <= "max"

     if(i <= max)

            printf("The score of %.2f was number %d in the list.\n", lookup, i + 1);

     // otherwise, "i" went past the value of "max".

     else

            printf("The score of %.2f was not found in the list.\n", lookup);

}

 

 

 

 

 

A sample input and output.

 

 

Array Initialization

char myname[ ] = "Mike";

char yourname[5] = {'M','i','k','e','\0'};

int score[5] = {4, 2, 654, 0, 78};

More Practice

#include<stdio.h>

 

void main()

{

     char a[11] = "Boring";

      printf("Index 0 has %c\n", a[0]);

      printf("Index 1 has %c\n", a[1]);

      printf("Index 2 has %c\n", a[2]);

      printf("Index 3 has %c\n", a[3]);

      printf("Index 4 has %c\n", a[4]);

      printf("Index 5 has %c\n", a[5]);

      printf("Index 6 has %c\n", a[6]);

      printf("Index 7 has %c\n", a[7]);

      printf("Numerically, the a[6] is %d\n", a[6]);

}

  1. How many slots/indexes does this array have?

  2. How many characters, counting the null character at the end, does this array hold?

  3. The number of an index is also called an index. What is the lowest index?

  4. What is the highest index for this array?

  5. Is the number for the highest index the same as that for the number of indexes?

  6. What is stored in the last index as a character, that is, in an index number 5?

  7. What is stored in the last index numerically? All strings should have this null character stored in its last index to signal the end of the string.

  1. 11.

  2. 7 characters including the null character.

  3. 0.

  4. 10.

  5. No. The number of indexes are 11 but the highest index is 10 because the index counting starts from 0 instead of 1.

  6. A 'g' character.

  7. A null character.

  

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

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

            printf("Index %d has %c\n",  i, a[i]);

}

 

  1. When we wanted to print the index or the number of the index, did we print i or a[i]?

  2. When we wanted to print what was stored in an index, did we print i or a[i]?

  3. Are i and a[i] the same?

  4. Which of the above is the value stored in an index?

  5. Which is the index number itself?

  1. We print i.

  2. a[i] in this case.

  3. No. i is the index but a[i] represent the data stored.

  4. a[i].

  5. i.

  
  • In the following example, the loop goes up to only “i = 9”.

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

     for(i = 0; i <= 9; i = i + 1)

            printf("%c", a[i]);

      printf("\n");

}

 

  1. Is the “\n” printed inside the loop or after it? Why?

  2. If it is printed inside the loop, how would you put it after?

  3. If it is printed after the loop, how would you put it inside?

  4. Can you remember an easy way to write out a string using one printf() and no loop?

  1. After or outside the loop because the for body doesn't have curly braces, { }.

  2. No. We can't put it after the loop to print it inside the loop.

  3. No. We can't put it inside the loop to print it after the loop.

  4. Just put the string as a literal sting or as an argument in the printf() such as printf("sweet girl");

 

 

 

 

 

 

  • Complete the for loop in the following program so that the characters of the array are printed in reverse order, starting from index number 10 down to slot number 0?

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

     

     for(_____; __________________; _______)

            printf("Index %d has %c\n", i, a[i]);

}

#include <stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

   

     for(i = 9; i > 0; i = i - 1)

         printf("Index %d has %c\n", i, a[i]);

}

  
  • Remember that in an assignment statement, the value on the right side of the equal sign is stored in the variable shown on its left side. Build and run the following program.

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

     

     for(i = 0; i <= 9; i = i + 1)

            a[i] = a[1]; // notice the 'i' and '1'

      printf("%s\n", a);

}

 

  1. What was stored in each index of the array?

  2. If the array were initialized to “good boy”, what would have been printed?

 

  1. A 'w' character.

  2. A 'o' character.

  
  • Next, try the following program.

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

     

     for(i = 0; i <= 9; i = i + 1)

            a[i] = '1';

      printf("%s\n", a);

}

 

  1. What was stored in each index of the array?

  2. If the array were initialized to “good boy”, what would have been printed?

  1. A '1' character.

  2. A '1' character.

  
  • First run the following program with the first printf() is commented out. Then answer the questions. Finally check your answer by uncomment the first printf() and rerun the program.

#include<stdio.h>

 

void main()

{

     char a[11] = "sweet girl";

     int i;

     for(i = 0; i <= 9; i = i + 1)

      {

            a[i] = a[i + 1];

           // printf("i = %d %s\n", i, a);

      }

      printf("%s\n", a);

}

  1. When i was 0, which index of a was changed? To which value?

  2. When i was 1, which index of a was changed? To which value?

  3. When i was 2, which index of a was changed? To which value?

  4. When i was 9, which index of a was changed? To which value?

  1. Index 0 was changed to 1. a[0+1] = a[1]. When index is 0, assign a[1] to a[0].

  2. Index 1 was changed to 2. a[1+1] = a[2]. When index is 1, assign a[2] to a[1].

  3. Index 2 was changed to 3. a[2+1] = a[3]. When index is 2, assign a[3] to a[2].

  4. Index 9 was changed to 10. a[9+1] = a[10]. When index is 9, assign a[10] to a[9].

 

When the first printf() uncommented the following is the output.

 

 

 

 

| Main |<Configure And Use Windows PSDK 2 |C & C++ Array Data Type 2 >|Site Index |Download |


The C & C++ 1D Array Aggregated Data Type Manipulation:Part 1 |Part 2 |Part 3 |Part 4