?
Solved

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

Posted on 2003-03-22
11
Medium Priority
?
367 Views
Last Modified: 2008-02-01
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
Comment
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
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

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

Author Comment

by:frostymiel
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

by:gj62
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
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

by:frostymiel
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?
Thanks for your effort.

frostymiel
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194514
frostymiel,

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

Mayank.
0
 

Author Comment

by:frostymiel
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

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

Author Comment

by:frostymiel
ID: 8194605
mayankeagle,

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

frostymiel
0
 

Author Comment

by:frostymiel
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

by:Mayank S
ID: 8194800
Sure.... dontspamme"at"please.com

Mayank.

Edited by _alias99 -- 16-Dec-09

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question