Hey! Im new to this C++ stuff and the first major problem I run into is a two dimensional array. I have to declare the array and store data values from ANOTHER data file. I have some of the code written down. Please help and tell me what I'm doing wrong! Oh the one dimensional array, I'm not allowed to use...

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int NROW = 20; //number of rows
const int NCOL = 10; // number of columns
int a[NROW][NCOL]; // two dimensional array
int main()
{
 ifstream infile;
 string fname;
 int x;
 int cnt;
 //Opening input file for reading
 cout << "Enter input file name: ";
 cin >> fname;
 infile.open(fname.c_str());
 if(!infile) {
   cerr << "Error: Can't open input file." << endl;
   return 1;
}
 //Reading data items from input file and storing into an array
 cnt = 0;
  infile >> x;
 while(infile && cnt < a[NROW][NCOL]){
   a[cnt][NCOL] = x;
   ++cnt;
 infile >> x;
}
infile.close();
  // print values in table format
     for(int i = 0; i < x; ++i) {
         for(int j = 0; j < x; ++j) 
    cout << setw(3) << a[i][j];
    cout << endl;
     }


// Printing Votes 
 cout << setw(40) << "*****************" << endl; 
 cout << setw(40) << "*-----Votes-----*" << endl; 
 cout << setw(40) << "*****************" << endl;
 cout << endl;


// Printing Regions

{
  int n;
  int c;
  int v;
  //cout << "n =  ";
  //cin >> n; // Input number of regions

  n = a[NROW,NCOL]; //giving n the value of first element in the array(Regions)
  c = a[NROW,NCOL + 1]; // giving c the value of the candidate number

//cout << n << endl;

  cout << " Candidates " << setw(2);
  for(int i = 1; i <= n; ++i) // Prints number of regions
    cout << "  Region" << setw(2) << i << "  ";
    cout << "    Total" << setw(2); // Prints total after regions
  cout << endl;
  for(int i = 1; i < n + 3; ++i)// Prints line after regions
    cout << " ---------- ";
  cout << endl;
  for(int i = 1; i <= c; ++i){ //Prints number of candidates
    cout << setw(7) << i;
  cout << endl;
  }
}
/*
// Printing whats in array

  cout << endl;
    for(int i = 0; i < cnt; ++i){
        cout << setw(5) << a[i] << endl;
}
*/

return 0;
}

Its a voter turnout in different regions, that has to be printed in table format.... Please help in anyway. The one dimensional array is commented out. Please make it to where it is simple for me to comprehend.

There are the values stored in the other data table

4
5
3 2 12
2 1 67
5 3 21
2 3 38
1 2 10
4 2 45
5 4 16
3 3 29
2 4 20
1 4 78
4 3 24
3 1  8
2 2 26
1 3 27
5 1 31
1 1 35
5 2  9
4 4 52
4 1 52
3 4 17

First line = candidates
second line = region
after those two it starts off as 3 1 12 first is candidate, region, amount of votes.

Thanks in advance!

Edited 3 Years Ago by Reverend Jim: Fixed formatting

while(infile && cnt < a[NROW][NCOL]){
a[cnt][NCOL] = x;
++cnt;
infile >> x;
}

You're while test is wrong. a[NROW][NCOL] has some random value stored in it because it hasn't been initialized yet, same as a it would in a simple one dimensional array. So, what you're saying is "while cnt is smaller than some random number (maybe 15889)" do something. Where what you want to say is:

while (infile && cnt < NROW) { do something }

Also, remember that a multiple dimensional array has rows and columns, like a spreadsheet. When you run:

a[cnt][NCOL] = x;

You're saying:
a[0][10] = x;
a[1][10] = x;
a[2][10] = x;
...etc.

Notice how you're only filling it one dimensionally. You're only using column 10. Then you do.

for(int i = 0; i < x; ++i) {
for(int j = 0; j < x; ++j) 
cout << setw(3) << a[i][j];
cout << endl;
}

In this code you're incrementing both rows and columns, but your columns haven't been initialized yet so you'll be printing radom garbage.

OK, so you stated:

"First line = candidates
second line = region
after those two it starts off as 3 1 12 first is candidate, region, amount of votes."

Therefore, first you want to read in the # of candidates. This number you'll use to control your loops when displaying the contents of your array. This will be a control for the maximum number of rows or columns, whatever you decide to make it.

Next, read the # of regions. Again use this for your loops.

Lastly, you want to choose some sane way of representing the data your array. You have 10 columns and 20 rows to work with. Organize the data the way you would in a spreadsheet with 10 columns and 20 rows.

I can't seem to get the code to print out what I want. X stores the first value of the whole list, but I can't seem to store all the values.

cnt = 0;
  infile >> x;
 while(infile && cnt < NROW){
   a[NROW][NCOL] = x;
   ++cnt;
 infile >> x;
}
infile.close();

cout << x << endl; // Prints out 5
cout << a[NROW][NCOL] << endl; //Prints out 45?
cout << a[NROW]<<endl; // Prints out random garbage

I've been at this for 2 days, I wanted to do it with a single array, but the professor barely taught us two dimensional arrays and wants this whole thing done with two dimensional arrays...So confused. Any further help?

Now you're not incrementing either the column nor the row??!! A multi-dimensional array is pretty much exactly the same as a single-dimensional array where you can record data from myArray[0][0] to myArray[MAX][MAX]. So increment them both as needed.

I have to go out right now, but when I get back I'll give you an example. Look at your while() loop. Carefully think about what you want to be doing there. I'll help you more when I get back in an hour or two.

OK, so if you imaging working with a spreadsheet then you could arrange the data as such:

a1, a2, a3
b1, b2, b3
c1, c2, c3
...etc

So in an array this would be:

intArray[0][0], intArray[0][1], intArray[0][2]
intArray[1][0], intArray[1][1], intArray[1][2]
intArray[2][0], intArray[2][1], intArray[2][2]
...etc

That's of course just one way to arrange the data and it would arrange the data exactly as it is in the input file. For some c++ code that could accomplish this take a look at my example (note that I don't test the input which isn't a great idea, but it's just meant as an example and not as a final product. Hope it helps you understand.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    // MAX represents the maximum size of the array
    const int MAX = 3;

    // 3 x 3 array
    int intArray[MAX][MAX];

    //Open the file
    ifstream infile;
    infile.open("data.txt");

    if (infile.fail())
    {
        cout << "Opening the file failed\n";
        exit(1);
    }

    //Loop through the array. First for loop is for rows, second is for columns
    for (int i = 0; i < MAX; i++)
    {
        for (int x = 0; x < MAX; x++)
        {
            infile >> intArray[i][x];

            //Display Output exactly like it was input into the program
            if (x < 2)
            {
                cout << intArray[i][x] << " ";
            }
            else
            {
                cout << intArray[i][x] << endl; //add a line break after every 3rd digit
            }
        }
    }

    //Pause the program
    cin.get();
    return 0;
}

Edited 7 Years Ago by necrolin: n/a

This article has been dead for over six months. Start a new discussion instead.