Solved

# reads in a list of test scores and outputs the highest score, lowest score and average

Posted on 2003-03-22
Medium Priority
367 Views
i already have preliminaries for this. my program is working but the highest score does not appear and when i placed higher scores such as 50, 20...the output only appears the average
0
Question by:frostymiel
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4
• 2

LVL 6

Expert Comment

ID: 8186975
You need to post the code...
0

Author Comment

ID: 8189215
here are actually the samples of my code.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main()

{
int test1, test2, test3, test4, test5;
float ave;

clrscr();

printf("\nComplete the following scores of students.");
printf("\nPlease enter the score in Test 1: ");
scanf("%d", &test1);
printf("Please enter the score in Test 1: ");
scanf("%d", &test2);
printf("Please enter the score in Test 3: ");
scanf("%d", &test3);
printf("Please enter the score in Test 4: ");
scanf("%d", &test4);
printf("Please enter the score in Test 5: ");
scanf("%d", &test5);

if ((test1>=test2)&&(test1>=test3))
if ((test1>=test4)&&(test1>=test5))

printf("\nHighest score: %d", &test1);

if ((test1<=test2)&&(test1<=test3))
if ((test1<=test4)&&(test1<=test5))

printf("\nLowest score:  %d", test1);

ave = (test1 + test2 + test3 + test4 + test5)/5;

printf("\nAverage: %.2f", ave);

getch();

}
0

LVL 6

Expert Comment

ID: 8189680
The way you've constructed your 'if's, you will only print the highest score if test1 is the highest.  You will only print the lowest score if test1 is the lowest.

Your average will not be correct (it will round down and never print decimals because it will do integer division.

Here's a modified program that will do what I think you want...

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int compare( const void *arg1, const void *arg2 )
{
/* Compare integers: */
if( *(int*)arg1 < *(int*)arg2) return (-1);
if( *(int*)arg1 > *(int*)arg2) return (1);
return(0);
}

int main()
{
int testArray[5];
float ave;

clrscr();
printf("\nComplete the following scores of students.");
printf("\nPlease enter the score in Test 1: ");
scanf("%d", &testArray[1]);
printf("Please enter the score in Test 2: ");
scanf("%d", &testArray[2]);
printf("Please enter the score in Test 3: ");
scanf("%d", &testArray[3]);
printf("Please enter the score in Test 4: ");
scanf("%d", &testArray[4]);
printf("Please enter the score in Test 5: ");
scanf("%d", &testArray[5]);
qsort(testArray,5,sizeof(int),compare);

printf("\nHighest score: %d", testArray[4]);
printf("\nLowest score:  %d", testArray[0]);

ave = (float)(testArray[1] + testArray[2] + testArray[3] + testArray[4] + testArray[5])/5;/*cast to float to get true average*/

printf("\nAverage: %.2f", ave);

getch();
}
0

LVL 30

Expert Comment

ID: 8193218
>> The way you've constructed your 'if's, you will only print the highest score if test1 is the highest.  You will only print the lowest score if test1 is the lowest

Exactly. So if you need to find the lowest and highest values, then try using two additional variables:

(and I'd prefer using an array rather than defining 5 variables test1, test2, ....)

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main ()
{
int test[5], min, max, i, sum = 0 ;
float ave ;

clrscr () ;
printf ( "\n Complete the following scores of the students. " ) ;

for ( i = 0 ; i < 5 ; i ++ )
{
printf ( "\n Please enter the score in Test %d: ", i + 1 ) ;
scanf ( "%d", &test[i] ) ;
sum += test[i] ;

if ( i == 0 )
min = max = test[0] ; // end if

else
{
if ( test[i] < min )
min = test[i] ; // end nested if

if ( test[i] > max )
max = test[i] ; // end nested if

} // end else

} // end for

ave = ((float) sum)/ 5 ;
printf ( "\n Average: %.2f", ave ) ;
printf ( "\n Maximum value: %d \n Minimum value: %d", max, min ) ;
getch();

} // end of main ()

That'll do it!

Mayank.
0

Author Comment

ID: 8194503
To: qj62
I tried your suggested format. However, i encountered another problem in the output. When i tried to enter the following numbers:
Input:    Output:
1         Highest score: 12802
2         Lowest score: 1
3         Average: 2563.20
4
5
Again, when i tried to enter another set of numbers, such as:
Input: 7,6,5,4,3
Output:
Highest score: 12802
Lowest score: 4
Average: 256
This is syntax and logical error, right?

frostymiel
0

LVL 30

Expert Comment

ID: 8194514
frostymiel,

Did you try my code above? It should solve your problem.

Mayank.
0

Author Comment

ID: 8194535
mayankeagle,
Sure! I'll try you suggested format.
Actually, am encoding it now.
Wait for my next comment. Maybe after a few minutes. OK?

frostymiel
0

LVL 30

Accepted Solution

Mayank S earned 120 total points
ID: 8194553
Waiting..
0

Author Comment

ID: 8194605
mayankeagle,

You did well! Now my program is running!
You get 30 points for your effort.

frostymiel
0

Author Comment

ID: 8194645
mayankeagle,

hope we can be of partners soon! i mean in exchanging ideas related to information technology, programming, software engineering or anything under the umbrella of computers.
Thanks again!

frostymiel
0

LVL 30

Expert Comment

ID: 8194800

Mayank.

``````Edited by _alias99 -- 16-Dec-09
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month8 days, 8 hours left to enroll