C Lab Worksheet 10A 2D Array Manipulation Part 2
C Lab Worksheet 10A 2D Array Manipulation Part 2
7. 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");
}
} a. Element in third row and fourth column.
b. Element in third row and third column.
a. When i is 2 and j is 3, the element in which row and which c. Element in the third row and first column.
column is printed? d. Element in second row and fourth column.
b. When i is 2 and j is 2, the element in which row and which e. i is 0 and j is 0.
column is printed?
c. When i is 2 and j is 0, the element in which row and which It is confusing isn't it? To make it clearer we can include another curly braces in the array
column is printed? declaration as shown below. The order of the element in the array won't change in this
d. When i is 1 and j is 3, the element in which row and which program. However how the elements displayed can be different.
column is printed?
e. When the last element is printed, what is i and what is j? a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
8. 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");
}
}
Answers for the reading loop:
#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");
}
}
a. 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?
b. When reading and when i was 0 and j was 0, show the
character that was read in the correct slot in the Figure.
c. When i was 0 and j was 1, show the character that was read.
d. Fill in all elements in the array as they are read in. Does your a. From the first two for loops, i <=3 and j <= 2.
answer match with the output? b. When j was 0 and j was 0, the character read was a.
c. The character read was d.
10. 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");
}
}
a. Show the contents of the array in memory after the three strings
are read in the array. a. Shown above.
b. Does your output agree? b. The output matched except the garbage.
c. How is the null character, ‘\0’ printed? c. Just an empty space.
d. Is there a garbage character in a[1][3]? If so, why? d. Yes. This slot has been reserved but not filled so whatever the previous data that
has been stored here would be displayed (if possible).
11. 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)
{ In this program we assign or fill the array with integer 7.
a[i][j] = 7;
printf("a[%d][%d] = %d\t", i, j, a[i][j]);
}
printf("\n");
}
}
#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 In this program, if the i and j index are equal then fill an integer 7 and fill the rest with
{ integer 8.
a[i][j] = 8;
printf("a[%d][%d] = %d\t", i, j, a[i][j]);
}
}
printf("\n");
}
}
12. 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) The array elements are the yields of the i - j or subtraction operations.
printf("(%d - %d) = %d\t", i, j, i - j);
printf("\n");
}
}
#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); In this program the array element are the yields of the 2*i-j operations while j <= i.
printf("\n");
}
}
13. 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");
}
}
#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");
}
}
#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");
}
}
#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");
}
}
www.tenouk.com
| Main |< C & C++ 2D Array 1 | C & C++ 2D Array 3 >| Site Index | Download |
tenouk.com, 2007