I am having some problems with writing a 2d array, then displaying it, and then showing the sum of all of the elements. A major part of this problem is that i nave to write three seperate functions to achieve this. it is supposed to do this:
For this exercise you will use both arrays and functions! Write a program called matrix.c that adds every element in a 2-dimensional array (matrix). Your matrix will be 5 x 5 (5 rows and 5 columns). The requirement is to use nested "for loops" in your program.
You must develop 3 functions within this program: One to read user input; another to display the 2-D array; and a third that returns the total of all elements in the 2-D array. The 3rd function should NOT print the value, rather the value should be returned from the function to main, and main will display the result. Your function prototype might look like this: int addArray( int arr[ ][ COLSIZE ] );
Make the program flexible so that you can easily change the size of the table. A suggestion for doing so is to use a define, for example: #define rowSIZE 5.
Input
5 x 5 matrixOutput
Output the matrixOutput the matrix sum
Below is a sample of expected output (my input is in blue - this is to illustrate only, yours will not be blue):
Please enter enter data for a 5 by 5 array:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25Our Array to sum is:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25The total of all elements in our array is: 325
Press any key to continue . . .
this is the code i have, i fear it is way wrong as i am getting error codes of "expected primary-expression before 'int' " , "expected ';' before 'int' " (both of these in my main staement line 47), and "expected '}' at end of input " (line 78 in my return statement)...
Here is my "sure to be flawed" code:
#include <stdio.h>
#include <stdlib.h>
#define rowSIZE 5
#define colSIZE 5
int myMatrix[rowSIZE][colSIZE];
int myFunction(void)
{
int row = 0,
col = 0;
for (row = 0; row < rowSIZE; row++)
for (col = 0; col < colSIZE; col++)
printf("Please enter the data for our %d by %d array: ", rowSIZE, colSIZE);
scanf("%i", &myMatrix[row][col]);
}
int printArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col;
for ( row = 0; row < rowSIZE ; row++ )
for ( col = 0; col < colSIZE ; col++ )
printf("%d\t", myMatrix[ row ][ col ]);
printf("\n");
}
int sumArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col, sum = 0;
for ( row = 0; row < rowSIZE ; row++ ) {
for ( col = 0; col < colSIZE ; col++ )
sum += myMatrix[row][col];
return sum ;
}
int main (void)
{
int sum = 0;
int row, col;
int arrayToSum[rowSIZE][colSize];
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = myFunction(myMatrix[row][col]);
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = printArray(myMAtrix[row][col])
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = sumArray(int myMatrix[row][col])
}
printf("This is the sum of our matrix: %d\n", sum);
return sum;
}