15,400,433 members
1.00/5 (1 vote)
See more:
Hi... So I have a problem that i'm unsure how to fix...The Output for average show the wrong answer...It seems that It does not add the last digit...for example student 5: 60 - does not add to the sum and give a average....
The other problem consist of the lowest Mark always displaying 0; I'm unsure how to fix it?....
Is there any way to make this less and more compact and simple to understand?..

OutPut:
```Please enter mark for student 1
20
Please enter mark for student 2
20
Please enter mark for student 3
40
Please enter mark for student 4
50
Please enter mark for student 5
60

0 - 29
* * *

30 - 39

40 - 69
* *

70 - 100

highest Mark = 50

lowest Mark = 0

Average Mark = 26

Number of passed Students(s) = 2

Number of failed Students(s) = 3```

What I have tried:

```#include <string>
#include <iostream>
using namespace std;

int main()
{
int student = 0;
int student_grade = 0;
int totalStudent;

int starcounterlow = 0;
int starcountermidlow = 0;
int starcountermidhigh = 0;
int starcounterhigh = 0;

int num_pass_student;
int num_failed_student;

int i = 0;

int L = 0;
int Lm = 0;
int Hm = 0;
int H = 0;

int highmark;
int lowmark;

double avgmark;
double mark_sum;

highmark = 0;
lowmark = 100;
mark_sum = 0;

for (int i = 0; i < 5; i++)
{
mark_sum = mark_sum + student_grade;

if (student_grade > highmark && student_grade <= 100)

if (student_grade < lowmark && student_grade <= 100)

if ((student_grade >= 0) && (student_grade <= 29)) {
student++;
L++;
cout << "Please enter mark for student " << student << endl;
}

else if ((student_grade >= 30) && (student_grade <= 39)) {
student++;
Lm++;
cout << "Please enter mark for student " << student << endl;
}

else if ((student_grade >= 40) && (student_grade <= 69)) {
student++;
Hm++;
cout << "Please enter mark for student " << student << endl;
}

else if ((student_grade >= 70) && (student_grade <= 100)) {
student++;
H++;
cout << "Please enter mark for student " << student << endl;
}

}

cout << endl << endl;
cout << "0 - 29 " << endl;
for (starcounterlow = 1; starcounterlow <= L; starcounterlow++) {
cout << "* ";
}
cout << endl << endl;
cout << "30 - 39 " << endl;
for (starcountermidlow = 1; starcountermidlow <= Lm; starcountermidlow++) {
cout << "* ";
}
cout << endl << endl;
cout << "40 - 69 " << endl;
for (starcountermidhigh = 1; starcountermidhigh <= Hm; starcountermidhigh++) {
cout << "* ";
}
cout << endl << endl;
cout << "70 - 100 " << endl;
for (starcounterhigh = 1; starcounterhigh <= H; starcounterhigh++) {
cout << "* ";
}
cout << "\n";

totalStudent = L + Lm + Hm + H;
cout << endl;

cout << "highest Mark = " << highmark << endl;
cout << endl;

cout << "lowest Mark = " << lowmark << endl;
cout << endl;

avgmark = mark_sum / totalStudent;
cout << "Average Mark = " << avgmark << endl << endl;

num_pass_student = Hm + H;
cout << "Number of passed Students(s) = " << num_pass_student << endl << endl;

num_failed_student = Lm + L;
cout << "Number of failed Students(s) = " << num_failed_student << endl << endl;

return 0;
}```
Posted
Updated 17-Nov-21 15:42pm
v2
jeron1 17-Nov-21 17:00pm

I've not read the whole thing but you can try moving,

mark_sum = mark_sum + student_grade;

if (student_grade > highmark && student_grade <= 100)

if (student_grade < lowmark && student_grade <= 100)

to the end of your for loop. In addition, do your grade decisions after the data is input.

Solution 1

