944,074 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4844
  • C++ RSS
Mar 2nd, 2005
0

How can I get Highest, Lowest and Average ?

Expand Post »
The assignment is to read and keep records and perform statistical analysis for a class of students.
This is what I have done so far,then the codes can show the information(Id,4quizes,exam) of each students on screen. But I need to show 3 more things (The Highest ,The Lowest,The Average of each quiz and exam ).
How can I get those ? I can't go ahead. Could anyone have an idea?

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

#include "Student.h"

int main()
{
Student stuArray[50];
int stuQuiz[4];
int i;
int numStudents = 0;
int idIn;
int quizIn[4];
int examIn;

ifstream inFile("page595data.txt");
if(!inFile.is_open())
{
cerr << "Could not open input file. Terminating program." << endl;
system("pause");
exit(100);
}

while(!inFile.eof())
{
inFile >> idIn;
for( i = 0; i < 4; i++)
inFile >> quizIn[i];
inFile >> examIn;
if(!inFile.eof())
{
stuArray[numStudents] = Student(idIn, quizIn, examIn);

numStudents++ ;
}
} // while


cout << setw(8) << "ID" << setw(8) << "Quiz 1" << setw(8) << "Quiz 2" ;
cout << setw(8) << "Quiz 3" << setw(8) << "Quiz 4" <<setw(8)<< "Exam" << endl ;
for(i=0; i < numStudents; i++)
stuArray[i].printStudent();



cout << setw(8) << "High" << endl;
cout << setw(8) << "Low" << endl;
cout << setw(8) << "Average" << endl;;
system("pause");
return 0;
} // main
--------------------------------------------------
class Student
{
private:
int id;
int quiz[4];
int exam;
public:
Student();
Student(int idIn,int quizIn[],int examIn);
void getStudent(int& idOut, int quizOut[], int& examOut);
void printStudent();

}; // Student
-----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
#include "Student.h"
Student :: Student ()
{

}
Student :: Student(int idIn,int quizIn[],int examIn)
{
id = idIn ;
quiz[0] = quizIn [0];
quiz[1] = quizIn [1];
quiz[2] = quizIn [2];
quiz[3] = quizIn [3];
exam = examIn ;
}

void Student :: printStudent()
{
cout << setw(8) << id << setw(8) << quiz[3] << setw(8) << quiz[1] ;
cout << setw(8) << quiz[2] << setw(8) << quiz [3] << setw(8) << exam << endl;
}
It looks weird , but it worked so far.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tenoran is offline Offline
9 posts
since Mar 2005
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

To find the largest value in a list, save the first value and walk through the list, saving the largest:
C++ Syntax (Toggle Plain Text)
  1. T largest = list[0];
  2. for (int i = 1; i < list.size(); i++) {
  3. if (list[i] > largest)
  4. largest = list[i];
  5. }
  6. cout<<"The largest value is "<< largest <<endl;
To find the smallest, just reverse the test:
C++ Syntax (Toggle Plain Text)
  1. T smallest = list[0];
  2. for (int i = 1; i < list.size(); i++) {
  3. if (list[i] < smallest)
  4. smallest = list[i];
  5. }
  6. cout<<"The smallest value is "<< smallest <<endl;
To find the average of all values in a list, sum them and then divide by how many items there were:
C++ Syntax (Toggle Plain Text)
  1. T sum = 0;
  2. for (int i = 0; i < list.size(); i++)
  3. sum += list[i];
  4. cout<<"The average is "<< sum / list.size() <<endl;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

Take a look at:
http://www.daniweb.com/code/snippet139.html

It might just give you an idea.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

Thanks for trying. But my queestion was little bit unclear.
I will put the txt file and out put that I get so far.

txt file out put
1234 052 007 100 078 034 Id Quiz1 Quiz2 Quiz3 Quiz4 Exam
2134 090 036 090 077 030 1234 52 7 100 78 34
3124 100 045 020 090 070 2134 90 36 90 77 30
4532 011 017 081 032 077 3124 100 45 20 90 70
5678 020 012 045 078 034 4532 11 17 81 32 77
6134 034 080 055 078 045 :
7874 060 100 056 078 078 :
8026 070 010 066 078 056 :
9893 034 009 077 078 020 :
1947 045 040 088 078 055 :
2877 055 050 099 078 080 :
3189 082 080 100 078 077 Highest
4602 089 050 091 078 060 Lowest
5405 011 011 000 078 010 Average
6999 000 098 089 078 020

