Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Processing two-dimensional arrays (http://www.go4expert.com/forums/processing-dimensional-arrays-t28277/)

LoveDream 27Apr2012 04:05

Processing two-dimensional arrays
 
The program is constantly reporting an error and I cannot debug it. The problem is that I don't know what is actually wrong... can someone just tell me where I made a mistake?

Code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

const int NUMBER_OF_ROWS = 7;
const int NUMBER_OF_COLUMNS = 6;

int matrix[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int row;
int col;
int sum;
int largest;
int temp;



int main()
{

        row = 4;
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
matrix[row][col] = 0;


for (row = 0; row < NUMBER_OF_ROWS; row++)
{
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
cout << setw(5) << matrix[row][col] << " ";
cout << endl;
}


for (row = 0; row < NUMBER_OF_ROWS; row++)
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
cin >> matrix[row][col];


sum = 0;
row = 4;
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
sum = sum + matrix[row][col];


//Sum of each individual row
for (row = 0; row < NUMBER_OF_ROWS; row++)
{
sum = 0;
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
sum = sum + matrix[row][col];
cout << "Sum of row " << row + 1 << " = " << sum << endl;
}


//Sum of each individual column
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
{
sum = 0;
for (row = 0; row < NUMBER_OF_ROWS; row++)
sum = sum + matrix[row][col];
cout << "Sum of column " << col + 1 << " = " << sum
<< endl;
}


row = 4;
largest = matrix[row][0]; //Assume that the first element of
//the row is the largest.
for (col = 1; col < NUMBER_OF_COLUMNS; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];



//Largest element in each row
for (row = 0; row < NUMBER_OF_ROWS; row++)
{
largest = matrix[row][0]; //Assume that the first element
//of the row is the largest.
for (col = 1; col < NUMBER_OF_COLUMNS; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
cout << "The largest element in row " << row + 1 << " = "
<< largest << endl;
}
//Largest element in each column
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
{
largest = matrix[0][col]; //Assume that the first element
//of the column is the largest.
for (row = 1; row < NUMBER_OF_ROWS; row++)
if (largest < matrix[row][col])
largest = matrix[row][col];
cout << "The largest element in column " << col + 1
<< " = " << largest << endl;
}


xpi0t0s 28Apr2012 14:34

Re: Processing two-dimensional arrays
 
You might feel a slight tingling while I read your mind to get the error message(s). If you find it unpleasant (some do), post the errors here instead.


All times are GMT +5.5. The time now is 11:46.