reading data into 2D array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • becca0619
    New Member
    • Oct 2006
    • 1

    #1

    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;inde xcol++)
    {
    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(numb er);
    }
    }


    }
    }

    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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by becca0619
    Code:
    #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
    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!

    Comment

    Working...