8 Replies - 465 Views - Last Post: 21 February 2012 - 01:06 PM Rate Topic: -----

#1 derk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 12

C grading question

Posted 19 February 2012 - 07:16 PM

coude some one please tell me what I am doing wrong I have to input 5 assignments, 2 tests and 1 exam . worth 20% , 40%, and 40% respectively. Also we can not have input greater than 100 or less than 0. my code is below but it does not output corectly. thank you

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


int main()
{

/* Set variable*/

    float a1;
    float a2,a3,a4,a5;
    float t1,t2;
  
    float e1;
    float a1good,a2good,a3good,a4good,a5good;
    float t1good,t2good;
    float e1good;
    
    float allgooda;
    float allgoodt;
    float allgoode;
    float allgood;
    float score1;
    float score2;
    float score3;
    float grade;
    
    
    
   
/* Input Assignment scores*/    
    printf("Input your Asignment 1 score:");
    scanf("%d", &a1);
        
    printf("Input Asignment 2 score:");
    scanf("%d", &a2);
        
    printf("Input Asignment 3 score:");
    scanf("%d", &a3);
        
    printf("Input Asignment 4 score:");
    scanf("%d", &a4);
       
    printf("Input Asignment 5 score:");
    scanf("%d", &a5);
    
   
/* Input Tests scores*/
    
    printf("Input your Test 1 score:");
    scanf("%d", &t1);
      
    printf("Input your Test 2 score:");
    scanf("%d", &t2);
    
    
        
/* Input Exam Scores*/  

    printf("Input your Exam score:");
    scanf("%d", &e1);
    
     
/* decide if scores are good */

    a1good = a1 >= 0 && a1 <=100;
    a2good = a2 >= 0 && a2<=100;
    a3good = a3 >= 0 && a3<=100;
    a4good = a4 >= 0 && a4<=100;
    a5good = a5 >= 0 && a5<=100;
    
    t1good = t1 >= 0 && t1<=100;
    t2good = t2 >= 0 && t2<=100;
    
    e1good = e1 >= 0 && e1<=100;

    allgood = a1good + a2good + a3good + a4good + a5good;
    allgood = allgood + t1good + t2good;
    allgood = allgood + e1good; 

    
if(allgood);
{
    score1 = (a1 + a2 + a3 + a4 + a5)/5 * .2;
    score2 = (t1 + t2)/2 * .4;
    score3 = (e1 * .4);
    
    grade = score1 + score2 + score3;
    
    if (grade >= 85)
        printf("A\n");


    else if (73 <= grade && grade <= 84)
         printf("B\n");

    else if (61 <= grade && grade <=72)
         printf("C\n");

    else if(grade >= 49 && grade <=60)
         printf("D\n");

    if (grade <= 48)
         printf("F\n");
}
     
   

system("pause\n");
return 0;

}


edit by ishkabible: please use code tags when posting code. like so, :code: thank you :)

This post has been edited by ishkabible: 19 February 2012 - 08:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C grading question

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: C grading question

Posted 19 February 2012 - 08:41 PM

Hi, welcome to DIC. In the scanf function %d is used for integers.

    scanf("%d", &a1);



The format specifier %f is used for floats.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: C grading question

Posted 19 February 2012 - 09:11 PM

Normally integers would be used as boolean variables in C.

066	    a1good = a1 >= 0 && a1 <=100;




Since the following are boolean variables you will want to use boolean operators rather than arithmetic operators. The results will differ.

077	    allgood = a1good + a2good + a3good + a4good + a5good;
078	    allgood = allgood + t1good + t2good;
079	    allgood = allgood + e1good;



Is everything correct here?