What I need know is how I can get The highest and the lowest and The average of each. How can I add quiz1 in 1st student to quiz1 in 2nd student ? How can I compare them?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tenoran is offline Offline
9 posts
since Mar 2005
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

Code tags will preserve the spacing.
Quote originally posted by tenoran ...
Thanks for trying. But my queestion was little bit unclear.
I will put the txt file and out put that I get so far.
C++ Syntax (Toggle Plain Text)
  1. txt file out put
  2. 1234 052 007 100 078 034 Id Quiz1 Quiz2 Quiz3 Quiz4 Exam
  3. 2134 090 036 090 077 030 1234 52 7 100 78 34
  4. 3124 100 045 020 090 070 2134 90 36 90 77 30
  5. 4532 011 017 081 032 077 3124 100 45 20 90 70
  6. 5678 020 012 045 078 034 4532 11 17 81 32 77
  7. 6134 034 080 055 078 045 :
  8. 7874 060 100 056 078 078 :
  9. 8026 070 010 066 078 056 :
  10. 9893 034 009 077 078 020 :
  11. 1947 045 040 088 078 055 :
  12. 2877 055 050 099 078 080 :
  13. 3189 082 080 100 078 077 Highest
  14. 4602 089 050 091 078 060 Lowest
  15. 5405 011 011 000 078 010 Average
  16. 6999 000 098 089 078 020
What I need know is how I can get The highest and the lowest and The average of each. How can I add quiz1 in 1st student to quiz1 in 2nd student ? How can I compare them?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

Quote originally posted by tenoran ...
Thanks for trying. But my queestion was little bit unclear.
I will put the txt file and out put that I get so far.

txt file out put
1234 052 007 100 078 034 Id Quiz1 Quiz2 Quiz3 Quiz4 Exam
2134 090 036 090 077 030 1234 52 7 100 78 34
3124 100 045 020 090 070 2134 90 36 90 77 30
4532 011 017 081 032 077 3124 100 45 20 90 70
5678 020 012 045 078 034 4532 11 17 81 32 77
6134 034 080 055 078 045 :
7874 060 100 056 078 078 :
8026 070 010 066 078 056 :
9893 034 009 077 078 020 :
1947 045 040 088 078 055 :
2877 055 050 099 078 080 :
3189 082 080 100 078 077 Highest
4602 089 050 091 078 060 Lowest
5405 011 011 000 078 010 Average
6999 000 098 089 078 020

What I need know is how I can get The highest and the lowest and The average of each. How can I add quiz1 in 1st student to quiz1 in 2nd student ? How can I compare them?
If your text file would look something like this
Quote ...
1234 052 007 100 078 034
2134 090 036 090 077 030
3124 100 045 020 090 070
4532 011 017 081 032 077
5678 020 012 045 078 034
6134 034 080 055 078 045
7874 060 100 056 078 078
8026 070 010 066 078 056
9893 034 009 077 078 020
1947 045 040 088 078 055
2877 055 050 099 078 080
3189 082 080 100 078 077
4602 089 050 091 078 060
5405 011 011 000 078 010
6999 000 098 089 078 020
then you could read it into a list or array of structures with
the fields of Id Quiz1 Quiz2 Quiz3 Quiz4 Exam
using fscanf() or whatever ...
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 2nd, 2005
0

Re: How can I get Highest, Lowest and Average ?

>But my queestion was little bit unclear.
Your question was clear enough, and the answer is the same. If it turns out that your question is actually how to read a text file so that you can easily store the results of each quiz in a list, that's a different story.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2005
0

Re: How can I get Highest, Lowest and Average ?

I got it done well!! Thank you all for trying to help me out !! I can't thank you guys enough.

You guys made me figure out that I had to study more about programming.
I will see you around!!

Thanks again.

Tenoran.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tenoran is offline Offline
9 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how do I do this? 1 = ' is on the x-axis'; (C++)
Next Thread in C++ Forum Timeline: c++ SQLServer ODBC errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC