473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

reading data into 2D array

Hey, Ive been looking around to find out how to read data into a 2D array and I cant find a solution that will fit my assignment. Im not sure that I should even use an array actually...

ok so there is a text file set up like this

21909 87 98 95 96 79
21910 74 75 84 86 57
21911 84 85 82 80 79
etc... There is no defined number of items in the file. (my professor wants to be able to read in any number of "rows" but there will always be 6 columns- "student ID" and 5 imaginary test scores.

Then we need to process the data and find averages and letter grades for each student, and a class average. I thought the easiest thing would be to use a 2D array. Here is what I have so far:

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

int number;
// Function Prototypes
bool checkGrade(int);

int main()
{
int counter = 0;
ifstream inputFile;
inputFile.open("grade.txt");
if (!inputFile)
cout << "Error opening file!" << endl;
else
{
// determines number of items in input file.
int item;
while (!inputFile.eof())
{
inputFile >> item;
counter++;
}
const int rows = (counter/6); // determine number of rows in array.
int data[rows][6]; // declare 2D array for data in file. // 33!

// write data into 2D array
for (int indexrow=0; indexrow<rows; indexrow++)
{
for (int indexcol=0; indexcol<6;indexcol++)
{
inputFile >> data[indexrow][indexcol];
}
}

// process data in array
for (int indexr=0; indexr< rows; indexr++)
{
for (int indexc=0; indexc < 6; indexc++)
{
number = data[indexr][indexc];
checkGrade(number);
}
}


}
}

bool checkGrade(int num)
{
bool status;
if (num >= 1 && num <= 100)
status = true;
else
status = false;
return status;
}

When I build I get 3 errors:
-expected constant expression
-cannot allocate an array of constant size 0
-'data': unknown size
all on line 33
Oct 26 '06 #1
1 14850
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int number;
  6. // Function Prototypes
  7. bool checkGrade(int);
  8.  
  9. int main()
  10. {
  11.     int counter = 0;
  12.     ifstream inputFile;
  13.     inputFile.open("grade.txt");
  14.     if (!inputFile)
  15.         cout << "Error opening file!" << endl;
  16.     else
  17.     {
  18.         // determines number of items in input file.
  19.         int item;
  20.         while (!inputFile.eof())
  21.         {
  22.             inputFile >> item;
  23.             counter++;
  24.         }
  25.         const int rows = (counter/6); // determine number of rows in array.
  26.         int data[rows][6]; // declare 2D array for data in file. // 33!
  27.  
  28.         // write data into 2D array
  29.         for (int indexrow=0; indexrow<rows; indexrow++)
  30.         {
  31.             for (int indexcol=0; indexcol<6;indexcol++)
  32.             {
  33.                 inputFile >> data[indexrow][indexcol];
  34.             }
  35.         }
  36.  
  37.         // process data in array
  38.         for (int indexr=0; indexr< rows; indexr++)
  39.         {
  40.             for (int indexc=0; indexc < 6; indexc++)
  41.             {
  42.                 number = data[indexr][indexc];
  43.                 checkGrade(number);
  44.             }
  45.         }
  46.  
  47.  
  48.     }
  49. }
  50.  
  51. bool checkGrade(int num)
  52. {
  53.     bool status;
  54.     if (num >= 1 && num <= 100)
  55.         status = true;
  56.     else
  57.         status = false;
  58.     return status;
  59. }
When I build I get 3 errors:
-expected constant expression
-cannot allocate an array of constant size 0
-'data': unknown size
all on line 33
I can see a few errors right away.

1) In setting up counter, you have gone through the entire input file. The next time you use inputFile >> (whatever), nothing will happen, as inputFile has reached the end of file! You will need to re-open the input data file, or create a seperate ifstream variable opening the same file.

2) When defining your array, you HAVE to use a global constant. This tells the compiler how to make the array. Instead, you have rows - a constant, but one that depends on counter. In order to make your array dynamic (a.k.a. able to accept a variable as a dimension), you will have to use a pointer array.

This should take care of the errors you get. I have just a few more observations I'd like to make:

In your final loop, when you check the values to see if they are valid, you could simply say checkGrade(data[indexr][indexc]); since accessing a member of an integer array returns an integer.

In the same loop, you loop indexr from 0 to rows - this is also going to include the studentID, which I assume you do not want to check as a grade.

Still in the same loop - what is the checkGrade function doing? It returns a boolean value, but you are not storing that value into a variable, nor are you doing anything with the result at all. Rethink this loop according to the problem's specification.

Before your function prototypes, you define an integer value named number - what are you doing with it? Why is it global instead of in your main()?

Hopefully this makes sense and helps you out!
Oct 26 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
4
by: taquito | last post by:
Hi, a C newbie here. I have been reading threads here about reading txt files into array. I simply copied a couple of useful codes from here and modified to fit my situations. Then, I have two...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
3
by: jordanbondo | last post by:
I have no clue why this isn't working. The top part that reads into size, shortTime, longTime, and threshold work fine. Down below where I try to do the exact same thing and read into x, i get...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.