Solved

loading a multidim array by user input

Posted on 2006-07-22
8
307 Views
Last Modified: 2008-02-01
I am trying to setup a program that asks a user for 25 integers then puts them in a multidim array displays the aray table
the shows the sum of each row.

I have everything working except the for loops to get the array loaded correctly.  the table is supposed to be 5x5 I am just not sure on how to setup the test conditions to make the for loops work.

Here is my code.

//mike vlahos
//pgm1203 sum by row

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

using namespace std;

const int MaxSize = 5;

void loadarray (int array[MaxSize][MaxSize],int MaxSize);
void printTable (ostream& report, int const number[MaxSize][MaxSize],int MaxSize);
int sumRows(int number[MaxSize]);


int main(){
      int number[MaxSize][MaxSize];
      cout << " Please enter 25 integers. (type the integer then press enter. " << endl;
      loadarray(number,MaxSize);
      ofstream report ("Pgm1203_vlahos_output.txt");
      if (!report){
            cerr << "unable to open output file!" << endl;
            return 1;
      }
      printTable(report,number,MaxSize);
      for (int n=0; n<MaxSize;n++){
            report <<" The sum of Row " << n+1 << " is: " << sumRows(number[n]) << endl;
      }
      return 0;
}

void loadarray (int number[MaxSize][MaxSize],int MaxSize){
for (int i=0, j=0; i<MaxSize && j<MaxSize; i++,j++){
      for (int q=0; q<MaxSize; q++){
            cin >> number[i][j];
            }
}
}

void printTable (ostream& report, int const number[MaxSize][MaxSize],int MaxSize){
      for (int j=0; j<MaxSize; j++){
            for (int i=0; i<MaxSize; i++){
                  report << "  " << setw(3) << number[j][i] << "    ";
            }
            report << endl;
      }
}

int sumRows(int number[MaxSize]){
      int i, sum = 0;
      for (i=0; i<MaxSize; i++){
            sum += number[i];
      }
      return sum;
}
0
Comment
Question by:vlahomt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161569
Hi, try this:


void loadarray (int number[MaxSize][MaxSize],int MaxSize)
{
    for (int i=0;i<MaxSize; i++)
    {
        for (int j=0; j<MaxSize; j++)
        {
            cin >> number[i][j];
        }
    }
}




---
Harish
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 400 total points
ID: 17161585
You don't need those extra braces:

void loadarray (int number[MaxSize][MaxSize],int MaxSize)
{
    for (int i=0;i<MaxSize; i++)
        for (int j=0; j<MaxSize; j++)
            cin >> number[i][j];
}

0
 

Author Comment

by:vlahomt
ID: 17161590
I think I tried that and it stoped my data entry after only 5 numbers because at that point the 6 number would make j>MaxSize.

I will try that though
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161614
vlahomt, try and don't hesitate to ask if you don't get it :)
0
 

Author Comment

by:vlahomt
ID: 17161705
that does not work.  it is like i said once you enter the 5th number j>MaxSize.

0
 
LVL 9

Expert Comment

by:DrAske
ID: 17162547
Hi valhomt ..

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

using namespace std;

const int MaxSize = 5;

void loadarray (int array[MaxSize][MaxSize],int MaxSize);
void printTable (ostream& report, int const number[MaxSize][MaxSize],int MaxSize);
int sumRows(int number[MaxSize]);


int main(){
     int number[MaxSize][MaxSize];
     cout << " Please enter 25 integers. (type the integer then press enter. " << endl;
     loadarray(number,MaxSize);
     ofstream report ("Pgm1203_vlahos_output.txt");
     if (!report){
          cerr << "unable to open output file!" << endl;
          return 1;
     }
     printTable(report,number,MaxSize);
     for (int n=0; n<MaxSize;n++){
          report <<" The sum of Row " << n+1 << " is: " << sumRows(number[n]) << endl;
     }
     return 0;
}

void loadarray (int number[MaxSize][MaxSize],int MaxSize){
for (int i=0; i<MaxSize ; i++){
     for (int j=0; j<MaxSize; j++){
          cin >> number[i][j];
          }
}
}

void printTable (ostream& report, int const number[MaxSize][MaxSize],int MaxSize){
     for (int j=0; j<MaxSize; j++){
          for (int i=0; i<MaxSize; i++){
               report << "  " << setw(3) << number[j][i] << "    ";
          }
          report << endl;
     }
}

int sumRows(int number[MaxSize]){
     int i, sum = 0;
     for (i=0; i<MaxSize; i++){
          sum += number[i];
     }
     return sum;
}
----------------------------------------------
input :
1 2 3 4 5 6 ..... 25
-----------------------------------------------
output to File :
    1        2        3        4        5    
    6        7        8        9       10    
   11       12       13       14       15    
   16       17       18       19       20    
   21       22       23       24       25    
 The sum of Row 1 is: 15
 The sum of Row 2 is: 40
 The sum of Row 3 is: 65
 The sum of Row 4 is: 90
 The sum of Row 5 is: 115
------------------------------------------------

So where is your problem??
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17162723
>> that does not work.  it is like i said once you enter the 5th number j>MaxSize.

Use an ANSI compliant compiler such as MSVC or GCC..

I have tested it in GCC and it works perfectly.
0
 

Author Comment

by:vlahomt
ID: 17163960
I had a typo sorry.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question