c++ arrays problem

Write a program that reads from the file HW4_Q1data.txt 30 integers in the range 0 – 200. The
program then determines the number of integers in each of the following ranges: 0 - 24, 25 –
49, 50 – 74, 75 – 99, 100 – 124, 125 – 149, 150 – 174, and 175 – 200. Output the ranges
and the number of integers in each range. (Use the following values in the file HW4_Q1data.txt:
76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99,
129, 149, 176, 200, 87, 35, 157, 189, 137, 23, 192, 78).
Specifically, create the following functions:
void readData(char filename[], int list[], int size);
This function reads from file filename and populates (or increments) the list array location
belonging to each number from the file.
void print(int list[], int size);
This function will print the ranges and the number of students that have scores in a particular
range, as shown below.


So this is what I've done but its not showing me the correct output
#include <iostream>
#include <fstream>
using namespace std;
const int size=8;
void readData(char filename[], int list[], int size)
{

ifstream fin;
fin.open("HW4_Q1data.txt");
int value=0;
for(int i=0;i<30;i++)
{
fin>>filename[i];
value=filename[i];
if (value >= 0 && value <= 24)
list[0]++;

else if (value >= 25 && value <= 49)
list[1]++;

else if (value >= 50 && value <= 74)
list[2]++;

else if (value >= 75 && value <= 99)
list[3]++;

else if (value >= 100 && value <= 124)
list[4]++;

else if (value >= 125 && value <= 149)
list[5]++;

else if (value >= 150 && value <= 174)
list[6]++;

else if (value >= 175 && value <= 200)
list[7]++;
}

fin.close();
}
void print(int list[], int size)
{
cout << " Range"<<'\t'<<"# of students"<<endl;
cout << "0-24: " <<'\t'<<list[0] << endl;
cout << "25-49: " << '\t'<<list[1] << endl;
cout << "50-74: " <<'\t'<< list[2] << endl;
cout << "75-99: " <<'\t'<< list[3] << endl;
cout << "100-124: " <<'\t'<< list[4] << endl;
cout << "125-149: " <<'\t'<<list[5] << endl;
cout << "150-174: " <<'\t'<< list[6] << endl;
cout << "175-200: " <<'\t'<< list[7] << endl;

}
int main()
{

char filename[70];
int list[size];

readData(filename,list,size);
print(list,size);

return 0;
}
What help do you need ?
Does your program compile?
If not, what are the errors?
If it does compile, does the program not run as expected?
Tell us what happens and what is supposed to happen,
as well as whatever you've done to try and understand what's causing the problem.
it does compile but the values I'm getting are not correct, they're mostly zeroes. I don't understand whats the point of the char filename array, and how are we supposed to be reading from the file, should we be storing it into the filename array?
void readData(char filename[], int list[], int size);
The filename you should pass from main since it is given. You don't have to store the numbers only the count of the range. I would try it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void readData(char filename[], int list[], int size)
{
ifstream fin(filename);
if (!fin)
{
   perror("File error");
   exit(-1);
}
int value=0, count = 0;
while(fin >> value)
{
  if (value >= 0 && value <= 24)
     list[0]++;
  // same for other ranges
}
got it, thankyou!!
Please use the * code* tags when posting code. the tags should be lowercase :) it is used for proper view of code http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.