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!