082	if(allgood);
083	{


Was This Post Helpful? 0
  • +
  • -

#4 derk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 12

Re: C grading question

Posted 19 February 2012 - 09:16 PM

Thanks for your responses I cleaned up the areas that were pointed out to me and it is workign correctly except my output is still wrong it does not associate my inputs with a letter grade liek I am looking for... where am i goign wrong in the final step?

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


/*  Dan Erklauer 
COSSC 1304 Homework 1
Write a C program to input an integer from the user, compute the number cubed, to the 5th, and 6th power using only multiplication and assignment statements, then output all three results.  The correct program only required a total of 4 multiplications.
*/

int main()
{

/* Set variable*/

    float a1;
    float a2,a3,a4,a5;
    
    float t1,t2;
  
    float e1;
    
    float a1good,a2good,a3good,a4good,a5good;
    float t1good,t2good;
    float e1good;
    
    float allgooda;
    float allgoodt;
    float allgoode;
    float allgood;
    float score1,score2,score3,grade;
   
    int score; 
    
    
    
   
/* Input Assignment scores*/    
    printf("Input your Asignment 1 score:");
    scanf("%f", &a1);
        
    printf("Input Asignment 2 score:");
    scanf("%f", &a2);
        
    printf("Input Asignment 3 score:");
    scanf("%f", &a3);
        
    printf("Input Asignment 4 score:");
    scanf("%f", &a4);
       
    printf("Input Asignment 5 score:");
    scanf("%f", &a5);
    
   
/* Input Tests scores*/
    
    printf("Input your Test 1 score:");
    scanf("%f", &t1);
      
    printf("Input your Test 2 score:");
    scanf("%f", &t2);
    
    
        
/* Input Exam Scores*/  

    printf("Input your Exam score:");
    scanf("%f", &e1);
    
     
/* decide if scores are good  */ 

    a1good = a1 >= 0 && a1 <=100;
    a2good = a2 >= 0 && a2<=100;
    a3good = a3 >= 0 && a3<=100;
    a4good = a4 >= 0 && a4<=100;
    a5good = a5 >= 0 && a5<=100;
    
    t1good = t1 >= 0 && t1<=100;
    t2good = t2 >= 0 && t2<=100;
    
    e1good = e1 >= 0 && e1<=100;

    allgood = a1good && a2good && a3good && a4good && a5good;
    allgood = allgood && t1good && t2good;
    allgood = allgood && e1good;
    

 

    score1 = (a1 + a2 + a3 + a4 + a5)/5 * .2;
    
    score2 = (t1 + t2)/2 * .4;
   
    score3 = (e1 * .4);
    
    grade = score1 + score2 + score3;
 
    
if(allgood)
    
    if (grade >= 85)
{
        score = 'A';
}

    else if (grade >= 73 && grade <= 84)
{
         score = 'B';
}
 
       else if (grade >= 61 && grade <=72)
{
         score = 'C';
}

    else if(grade >= 49 && grade <=60)
{
         score = 'D';
}
    if (grade = 48)
{
         score = 'F';
}



printf("Your Grade is= %d\n", &score);
   

system("pause\n");
return 0;

}


This post has been edited by Salem_c: 19 February 2012 - 10:16 PM
Reason for edit:: still failing on code tags

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: C grading question

Posted 19 February 2012 - 10:03 PM

Is this correct?

if (grade = 48) /* <-assign or compare */
{
  score = 'F';
}




You don't need to use the ampersand here. Also you are assigning characters to score. What type of data do you want to print? Look at printf.

printf("Your Grade is= %d\n", &score);




You can check your program and logic by printing intermediate values and information messages.

  allgood = allgood && e1good;

  if(allgood)
    puts("All good!");


Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1657
  • View blog
  • Posts: 3,136
  • Joined: 30-May 10

Re: C grading question

Posted 19 February 2012 - 10:20 PM

Using a compiler that can give you lots of help would also be beneficial.
$ gcc -W -Wall -Wextra bar.c
bar.c: In function ‘main’:
bar.c:99: warning: suggest explicit braces to avoid ambiguous ‘else’
bar.c:120: warning: suggest parentheses around assignment used as truth value
bar.c:127: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
bar.c:28: warning: unused variable ‘allgoode’
bar.c:27: warning: unused variable ‘allgoodt’
bar.c:26: warning: unused variable ‘allgooda’


Two of these warnings have already been pointed out by #define
Was This Post Helpful? 0
  • +
  • -

#7 derk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 12

Re: C grading question

Posted 20 February 2012 - 11:29 AM

Could some one please explain what am I doing wrong with my boolean? everything works except you can still insert unreal scores....
#include<stdio.h>
#include<stdlib.h>




int main()
{

/* Set variable*/

    float a1;
    float a2,a3,a4,a5;
    
    float t1,t2;
  
    float e1;
    
    float a1good,a2good,a3good,a4good,a5good;
    float t1good,t2good;
    float e1good;
    
    bool allgooda;
    bool allgoodt;
    bool allgoode;
    bool allgood;
    
    float score1,score2,score3,grade;
   
    char score; 
    
    
    
   
/* Input Assignment scores*/    
    printf("Input your Asignment 1 score:");
    scanf("%f", &a1);
        
    printf("Input Asignment 2 score:");
    scanf("%f", &a2);
        
    printf("Input Asignment 3 score:");
    scanf("%f", &a3);
        
    printf("Input Asignment 4 score:");
    scanf("%f", &a4);
       
    printf("Input Asignment 5 score:");
    scanf("%f", &a5);
    
   
/* Input Tests scores*/
    
    printf("Input your Test 1 score:");
    scanf("%f", &t1);
      
    printf("Input your Test 2 score:");
    scanf("%f", &t2);
    
    
        
/* Input Exam Scores*/  

    printf("Input your Exam score:");
    scanf("%f", &e1);
    
     
/* decide if scores are good  */ 

    a1good = a1 >= 0 && a1 <=100;
    a2good = a2 >= 0 && a2<=100;
    a3good = a3 >= 0 && a3<=100;
    a4good = a4 >= 0 && a4<=100;
    a5good = a5 >= 0 && a5<=100;
    
    t1good = t1 >= 0 && t1<=100;
    t2good = t2 >= 0 && t2<=100;
    
    e1good = e1 >= 0 && e1<=100;

    allgood = a1good && a2good && a3good && a4good && a5good;
    allgood = allgood && t1good && t2good;
    allgood = allgood && e1good;
    
    
    
        

    score1 = (a1 + a2 + a3 + a4 + a5)/5 * .2;
    
    score2 = (t1 + t2)/2 * .4;
   
    score3 = (e1 * .4);
    
    grade = score1 + score2 + score3;
 
    
if(allgood)
    
    if (grade >= 85)
        score = 'A';


    else if (grade >= 73 && grade <= 84)
         score = 'B';

 
       else if (grade >= 61 && grade <=73)
         score = 'C';


    else if(grade >= 49 && grade <=61)
         score = 'D';

    if (grade <= 49)
         score = 'F';




printf("Your Grade is = %c", score);
   

system("pause\n");
return 0;

}



Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C grading question

Posted 20 February 2012 - 12:10 PM

Yeah, we could, but I'm guessing the entire concept behind your assignment is around if statements and boolean expressions, so perhaps you should go back to your notes and book and re-read.
Was This Post Helpful? 0
  • +
  • -

#9 derk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 12

Re: C grading question

Posted 21 February 2012 - 01:06 PM

View PostJackOfAllTrades, on 20 February 2012 - 12:10 PM, said:

Yeah, we could, but I'm guessing the entire concept behind your assignment is around if statements and boolean expressions, so perhaps you should go back to your notes and book and re-read.



That comment is as useful as my text book...thanks to the individuals who gave input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1