# How to determine the highest/lowest score

Page 1 of 1

## 8 Replies - 19620 Views - Last Post: 18 January 2009 - 03:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=81295&amp;s=cb49f609a4cc7541e44919ee84dba985&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jkaiser18

Reputation: 0
• Posts: 8
• Joined: 16-January 09

# How to determine the highest/lowest score

Posted 16 January 2009 - 06:59 PM

Hey what is wrong with this code.
I need to know how to display the highest and lowest test score.
```#include<stdio.h>
int main()
{
float a,b,c,d=0,L,H;
printf("Enter number of students:");
scanf("%f",&b);
for (a=1; a<=b; a++)
{
printf("\nEnter test score for student No.%.f:",a);
scanf("%f",&c);
if(c>=90)
else if(c>=80)
else if(c>=70)
else if(c>=60)
else if(c<=50)
d=d+c;
}
printf("\nThe Average of the %.f score(s) is: %.2f\n",b,d/b);
if (c>b)
L=c;
else (c<b);
H=c;
printf("The lowest test score is: %.f\n",L);
printf("The highest test score is: %.f\n",H);
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: How to determine the highest/lowest score

### #2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: How to determine the highest/lowest score

Posted 16 January 2009 - 07:18 PM

Modified title to be more descriptive of the problem.

Move the IF statement inside your loop and replace the ELSE with an IF to the else portion of your statement. Basically break it into two separate statements. The reason you need it in the loop is you want to compare it to each score entered to find the highest/lowest. You also need to compare to 'L' and 'H', not 'a' and 'b'.

In addition, 'a' and 'b' need to be of type integer because you are using them in the loop.
```#include<stdio.h>
int main()
{
float c,d=0,L,H;
int a,b;
printf("Enter number of students:");
scanf("%f",&b);
for (a=1; a<=b; a++)
{
printf("\nEnter test score for student No.%.f:",a);
scanf("%f",&c);
if(c>=90)
else if(c>=80)
else if(c>=70)
else if(c>=60)
else if(c<=50)
d=d+c;

//check for lowest score
if (c<L)
L=c;

//check for highest score
if (c>H)
H=c;

}
printf("\nThe Average of the %.f score(s) is: %.2f\n",b,d/b);

printf("The lowest test score is: %.f\n",L);
printf("The highest test score is: %.f\n",H);
return 0;
}
```

### #3 jkaiser18

Reputation: 0
• Posts: 8
• Joined: 16-January 09

## Re: How to determine the highest/lowest score

Posted 16 January 2009 - 07:28 PM

thanks! I`m just starting to learn c programming. i appreciate your help

### #4 jkaiser18

Reputation: 0
• Posts: 8
• Joined: 16-January 09

## Re: How to determine the highest/lowest score

Posted 17 January 2009 - 04:45 AM

I have reconstructed the program and followed what you have said but,
there is one problem it cannot display the lowest score. I wonder why?

```#include<stdio.h>
int main()
{
int a,b;
float c,d=0,L,H;
printf("Enter number of students: ");
scanf("%d",&b);
for (a=1; a<=b; a++)
{
printf("\nEnter test score for student No.%d: ",a);
scanf("%f",&c);
if(c>=90)
else if(c>=80)
else if(c>=70)
else if(c>=60)
else if(c>=50 || c<=50)
d=d+c;
if (c<L)
L=c;
if (c>H)
H=c;
}
printf("\nThe Average of the %d test score(s) is: %.2f\n",b,d/b);
printf("The lowest test score is: %.f\n",L);
printf("The highest test score is: %.f\n",H);
return 0;

```

here is what the output shows:

Enter number of students:3

Enter test score for student No.1: 88

Enter test score for student No.2: 90

Enter test score for student No.3: 78

The Average of the 3 test score(s) is: 85.33
The lowest test score is: -107374176
The highest test score is: 90

### #5 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: How to determine the highest/lowest score

Posted 17 January 2009 - 06:15 AM

float c,d=0,L,H;

If you just assign L and H some values, it ought to solve your problem.

Assign L the maximum score and H the minimum score.

### #6 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: How to determine the highest/lowest score

Posted 17 January 2009 - 10:52 AM

As Gloin indicated the issue is that you have not assigned any values to L and H when you declared them. As a result, they are given whatever value is currently stored in that memory address.

Assign values when you declare them and that should solve the problem. The low will be anything below 100 and the high will be anything over 0.

```float c,d=0,L = 100,H = 0;
```

### #7 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: How to determine the highest/lowest score

Posted 17 January 2009 - 04:52 PM

You may like to look at this ... (to get started on crash proofing your integer inputs from the keyboard and some data validation)

Shalom,
David
http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html

```/*
C Safe data entry ... 'crash proofing' and 'data validation' ...

1. This program takes in the whole line entered from the keyboard,
up to a user specifed max number of characters, into a CString,
and 'eats up' any remaining char's on the line, including the '\n'
character at the end.

2. It avoids the 'unsafe' buffer overflow problem that exists with 'gets'

3. and it avoids the problem of flushing '\n' or other residual characters
left in 'stdin' by 'scanf'

4. Problems with errors with integer input are more readily handled by
inputing the data as a CString and then using atoi to convert that string
into an int ... This also avoids the problems mentioned in #3 above.

*/

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

typedef char * CString;

/* remember to free the allocated memory when done with the CString ... */
CString getLine( FILE *fp, int maxLen )
{
CString buffer = (CString) calloc( maxLen+1, sizeof(char) );
int i=0, c;
buffer[0] = 0;                  /* NULL terminate 'as we go' ... */
while( (c=fgetc(fp)) != '\n' )  /* eats up WHOLE line ... including '\n' */
{
if( i<maxLen )
{
buffer[i] = c;
buffer[i+1] = 0;        /* NULL terminate 'as we go' ... */
++i;
}
}
return realloc( buffer, i+1 );
}

const int confirmValue = 10; /* set this as you like ... */

int main()
{
int i, num; /* i is a counter, num is the number of students to be input */
/*
and 'high' the lowest value accepted  ...

or ...

take in the first value before you loop and assign this 'actual value',
before you begin your loop, to both of high and low.

*/
float low = 1000, high = 0;
float score, sum = 0;
CString str;
int valid = 0; /* 0 for false is used as the initial value */

do
{
printf( "Enter number of students > 0 : " );
str = getLine( stdin, 32 );
num = atoi( str ); /* returns 0 if non integer entered for first char's */
free( str );
if( num > confirmValue )
{
printf( "Do you really have %d students ... (y/n) ? ", num );
str = getLine( stdin, 1 );
if( str[0]=='y' || str[0]=='Y' ) valid = 1;
free( str );
}
else if( num > 0 ) valid = 1;

/* else ... not valid */

}while( !valid );

for( i = 1; i <= num; ++i )
{
printf( "\nEnter test score for student No. %d : ", i );
str = getLine( stdin, 32 );
score = atoi( str ); /* returns 0 if non integer entered for first char's */
free( str );

if( score > 100 )
{
printf( "Is score really %.f ... (y/n) ? ", score );
str = getLine( stdin, 1 );
if( !(str[0]=='y' || str[0]=='Y') )
{
--i; /* reset counter ...  */
free( str );
continue; /*** NOTE: go right now to top of for loop ... ***/
}
else printf( "Test score: %.f Grade : F\n", score );
free( str );
}
else if( score >= 90 )
printf( "Test score: %.f Grade : A\n", score );
else if(score >= 80 )
printf( "Test score: %.f Grade : B\n", score );
else if( score >= 70 )
printf( "Test score: %.f Grade : C\n", score );
else if( score >= 60 )
printf( "Test score: %.f Grade : D\n", score );
else if( score == 0 )
{
printf( "Is score really 0 ... (y/n) ? " );
str = getLine( stdin, 1 );
if( !(str[0]=='y' || str[0]=='Y') )
{
--i; /* reset counter ...  */
free( str );
continue; /*** NOTE: go right now to top of for loop ... ***/
}
else printf( "Test score: %.f Grade : F\n", score );
free( str );
}
else if(score < 0 )
{
printf( "A score of %.f is NOT a valid entry ...  ", score );
--i; /* reset counter ...  */
continue; /*** NOTE: go right now to top of for loop ... ***/
}
else
printf( "Test score: %.f Grade : F\n", score );

sum += score;

if( score < low ) /* update low and high ... */
low = score;
if( score > high )
high = score;
} /* end of for loop */

printf( "\nThe average of the %d test score(s) is: %.2f\n", num, sum/num );
printf( "The lowest test score is: %.f\n", low );
printf( "The highest test score is: %.f\n", high );

printf( "Press 'Enter' to continue ... " );
getchar();
return 0;
}
```

### #8 jkaiser18

Reputation: 0
• Posts: 8
• Joined: 16-January 09

## Re: How to determine the highest/lowest score

Posted 18 January 2009 - 02:06 AM

thanks I've finished it now thanks to you guys..

### #9 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: How to determine the highest/lowest score

Posted 18 January 2009 - 03:48 PM

jkaiser18, on 18 Jan, 2009 - 01:06 AM, said:

thanks I've finished it now thanks to you guys..

You are very welcome ... And ... Thank You for replying. And please don't forget to spread the 'good word' all around .. to all your other co-students.

You all might like to make a more solid foundation in computing ... by some exposure to some TOP user friendly assembly language ... Take a look at the famous professor Randy Hyde's HLA ... designed especially for students of C/C++ ... to make it facile to dive into assembly language programming for them ... and to thus get exposed to some of the many insights, into computers and computing, that one may NOT readily get ANY other way.

See this link for a very easy start ... (High School to intro College level.)

http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html

Shalom,
David