8 Replies - 19620 Views - Last Post: 18 January 2009 - 03:48 PM Rate Topic: -----

#1 jkaiser18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
printf("Test score: %.f Grade: A\n",c);
else if(c>=80)
printf("Test score: %.f Grade: B\n",c);
else if(c>=70)
printf("Test score: %.f Grade: C\n",c);
else if(c>=60)
printf("Test score: %.f Grade: D\n",c);
else if(c<=50)
printf("Test score: %.f Grade: F\n",c);
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   User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • 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)
printf("Test score: %.f Grade: A\n",c);
else if(c>=80)
printf("Test score: %.f Grade: B\n",c);
else if(c>=70)
printf("Test score: %.f Grade: C\n",c);
else if(c>=60)
printf("Test score: %.f Grade: D\n",c);
else if(c<=50)
printf("Test score: %.f Grade: F\n",c);
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;
}

Was This Post Helpful? 0
  • +
  • -

#3 jkaiser18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ^_^
Was This Post Helpful? 0
  • +
  • -

#4 jkaiser18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
printf("Test score: %.f Grade: A\n",c);
else if(c>=80)
printf("Test score: %.f Grade: B\n",c);
else if(c>=70)
printf("Test score: %.f Grade: C\n",c);
else if(c>=60)
printf("Test score: %.f Grade: D\n",c);
else if(c>=50 || c<=50)
printf("Test score: %.f Grade: F\n",c);
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
Test score: 88 Grade: B

Enter test score for student No.2: 90
Test score: 90 Grade: A

Enter test score for student No.3: 78
Test score: 78 Grade: C

The Average of the 3 test score(s) is: 85.33
The lowest test score is: -107374176
The highest test score is: 90
Was This Post Helpful? 0
  • +
  • -

#5 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • 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;

Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • 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 */
    /* 
        Start with 'low' higher than any possible 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;
}

Was This Post Helpful? 0
  • +
  • -

#8 jkaiser18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#9 David W   User is offline

  • DIC supporter
  • member icon

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

Re: How to determine the highest/lowest score

Posted 18 January 2009 - 03:48 PM

View Postjkaiser18, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1