Home > Mobile >  How an I output the minimum grade(and the number o ocurences ) in a do while expression
How an I output the minimum grade(and the number o ocurences ) in a do while expression

Time:11-07

I am doing the following practice exercise: Design a program that reads, from the standard input (keyboard), a sequence of three or more integer values ​​ending with a negative value, which represent the grades of the students in a class and determine the following values: The linear rounded mean of the entire sequence of values. The highest score and having several equal to the highest indicate the total number of occurrences The lowest grade and having several equal to the lowest indicate the total number of occurrences The centered mean rounded off by taking out all high-value notes and all minor notes

Heres what I did so far:

#include<stdio.h>
#include<math.h>
int main(void)
{
int grade,counter=0, sum=0, median , centeredmedian,maximumgrade=0,minimumgrade=0;
printf("Input three or more integer values ending with a negative value.\n");
do
{
scanf("%d",&grade);
if(grade>=0 && grade<20)
{
    

counter=counter 1;
sum=sum   grade;
//maximum and minimum grade
if(grade>maximumgrade)
{
    maximumgrade=grade;
        }
        if(grade>=0 && grade<minimumgrade)
{
    minimumgrade=grade;
}
        
}
}
while(grade >= 0);
if(counter < 3)
{
printf("erro. sequencia invalida\n ");
return 0;
}
median= round(sum/counter);
printf("the linear median is %d values", median);
printf("The maximum grade is %d ",maximumgrade);
printf("The minimum grade is %d ",minimumgrade);

}

But ater inputing 3 or more integers It gives the corret meant and maximumgrade but the wrong minimum valueenter image description here

Could you help me figure this out and additionaly give me sugestions on the next step( indicate the total number of occurrences o the maximum and minimum grade).

Your time is greatly appreciated. thank you.

CodePudding user response:

the problematic part is: grade<minimumgrade. At first it checks if grade<0 and this is never true so it thinks the minimum grade is always 0. You should give max integer value for the initial minimum grade i.e. minimumgrade = 2147483647

CodePudding user response:

First of all, please properly indent your code in order to make it readable. In the current state, the reader is forced to explicitly search for the closing brackets to find where the control structures begin and end.

if(grade>=0 && grade<20)
{
    

counter=counter 1;
sum=sum   grade;
//maximum and minimum grade
if(grade>maximumgrade)
{
    maximumgrade=grade;
        }
        if(grade>=0 && grade<minimumgrade)
{
    minimumgrade=grade;
}
        
}

would be far more readable if written as :

if(grade>=0 && grade<20) {
    counter  ;
    sum  = grade;
    //maximum and minimum grade
    if(grade > maximumgrade) {
        maximumgrade = grade;
    }
    if(grade >= 0 && grade < minimumgrade) {
        minimumgrade=grade;
    }     
}

It's not only for us, but also for you. A well formated code allow for more efficient debugging.

For your problem, it's absolutely simple : you initialize the min grade already at the lowest value possible. (first line of the main : minimumgrade=0). the value of minimumgrade change only when the user input a value which between 0 and minimumgrade : never. Just initialize minimumgrade with the highest value possible for a grade (here : 20) and the problem is resolved.

for your problem of counting the number of min and max grade, it's very simple. Just create two new variables, count_min and count_max, with 0 as starting value.

Then, in your loop, write :

if(grade>=0 && grade<20) {
    counter  ;
    sum  = grade;
    if(grade > maximumgrade) {
        // new max value
        maximumgrade=grade;
        count_max = 1;
    } else if (grade == maximumgrade) {
        // max value previously encountered
        count_max  ;
    }
    if(grade >= 0 && grade < minimumgrade) {
        minimumgrade=grade;
        count_min = 1;
    } else if (grade == minimumgrade) {
        count_min  ;
    }    
}
  •  Tags:  
  • c
  • Related