Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays)
Part 1:
Create a 2-by-3 integer array and initialize it with 6 integers of your choice.
Using nested for loops, display the contents of the array on the screen
Part 2: (Continue on part 1)
Display the sum of all the integers in the array that you created in part 1.
Create a function
Part 3:
Create a function that will display the elements of the array you created in part 1. Call the function from main to display the 6 integers.
What to submit:
The code is written in C
#include <stdio.h>
void display(int arr[][3])
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",arr[i][j]); // Printing the array element
}
printf("\n");
}
}
int find_sum(int arr[][3])
{
int sum=0;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
sum+=arr[i][j]; // Adding the value of array element to the sum
}
}
return sum;
}
int main(void) {
int arr[2][3]; // PART 1 starts
int number=1;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=number; // Initializing the array element with a number
number++;
printf("%d ",arr[i][j]); // Printing the array element
}
printf("\n");
} // PART 1 ends
int sum = find_sum(arr); // PART 2
printf("%s","The sum is: ");
printf("%d\n",sum);
display(arr); // PART 3
}
Following is the output:
Get Answers For Free
Most questions answered within 1 hours.