-
c help arrays
I am a newbie trying create an array that will sum the row and columns. I have been able to get them to display,but not total. Any help would be great.
Code:
#include <stdio.h>
void main(void)
{
int val[5][5] = {8,3,9,0,10,
3,5,17,1,1,
2,8,6,23,1,
15,7,3,2,9,
6,14,2,6,0};
void display (int nums[5][5]); /* function prototype */
display(val);
}
void display(int nums[5][5])
{
int row_num, col_num;
for (row_num = 0; row_num <5; ++row_num)
{
for(col_num = 0; col_num <5; ++col_num)
printf("%5d",nums[row_num][col_num]);
printf("\n");
}
}
-
So want every part of the array to be sumed with all the other parts, if not can you say it again, i didnt really understand.
-
C help arrays
I need to know how to total all the rows and columns in the arrays
-
Something like this.
Code:
#include <stdio.h>
void display(int nums[5][5])
{
int r, c;
for ( r = 0; r < 5; ++r )
{
int sum = 0;
for ( c = 0; c < 5; ++c )
{
printf("%5d ", nums[r][c]);
sum += nums[r][c];
}
printf("sum = %d\n", sum);
}
}
int main(void)
{
int val[5][5] =
{
{8,3,9,0,10},
{3,5,17,1,1},
{2,8,6,23,1},
{15,7,3,2,9},
{6,14,2,6,0},
};
display(val);
return 0;
}
/* my output
8 3 9 0 10 sum = 30
3 5 17 1 1 sum = 27
2 8 6 23 1 sum = 40
15 7 3 2 9 sum = 36
6 14 2 6 0 sum = 28
*/
-
Hi,
I have made some changes to your program, read my comments.
Code:
#include <stdio.h>
int display (int nums[5][5]); /*This function will return the total*/
int main(void) /*main should ALWAYS return an integer*/
{
int val[5][5] = {8,3,9,0,10,
3,5,17,1,1,
2,8,6,23,1,
15,7,3,2,9,
6,14,2,6,0};
int total;
total=display(val); /*Returned back total from display*/
printf("\nTotal is:%d",total);
return 0; /*Since main was defined as int main(void) */
}
int display(int nums[5][5]) /*This line was causing trouble*/
{
int row_num, col_num;
int total=0; /*This variable will hold your total*/
for (row_num = 0; row_num <5; ++row_num)
{
for(col_num = 0; col_num <5; ++col_num)
{
printf("%5d",nums[row_num][col_num]);
total = total + nums[row_num][col_num]; /*total calc */
}
}
return total; /*Returning total back to main*/
}
Make sure you go through this program and understand what's going on.
-
c help arrays
Hi Sahil I am getting the folowing errors. I am using VB to compile.
compiling...
summ arrayhelp.cpp
c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(25) : error C2556: 'void __cdecl display(int [][5])' : overloaded function differs only by return type from 'int __cdecl display(int [][5])'
c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(3) : see declaration of 'display'
c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(25) : error C2371: 'display' : redefinition; different basic types
c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(3) : see declaration of 'display'
Error executing cl.exe.
summ arrayhelp.obj - 2 error(s), 0 warning(s)
-
Oh!, I am sorry I made a typo, but you should have figured that out yourself, the function prototype and the definition dont match, I am fixing that right now.
-
thanks, I didn't catch it