tags:

views:

604

answers:

7
+1  Q: 

C language problem

Hi guys I want to create a program that requests from the user 10 grades and then filters them to pass and fail, then prints the number of passes and fails. I did the program but the output is wrong.

int pass,fail,grade,studentcounter;

pass=0;
fail=0;
grade=0;
studentcounter=10;

while (studentcounter!=0)
{

    printf("enter the next grade\n");
    scanf("%d",grade);
    student--;
}

switch (grade)
{
case 1:
    if (grade >= 50)
        pass++;
    break;
case 2:
    if (grade <= 49)
        fail++;
    break;
}

}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);

}

The problem is that the program request the ten grades but at the end it will print the number of fail and the number of pass as zero. Why?

A: 

you need to keep the count of pass/fails inside the while loop, the grade variable will be overwritten at every input.

edit: also, dont use a switch statement.

John Boker
+3  A: 

You are entering a grade number. A switch statement tests that grade against the cases, and i am pretty sure the grades are not 1 percent or 2 percent. An if statement would be a more logical choice in this situation.

Second of all, you have a code block which is never used. First you set studentcounter to zero, then you say "only execute this block when studentcounter is NOT zero"...

studentcounter=0;

while (studentcounter!=0) {

printf("enter the next grade\n");

scanf("%d",grade);

student--;

}

The third problem is, you misspelled grade.

you can rewrite your code as follows:

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

int main()
{
int pass,fail,grade,studentcounter;

pass=0;
fail=0;
grade=0;
studentcounter=0;

while (studentcounter < 10) {

printf("enter the next grade:\n");

scanf("%d",&grade);

if (grade >= 50) {
    pass++;
} else {
    fail++;
}

studentcounter++;
}


printf("the number of fail is: %d \n",fail);
printf("the number of pass is: %d \n",pass);
return 0;
}

Sorry if I overlooked something; I don't have time to throw it into my editor :P

cheers

John T
A switch will not work in this case (no pun intended).
Brian C. Lane
You don't need the 'else if'; just use 'else'!
Jonathan Leffler
You know, giving a student the answer won't help them learn to think.
Paul Tomblin
I am in a rush and wanted to help someone out, didn't see the tag homework I apologize. And yes the 'else if' is not needed but it isn't essentially wrong.
John T
A: 

Ok, the code as presented is a pile of crap. I will leave it as an exercise for you to sort out what I fixed for you:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int pass = 0;
    int fail = 0;
    int grade= 0;
    int studentcounter;

    for (studentcounter=1; studentcounter<=10; studentcounter++ )
    {
        printf("Enter grade for student #%-2d  :", studentcounter);
        scanf("%d",&grade);

        if(grade >=50)
            pass++;

        if(grade<=49)
            fail++;
    }
    printf("the number of fail is %d\n",fail);
    printf("the number of pass is %d\n",pass);
}

PS. If this is homework, please give credit when you turn it in. You can learn a lot by looking at other people's code, but never claim to create that which you haven't.

Brian C. Lane
Use 'else' instead of two tests.
Jonathan Leffler
It is polite to leave a comment if you downvote. There is nothing wrong with this solution.
Brian C. Lane
A: 

isn't there a way to keep using the switch function how to embedded the condition that say grade<=49 or grade>=50 into the cases ??

Not practically. A: switch is a statement, not a function. B: the only way to make switch work would be to list case 0: case 1: case 2: case 3: ... case 49: before the code that increments fail and list case 50: ... case 100: before the code that increments pass. A single if/else is much better!
Jonathan Leffler
thanx yes i understand
A: 

C 'case' statements do not work the way you are expecting.

They operate on 'constant-expressions' which means that in order to accomplish the task you have given, 'Determine if a grade is pass or fail' one would have to write a convoluted switch statement of the form

  switch ( grade )
  {
    case 100:
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
    case 94:
    case 93:
    case 92:
    case 91:
    case 90:
    case 89:
    case 88:
    case 87:
    case 86:
    case 85:
    case 84:
    case 83:
    case 82:
    case 81:
    case 80:
    case 79:
    case 78:
    case 77:
    case 76:
    case 75:
    case 74:
    case 73:
    case 72:
    case 71:
    case 70:
    case 69:
    case 68:
    case 67:
    case 66:
    case 65:
    case 64:
    case 63:
    case 62:
    case 61:
    case 60:
    case 59:
    case 58:
    case 57:
    case 56:
    case 55:
    case 54:
    case 53:
    case 52:
    case 51:
    case 50:
      pass++;    
      break;
    default:
      fail++;    
      break;
  }

This would probably give you the correct answer, however, you may receive a 'fail' on your homework for writing such a monster.

I would try to think of another problem that has 'while, switch and if' statements or perhaps you could modify your current program such that, instead of accepting 10 grades, it prompted the user if they wished to enter another grade and accepted only the answer 'Y', 'y', or 'N', 'n' thus 'switching' on that answer to continue.

A: 
A: 

hmm, since you have to use swicth i will suggest the following: Keep a variable which checks whether the grade is more than 50 or not and then use this variable inside switch:


char pass_fail = (grade >= 50)? y : n;
switch( pass_fail )
{
case 'y': 
     // print pass
     break;
case 'n':
    // print fail
    break;
}


hope this helps you

Raghu