-
Print a 2D Array
How do I print a 2D array.
Code:
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <ctype.h>
int main()
{
int array [5][5]={ {2,4,6,8,10},
{12,14,16,18,20},
{22,24,26,28,30},
{32,34,36,38,40},
{42,44,46,48,50}
};
int i,j,temp[5];
//print array
printf("%d",array[5][5]);
return 0;
}
also how do I sum all the value of the array?
-
There isn't a standard function to "print an array", in the way you want. What you will have to do is loop over each row, then loop over each column--implying a for loop within a for loop--and in each iteration, you print the element of the array, corresponding to the current row and column.
For summing all values in the array, of course you need some variable that stores the sum (probably initialized to 0). I imagine you can figure out how to do it, after understanding how to print it first. Work on printing it above, making sure to understand what you are doing (versus just writing arbitrary lines of code), then make an attempt at summing all values. If you have a problem, post your complete, updated code, as well as describe the problem or tell us the error/warnings you're getting.
-
How do I get the inverse of col1?
Code:
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <ctype.h>
int printArray(int pary[][5]);
int manipulateArray(int mary[][5]);
int main()
{
//Initialize 2D Array
int array[5][5] =
{
{2,4,6,8,10},
{12,14,16,18,20},
{22,24,26,28,30},
{32,34,36,38,40},
{42,44,46,48,50}
};
int reverse;
//Print Array
printArray(array);
printf("\n\n");
//Reverse the first column
//Print the array on the screen
manipulateArray(array);
//Swap the contents of column 3 and column 4
//Print the array on the screen
//Sort row 5 in descending order
//Sum the contents of the entire array
return 0;
}
int printArray(int pary[][5])
{
int r,c; //row and col counter
for (r = 0; r <= 4; r++)
{
for (c = 0; c <=4; c++)
{
printf("\t%d", pary[r][c]);
}
printf("\n");
}
}
int manipulateArray(int mary[5][5])
{
int reverse[5]; //store temp value
int r,c; //row and col counter
for (r=4;r<=0;r++)
{
for (c<=4;c=0;c++)
{
reverse[5]=mary[0][c];
}
printf("\t%d",mary[r][c]);
}
printf("\n");
}
-
Invert it. You know... backwards.
Quzah.