1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Processing two-dimensional arrays

Discussion in 'C++' started by LoveDream, Apr 26, 2012.

  1. LoveDream

    LoveDream New Member

    Joined:
    Apr 24, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page