473,614 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading data into 2D array

1 New Member
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
Oct 26 '06 #1
1 14878
Ganon11
3,652 Recognized Expert Specialist
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
3019
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 when reading out the data into the PHP variables / array. It should be displaying the information out in the following way, using the PHP array: n_date, n_headline, n_summarytext, n_fulltext
6
5629
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 that array back into another file. I've tried, but i can only read the data and place it on the screen, i cant get it into an array. Any help would be helpful.
3
9503
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
21
13069
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
6
5259
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
8347
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 delivery of Travis Oliphant's NumPy manual, I have a quick question (hopefully) regarding how to read in Fortran written data. The data files are not binary, but ASCII text files with no formatting and mixed data types (strings, integers,...
7
11775
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) Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are 'parallel' in the sense that the n-th element of the authors...
4
2765
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 questions. I have a data set whose data sizes vary. let's say: data A: 100*20 array; data B: 180*20 array; data C: 250*20 array;
21
3040
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
3
2791
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 segmentation fault every single time. It seems to me that everything is exactly the same. Reading a value from the file into an int*. Does anyone see what is wrong here? Don't worry about the array and other stuff. I'm going to be putting the values...
0
8185
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8630
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7104
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6092
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4053
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4129
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.