Here is a simpler piece of code to implement your program and I find it considerably easier to understand. Primarily because it uses several loops instead of explicit code.
C++
```int main()
{
const int binCount  = 5;
const int binLimits[ binCount ] = { 19, 39, 59, 79, 100 };
int markBins[ binCount ] = { 0 };

int inputCount = 0;

// first value is number of student marks

std::cout << "Enter number of students : ";
std::cin >> inputCount;

if( inputCount < 1 )
return 0;

// allocate an array to hold the marks

int * marks = new int[ inputCount ];

// acquire all student marks

int mark;
for( int n = 0; n < inputCount; ++n )
{
std::cout << "\nPlease enter mark for student " << n << " : ";
std::cin >> mark;

if( ( mark < 0 ) || ( mark > 100 ) )
{
std::cout << "Error - marks may range from 0 to 100" << std::endl;
return 0;
}

marks[ n ] = mark;
}

// process the data

int highMark = -1;
int lowMark = 1000;

double sumMarks = 0;

for( int i = 0; i < inputCount; ++i )
{
// compute sum and evaluate limits

sumMarks += marks[ i ];

if( highMark < marks[ i ] )
highMark = marks[ i ];
if( lowMark > marks[ i ] )
lowMark = marks[ i ];

for( int n = 0; n < binCount; ++n )
{
if( mark <= limits[ n ] )
{
++bins[ n ];
break;
}
}
}

// output counts in the bins

std::cout << std::endl << std::endl;

int lastMin = 0;
for( int n = 0; n < binCount; ++n )
{
std::cout << lastMin << " - " << binLimits[ n ] << std::endl;

for( int m = 0; m < markBins[ n ]; ++m )
std::cout << " *";

std::cout << std::endl;

lastMin = binLimits[ n ] + 1;
}

std::cout << std::endl;

std::cout << "highest Mark = " << highMark << std::endl;
std::cout << std::endl;

std::cout << "lowest Mark  = " << lowMark << std::endl;
std::cout << std::endl;

double avgmark = sumMarks / (double) inputCount;
std::cout << "Average Mark = " << avgmark << std::endl;

delete [] marks;

return 0;
}```
Here is the output from a file of 50 random values :
```0 - 19
* * *
20 - 39
* * * * * * * * * * *
40 - 59
* * * * * * *
60 - 79
* * * * * * * * * * * * * * * *
80 - 100
* * * * * * * * * * * * *

highest Mark = 98

lowest Mark  = 5

Average Mark = 59.62```
and here is the file used :
```50
26
60
50
71
5
96
86
54
47
64
36
79
98
62
88
58
38
33
25
46
21
31
23
35
60
40
82
84
10
98
93
65
76
79
69
28
84
61
17
97
61
75
71
92
53
92
37
88
68
69```
Note that this does not solve your problem exactly because I don't want to just give you the answer. You will have to adjust this code to do what you want. The point of homework and quizzes is to learn something and you won't if someone does all the work for you. Hopefully this gives you some different ideas on how to approach this problem.

One of the key things I mentioned earlier was the use of loops and arrays. Explicit code and data are rarely the best way to go when they are both repeated, as yours is. When you can organize your code into loops and arrays (and functions) it makes it much easier to extend and change things. In this case, the bin count and limits are different than yours and it is very, very easy to change them to match yours. That is something YOU will have to do. I also omitted the count of passes and failures and you will need to add those also. Here's a hint : add them in the section with the comment "process the data."
Rick York 17-Nov-21 21:45pm

BTW - this same code will accept input from the command line or by reading the file. I use the input redirector to have the program read the file. I did this by typing "ConsoleProgram < InputData.txt" in a command window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 88 Richard MacCutchan 65 Richard Deeming 20 CHill60 20 Patrice T 10
 OriginalGriff 2,685 Richard MacCutchan 1,355 Richard Deeming 580 Patrice T 354 0x01AA 345

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900