| Main |< C & C++ 2D Array 1 | C & C++ 2D Array 3 >| Site Index | Download |


 

 

 

 

 

C LAB WORKSHEET 10a

2D Array Manipulation Part 2

 

 

 

 

 

  1. More practice on 2D array data type.

  2. Array indexes (rows and columns) manipulation.

  3. Character, strings and 2D array.

  4. Tutorial references that should be used together with this worksheet are C & C++ array part 1 and C & C++ array part 2.

 

 

  1. Change the previous code with the following modification. Show the output and answer the questions.

 

#include <stdio.h>

 

void main()

{

      int i, j, a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

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

      {

            for(j = 3; j >= 0; j = j - 1)

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

            printf("\n");

      }

}

  1. When i is 2 and j is 3, the element in which row and which column is printed?

  2. When i is 2 and j is 2, the element in which row and which column is printed?

  3. When i is 2 and j is 0, the element in which row and which column is printed?

  4. When i is 1 and j is 3, the element in which row and which column is printed?

  5. When the last element is printed, what is i and what is j?

C & C++ 2D array - reading and writing data into arrays

  1. Element in third row and fourth column.

  2. Element in third row and third column.

  3. Element in the third row and first column.

  4. Element in second row and fourth column.

  5. i is 0 and j is 0.

 

It is confusing isn't it? To make it clearer we can include another curly braces in the array declaration as shown below. The order of the element in the array won't change in this program. However how the elements displayed can be different.

a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

 

 

 

 

 

 

 

 

 

 

 

  1. In the following experiment, the first loop scans characters into a character array and the second prints them. For both loops, i stays fixed while j varies or the row stays fixed while the column varies. Hence, the data is read in the array row-wise and printed out row-wise. Enter this data: a, b, c, d, e, f, g, h, i, j, k, l. Remember to put a space preceding the %c in the scanf_s(). Show the output and answer the questions.

 

#include <stdio.h>

 

void main()

{

      int i, j;

      char a[3][4];

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

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

                  // scanf(" %c", &a[i][j]);

                  scanf_s(" %c", &a[i][j], sizeof(a));

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

      {

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

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

            printf("\n");

      }

}

 

C & C++ 2D array illustrated in a table

 

Questions for the reading loop:

  1. Initially when i is 0 and j is 0, the first character entered is read. In the Figure above, write this first character read in the proper slot.

  2. Write the second character in its proper slot. What is i and j?

  3. Fill in all the characters that are read into this array properly.

 Questions for the printing loop.

  1. Initially when i is 0 and j is 0, which character is printed?

  2. What is the second character printed?

  3. How many characters are printed before the ‘\n’ is printed?

  4. Was the first row or the first column of the array printed first?

 

Answers for the reading loop:

 

C & C++ 2D array - a very confusing rows and columns manipulation

 

C & C++ 2D array depicted in rows and columns

  1. From the above figure, the green colour is i (row) and the red colour is j (column). Then, i is 0 and j is 1.

 

Answers for the printing loop:

  1. 'a' was printed.

  2. 'b' is the second character that was printed.

  3. Four characters.

  4. The first row was printed first.

   
  1. Use the same array and the input data from the previous program example. Do some modification as shown below for the previous code that is for reading only, i and j have been reverse.

 

#include <stdio.h>

 

void main()

{

      int i, j;

      char a[3][4];

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

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

                  // scanf(" %c", &a[j][i]);

                  scanf_s(" %c", &a[j][i], sizeof(a));

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

      {

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

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

            printf("\n");

      }

}

 

C & C++ 2D array - reading and writing characters into 2D array

  1. When reading, notice that j and not i is the row subscript. This is because j is used as the first index with a[ ][ ]. When reading, why did i go up to 3 and j go only to 2?

  2. When reading and when i was 0 and j was 0, show the character that was read in the correct slot in the Figure.

  3. When i was 0 and j was 1, show the character that was read.

  4. Fill in all elements in the array as they are read in. Does your answer match with the output?

 

 

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

 

C & C++ 2D array - manipulating indexes, for loop and character data values

 

C & C++ 2D array - characters data seen in an array

  1. From the first two for loops, i <=3 and j <= 2.

  2. When j was 0 and j was 0, the character read  was a.

  3. The character read was d.

   
  1. Strings are read in by the rows. Each row will have one string. Enter the following data: “you”, “my”, “luv”. Remember that after each string, a null character is added. We are reading in strings but printing out only characters.

 

#include <stdio.h>

 

void main()

{

      int i, j;

      char a[3][4];

      printf("Give three 3-characters strings.\n");

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

            // scanf("%s", &a[i]);

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

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

      {

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

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

            printf("\n");

      }

}

 

C & C++ 2D array columns and rows indexes represented in a table

 

  1. Show the contents of the array in memory after the three strings are read in the array.

  2. Does your output agree?

  3. How is the null character, ‘\0’ printed?

  4. Is there a garbage character in a[1][3]? If so, why?

 

C & C++ 2D array - reading and writing strings into 2D arrays

  1. Shown above.

  2. The output matched except the garbage.

  3. Just an empty space.

  4. Yes. This slot has been reserved but not filled so whatever the previous data that has been stored here would be displayed (if possible).

   
  1. Show the outputs for the following program examples. Can you explain what does the following program do?

 

#include <stdio.h>

 

void main()

{

      int i, j, a[3][4];

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

      {

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

            {

                  a[i][j] = 7;

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

            }

                  printf("\n");

      }

}

C & C++ 2D array - filing 2D array with similar integers

 

In this program we assign or fill the array with integer 7.

   

#include <stdio.h>

 

void main()

{

      int i, j, a[3][4];

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

      {

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

            {

                  if(i == j)

                  {

                        a[i][j] = 7;

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

                  }

                  else

                  {

                        a[i][j] = 8;

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

                  }

            }

                  printf("\n");

      }

}

 

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

C & C++ 2D array - if the i and j index are equal then fill an integer 7 and fill the rest with integer 8

 

In this program, if the i and j index are equal then fill an integer 7 and fill the rest with integer 8.

   
  1. Show the output for each question and explain what the codes do.

 

#include <stdio.h>

 

void main()

{

      int i, j;

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

      {

            for(j = 1; j <= 3; j = j + 1)

                  printf("(%d - %d) = %d\t", i, j, i - j);

            printf("\n");

      }

}

C & C++ 2D array - array elements are the yields of the  i - j or subtraction operations

 

The array elements are the yields of the  i - j or subtraction operations.

   

#include <stdio.h>

 

void main()

{

      int i, j;

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

      {

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

                  printf("(2 * %d - %d) = %d\t", i, j, 2 * i - j);

            printf("\n");

      }

}

C & C++ 2D array - array element are the yields of the 2*i-j operations while j <= i

 

In this program the array element are the yields of the 2*i-j operations while j <= i.

   
  1. Show the output for each of the following programs. Use the following sample array data:

 

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

 

#include <stdio.h>

 

void main()

{

      int i, j;

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

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

      {

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

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

            printf("\n");

      }

}

C & C++ 2D array manipulation sample output

   

#include <stdio.h>

 

void main()

{

      int i, j;

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

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

      {

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

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

            printf("\n");

      }

}

C & C++ 2D array manipulation sample program output

   

#include <stdio.h>

 

void main()

{

      int i, j;

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

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

      {

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

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

            printf("\n");

      }

}

C & C++ 2D array - i and j indexes sample program output

   

#include <stdio.h>

 

void main()

{

      int i, j;

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

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

      {

            for(j = 2; j >= 0; j = j - 1)

                  printf("a[2][%d] = %d\t",  j, a[2][j]);

            printf("\n");

      }

}

C & C++ 2D array rows and columns manipulation sample output

 

 

 

 

 

www.tenouk.com

 

| Main |< C & C++ 2D Array 1 | C & C++ 2D Array 3 >| Site Index | Download |


The C & C++ 2D Array Manipulation: Part 1 | Part 2 | Part 3