dimanche 9 octobre 2016

display highest/lowest and the avg of inputed marks - C programming

I'm writing a c program for my intro class and I'm stuck on the if/else statements. we have to output the highest,lowest grade entered as well as the avg for those. Marks entered must be between 0-100. this is my code so far, i can't seem to figure it out, thanks for any advice! :

#include <stdio.h>

int main (void)
{

  int numberofMarks;
  int i;
  int x;
  int y;

  int numofPasses=1;
  int numofFails=1;
  float sumpassedMarks =0;
  float sumfailedMarks=0;
  float markEntered=0;
  float highestMark=0;
  float lowestMark=0;
  float totalMark=0;
  float avgofMarks=0;
  float avgpassedMarks=0;
  float avgfailedMarks=0;



        printf ("       ---=== IPC mark Analyser ===---\n");
        printf ("Please enter the number of marks(between 3 and 40): ");
         scanf ("%d", &numberofMarks);


//asks user for number of marks, only takes 3-40, otherwise outputs error msg
        for (i=1 ; (numberofMarks < 3)||(numberofMarks > 40) ; i++) {
          printf ("Invalid number, enter a number between 3 and 40 inclusive: ");
          scanf ("%d", &numberofMarks);
        }

//for loop recieves the mark
        for (x=1 ;(x <= numberofMarks) ; x++) {
          printf ("%d> ", x);
          scanf ("%f", &markEntered);


//contd loop..loop makes sure mark entered is between 1-100
        for (y=1; (markEntered <0)||(markEntered>100); y++)
        {
            printf ("Error, Enter values between 0 and 100 incluisve.\n");
            printf ("%d> ", x);
             scanf ("%f", &markEntered);


            if (markEntered >= 50) {
              numofPasses=numofPasses+1;
              sumpassedMarks+=markEntered;
           }

            if else (markEntered <= 49) {
              numofFails = numofFails+1;
              sumfailedMarks+=markEntered;
           }

            if else (markEntered > highestMark) {
              highestMark = markEntered;
           }

            else (markEntered < lowestMark) {
              lowestMark = markEntered;
           }

        }
//adds the mark entered to all the marks entered for a overall sum
        totalMark = totalMark + markEntered;
        }

        avgofMarks = (float)totalMark / numberofMarks;
        avgpassedMarks = (float)sumpassedMarks / numberofMarks;
        avgfailedMarks = (float)sumfailedMarks / numberofMarks;


        printf ("Total of %d students passed with an average of %.1f.\n", numofPasses,avgpassedMarks);
        printf ("Total of %d students failed with an average of %.1f.\n", numofFails, avgfailedMarks);
        printf ("Highest mark in the group: %.1f\n", highestMark);
        printf ("lowest mark in the group: %.1f\n", lowestMark);
        printf ("The average of all marks in this group is %.1f.\n", avgofMarks);
        printf ("Program Ended.\n");

return 0;

}

The work asks,

[> >In the loop in which marks are being entered, after each entry examine the value of the mark entered: • If it is a pass, add one to

the number of passes and add the value of the mark to the sum of passed marks. • If it is a fail, add one to the number of fails and add the value of the mark to the sum of failed marks. • If the value is higher than the highest mark, set highest mark to the value read. • If the value is lower than the lowest mark, set the lowest mark to the value read. After all the marks are entered and examined, divide the sums by the corresponding number of marks to get the average and print the results:]1 [this is my output vs the sample output]2

Aucun commentaire:

Enregistrer un commentaire