I am so confused on how I am supposed to write this program. The datafile "scores.dat" contains 1000 individual student scores (integers in the range of 0 to 100) for a campus wide assessment instrument. It should read the file and load the data into an array. It should then process the array and produce the following statistics: average score, minimum score, maximum score, and count and report the number of "Excellent" (95-100), "Satisfactory" (80-94), "Normal" (50-79), and "Needs Improvement" (0-49) scores. Any help would be appreciated!
std::ifstream inp("scores.dat");
int scores[1000];
int x;
int i = 0;
while(inp >> x && i < 1000) {
scores[i++] = x;
}
Now you have an array with scores. You can iterate over it and calculate all data you need. (actually you can optimize everything to not use an array at all)
If you know the basics, then you should be able to start. The intention is most likely that you do learn by doing. Please tell, that you know Hello World?
PS. std::vector, std::accumulate, std::count_if, std::max_element, std::min_element. Probably something that you are not supposed use, because they make it all to easy and don't teach to think.
The forum definitely needs more information to help you out. Particularly, whether or not you know how to use input/output file streams (ifstream/ofstream).
I'm not a pro, but for your particular problem, I'd recommend:
1) create a structure (or class) to handle the data for each student.
1 2 3 4 5 6 7 8 9 10 11 12
struct student
{
string studentName;
int minScore;
int maxScore;
int avgScore;
int scoreCount;
int numExcellent;
int numSatisfactory;
int numNormal;
int numNeedsImprove;
};
The benefit of using classes here is that you can add member function(s) that return the excellent, satisfactory, normal, needs improvement statistics you are looking for; as well as the calculate the avgScore and other needed information. I don't have enough time to build a class but you get the drift.
2) You'll then need to create an array of this structure (or object), to hold the information for each student. You can of course dynamically allocate this if need be.
student allStudents[15];
3) Finally, you'll have to create an ifstream object and get the information needed from the file. We would need much more information about the format of your data file, but assuming it's a .txt filel, it would be something like:
Ok, so I tried to put some of it together and I don't think its working out right. Also I put in the forum that the datafile "scores.dat" contains 1000 individual student scores (integers in the range of 0 to 100).
#include <iostream>
#include <fstream>
usingnamespace std;
constint num = 15;
int main()
{
struct student
{
double allStudent[num];
string studentName;
int minScore;
int maxScore;
int avgScore;
int scoreCount;
int numExcellent;
int numSatisfactory;
int numNormal;
int numNeedsImprove;
}
ifstream fin;("Mytext.txt");
while (fin >> allStudent.studentName >> ", " >> allStudent.minScore);
{
}
return 0;
}
I don't see how it matters.. This program should be able to just count by itself and everything after you input it.. Its just a white screen and has 1000 numbers between 0-100 in it.
@todricos:
1. That struct does not match at all the "The datafile contains N individual student scores (integers in the range of 0 to 100)" description. AkramIzzeldin asks the exact format, which I bet to be closer to what MiiNiPaa assumed; a thousand numbers and nothing more.
2. student allStudents[15]; is static allocation, not dynamic. For dynamic allocation, one would look at the standard library containers.
1 2 3 4 5 6
std::vector<int> bar;
bar.reserve( 1000 );
int score = 0;
..
// within loop
bar.push_back( score );
@Cluterburg:
Structs/classes are not defined inside main(). Const 'num' can be inside main().
You now have a loop that repeatedly overwrites the value of same variable(s) from input. It should store each value to a different memory location -- "next slot on array". MiiNiPaa's loop has one example. vector::push_back() is bit different.
The assignment was quite clear:
1. Form a list (array) of numbers.
2. Look at the numbers on the list to count something.
3. Show results.
MiiNiPaa's code does the first step. 'scores' is the list. It contains 'i' numbers.
Second step is again iteration over list. You must have some variables to collect results to. For example, increase 'numExcellent' by one every time you look at a number in the list that is within limits of "excellent".
OK i will assume that the dat file is written with only the degrees seprated by a space cause you dont need the students names.
First you will make an array of ints with size 1000, then you will open the file for reading and make a for loop counts to 1000 in each time it reads from the file to the array element i, now you have the scores in the array the only thing left is calculating the average score (sum of the elements of the array / 1000.0), the lowest score (comparing every element until findin the lowest one), the highest score (test each element until finding the highest) and the exellent, weak... Scores (testing elements and counting the exellent scores, weak..).
Try this and show us if anything went wrong
So in line 15 where it says accumulate it says that 'accumulate' : is not a member of 'std' and in line 17 the open bracket says a lambda that has been specified to have a void return type cannot return a value.
Update your compiler. accumulate was member of std since forever (I believe from C++03)
It should deduce return type of lambda too. But you can just change line 16 to [&](int x, int y) -> int