| Main |< C & C++ Array Data Type 1 | C & C++ Array Data Type 3 >| Site Index | Download |


 

 

 

 

 

C LAB WORKSHEET 9_1

C & C++ Array Data Type Part 2

 

 

 

 

 

 

  1. More questions and answers.

  2. Array and loops

  3. Tutorial references that should be used together with this worksheet are array part 1 and array part 2.

 

 

  • First run the following program with the first printf() commented out then answer the questions. Finally check your answers by removing the // (uncomment).

#include <stdio.h>

 

void main()

{

      char a[11] = "sweet girl";

      int i;

 

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

      {

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

            // 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 1 was changed to 0. a[1]=a[0]. Assign a[0] to a[1].

  2. Index 2 was changed to 1. a[2]=a[1]. Assign a[1] to a[2].

  3. Index 3 was changed to 2. a[3]=a[2]. Assign a[2] to a[3].

  4. Index 10 was changed to 9. a[10]=a[9]. Assign a[9] to a[10].

 

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

 

 

 

 

 

 

 

 

 

 

 

 

  • First run the following program with the first printf() commented out then answer the questions. Finally check your answers by removing the // (uncomment).

#include <stdio.h>

 

void main()

{

      char a[11] = "sweet girl";

      int i;

 

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

      {

            a[i] = a[9 - i];

            // 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 9. a[0]=a[9], assign a[9] to a[0].

  2. Index 1 was changed to 8. a[1]=a[8], assign a[8] to a[1].

  3. Index 2 was changed to 7. a[2]=a[7], assign a[7] to a[2].

  4. Index 9 was changed to 0. a[9]=a[0], assign a[0] to a[9].

 

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

 

 

  • So far we have been looking only at character arrays that terminate with a null character or simply strings. Now let us turn our attention to numeric arrays. Here, the first loop completes before the second one starts.

#include <stdio.h>

 

void main()

{

      int i, x[6], y[6] = {3, 8, 2, 9, 4, 1};

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

           x[i] = y[i];

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

           printf("x[%d] = %d\t, y[%d] %d\n", i, x[i], i, y[i]);

}

  1. What are the names of the arrays?

  2. What are the names of the scalars? Scalars are the variables that are not arrays.

  3. How many indexes do each of the arrays have?

  4. What is the lowest index and the highest index for the arrays?

  5. Which array is initialized during its declaration?

  6. Which array is assigned values in a loop?

  7. Are the arrays printed horizontally or vertically?

  8. How would you write the code if you had wanted to write the arrays the opposite way?

  1. x and y.

  2. i.

  3. 6.

  4. The lowest index is 0 and the highest is 5.

  5. Array y.

  6. Array x.

  7. Vertically.

  8. Change the for loop components to the following for both arrays.

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

 

#include <stdio.h>

 

void main()

{

    int i, x[6], y[6] = {3, 8, 2, 9, 4, 1};

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

        x[i] = y[i];

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

        printf("x[%d] = %d,\t y[%d] %d\n", i, x[i], i, y[i]);

}

 

   
  • Run the following program and answer the questions. It is the previous program with some code has been changed.

#include <stdio.h>

 

void main()

{

      int i, x[6], y[7] = {3, 8, 2, 9, 4, 1, 0};

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

            x[i] = y[i + 1];  // Statement 1

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

            printf("x[%d] = %d\t y[%d] = %d\n", i, x[i], i, y[i]);

}

  1. When i is 0, which index of y is used? Which index of x is changed?

  2. When i is 1, which index of y is used? Which index of x is changed?

  3. When i is 5, which index of y is used? Which index of x is changed?

  4. Can you determine what would be printed if Statement 1 were changed to: x[i] = y[i] + 1;?

  5. Can you determine what would be printed if Statement 1 were changed to: x[i] = y[6 -i];?

  6. Can you determine what would be printed if Statement 1 were changed to: x[i] = y[1];?

  1. Index 1 of y was used. No change on index x.

  2. Index 2 of y was used. No change on index x.

  3. Index 6 of y was used. No change on index x.

 

 

   
  • When running the following program, enter the following input: 6, 4.0, 5.0, 2.0, 55.0, 8.0, 1.0. The first number is the count and the rest are the items that go in the array. When scanning a float, use %f instead of %.2f.

#include <stdio.h>

 

void main()

{

      int count, i;

      float a[6];

      printf("How many numbers do you have?\n");

      scanf_s("%d", &count);

      printf("Enter the number(s): \n");

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

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

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

            printf("a[%d] = %.2f\n", i, a[i]);

}

 

  1. What was the value of count?

  2. In the scanf_s() and the printf(), did i ever reach the value of count? Why or why not?

  3. The 4.0 was read into which index of the array?

  4. The 5.0 was read into which index of the array?

  1. 6.

  2. i did not reach the value of count because the terminal condition is i < count and an array index start from 0 instead of 1.

  3. Index 0.

  4. Index 1.

 

 

 

 

 

 

 

 

 

 

 

  • Enter the following input: 4.0, 5.0, 2.0, 55.0 for the below program example.

#include <stdio.h>

 

void main()

{

      int i, j;

      float a[30];

      printf("Enter floats that are < 10, 10 and above to stop:\n");

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

      for(i = 0; a[i] <= 10.0; i = i + 1)

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

      // printf("Last value of i was %d\n", i);

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

          printf("a[%d] = %.2f\n", j, a[j]);

}

 

  1. Does the scanf_s() here change the value of i?

  2. The first data item was read into which index of the array?

  3. The first time the condition a[i] <= 10.0 was checked, i was 0. Was the condition true or false? During the first time through the loop, into which index did the scanf_s() place a data item?

  4. The second time that the condition a[i] <= 10.0 was checked, i was 1? Was the condition true or false? Into which index did the next scanf_s() place a new data item during the second iteration of the loop?

  5. When the condition a[i] <= 10.0 finally became false, what was the value of i? Run the program again by removing the // (uncomment) to check your answer.

  6. In the second loop does j ever reach this last value of i in the printf()?

 

 

 

------------------------------------------------------------------------

  1. No.

  2. Index 0.

  3. True. Index 0.

  4. True. Index 2.

  5. i was 3.

  1. No, at this stage i = 3 and j = 3.

   

More Question and Activities

 

  1. Show the output from each code using the given array and for loop. Remember that the first index is always 0 and not 1. Use the same for loop with each printf(). Study the source code and the output.

 

#include <stdio.h>

 

void main()

{

      int i, a[ ] = {40, 20, 70, 10, 80, 30, 90};

      for(i = 1; i <= 5; i = i + 1)

            -----------------------------

      printf("\n");

}

 

Example:

 

#include <stdio.h>

 

void main()

{

      int i, a[ ] = {40, 20, 70, 10, 80, 30, 90};

      for(i = 1; i <= 5; i = i + 1)

            printf("%d\t",  a[i]);

      printf("\n");

}

  1. printf("%d\t",  i);

  2. printf("%d\t", a[i + 1]);

  3. printf("%d\t", a[i -1]);

  4. printf("%d\t", a[i] + a[i + 1]);

  5. printf("%d\t", a[i] + 1);

  6. printf("%d\t", a[i] + i);

 

 

 

 

 

 

 

 

   
  1. Show the contents of the new array after it is changed by each assignment statement and complete the code. Use the same for loop with each assignment statement. Start with the given array for each. When doing these, remember that during each iteration of the loop, array elements are changing. This is especially important in question 2.d, 2.f and 2.h. Study the source code and the output.

 

#include <stdio.h>

 

void main()

{

      int i, a[ ] = {40, 20, 70, 10, 80, 30, 90};

      for(i = 1; i <= 5; i = i + 1)

      {

            --------------------------------------

            --------------------------------------

      }

}

 

Example:

 

#include <stdio.h>

 

void main()

{

      int i, a[ ] = {40, 20, 70, 10, 80, 30, 90};

      for(i = 1; i <= 5; i = i + 1)

      {

            a[i] = 0;

            printf("a[%d] = %d ", i, a[i]);

      }

      printf("\n");

}

  1. a[i] = i;

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

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

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

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

  6. a[6 – i] = a[5 – i];

  7. a[5 – i] = a[6 – i];

 

 

 

 

 

 

 

 

 

 

 

 

 

 

| Main |< C & C++ Array Data Type 1 | C & C++ Array Data Type 3 >| Site Index | Download |


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