Jump to content

Another stupid question from a beginner

- - - - -

  • Please log in to reply
14 replies to this topic

#1
wangyoung1991

wangyoung1991

    Newbie

  • Members
  • PipPip
  • 19 posts
The program will ask the user to input percentages of each portion of the grade and then the program will calculate each portion and the total points. I gotta most of it.
Here is the program:


#include <stdio.h>

#define HW    35
#define LAB   60
#define MID   200
#define FINAL 150
#define LECTQ 50
#define LABQ  60

int main (void)
{
  float hw;
  float lab;
  float mid;
  float final;
  float lectq;
  float labq;
  float hwp;
  float labp;
  float midp;
  float finalp;
  float lectqp;
  float labqp;
  float hwpt;
  float labpt;
  float midpt;
  float finalpt;
  float lectqpt;
  float labqpt;
  float total;

  printf("Enter the percentage of points earned on homework assignments: ");
  scanf("%f", &hw);
  printf ("Enter the percentage of points earned on lab assignments: ");
  scanf("%f", &lab);
  printf("Enter the percentage of points earned on the midterms: ");
  scanf("%f", &mid);
  printf("Enter the percentage of points earned on the final exam: ");
  scanf("%f", &final);
  printf("Enter the percentage of points earned on the lecture quizzes: ");
  scanf("%f", &lectq);
  printf("Enter the percentage of points earned on the lab quizzes: ");
  scanf("%f", &labq);

  hwp    = hw * 100;
  labp   = lab * 100;
  midp   = mid * 100;
  finalp = final * 100;
  lectqp = lectq * 100;
  labqp  = labq * 100;

  hwpt    = HW * hw;
  labpt   = LAB * lab;
  midpt   = MID * mid;
  finalpt = FINAL * final;
  lectqpt = LECTQ * lectq;
  labqpt  = LABQ * labq;
  total   = hwpt + labpt + midpt + finalpt + lectqpt + labqpt;

  printf("\n============ Semester  Results ============\n");
  printf("Homework: %10.2f%% Points Earned: %6.2f\n", hwp, hwpt);
  printf("Labs: %14.2f%% Points Earned: %6.2f\n", labp, labpt);
  printf("Midterms: %10.2f%% Points Earned: %6.2f\n", midp, midpt);
  printf("Final Exam: %8.2f%% Points Earned: %6.2f\n", finalp, finalpt);
  printf("Lecture Quiz: %6.2f%% Points Earned: %6.2f\n", lectqp, lectqpt);
  printf("Lab Quiz: %10.2f%% Points Earned: %6.2f\n", labqp, labqpt);
  printf("===========================================\n");
  printf("====================== Total Points: %.2f\n", total);
[B]  [SIZE=4]Question!!![/SIZE][/B] //  printf("====================== Course Grade:
  return(0);
}
Notice the line I commented out right before return(0), I have no clue how to determine the letter grade. The boundaries are:

Grade Total Points
A 470
B 415
C 360
D 305

Our professor asks not to use any conditional statement like "if" "elseif". Otherwise, it will be a zero. Is there anything way I can determine the grade without using conditional statements?

Edited by Alexander, 26 January 2011 - 11:39 PM.
Added bb [code] tags


#2
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Use iostream. Do not define variables and also be aware that int * float results in truncation (I believe?).

// Use iostream

#include <iostream>


int main (void)

{

  const float HW_WEIGHT = 35.00;

  const float LAB_WEIGHT = 60.00;

  const float MID_WEIGHT = 200.00;

  const float FINAL_WEIGHT = 150.00;

  const float LECTO_WEIGHT = 50.00;

  const float LABO_WEIGHT = 60.00;


  float hw, lab, mid, final, lectq, labq;


  // do the rest of your **** here.

}


Edited by dargueta, 27 January 2011 - 02:22 PM.
Fixed code tags


#3
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
You can't use conditional statements, but can you use conditional expressions within a loop statement?

Here is what i came up with, you will have to tailor it to your needs.
source:
#include "stdio.h"


int main( int argc, char** argv )

{

	int total;

	printf("total: ");

	scanf("%d", &total );

	

	char letterGrade = 'A';

	int highestPossiblePoints = 525;

	int letterGradeVariation = 55;

	

	while(  (! (total >= ( highestPossiblePoints -= letterGradeVariation))) && (++letterGrade <= 'D') )

	{

		;

	}

	printf( "grade: %c %d\n", letterGrade, total );

	

	return 0;

}

I am going to assume the values don't match up. that the total "out of" number is 525 and the variance is 55, in that case the code above now works as expected.
.. except for the fact that if you are supposed to get a 'F' it really displays a 'E'... (because e comes after d in the alphabet..not f..)

to fix that i suggest creating a char array of the possible letter grades, 'a' 'b' 'c' 'd' and 'f'
then have a pointer that starts pointing at a. if the grade is dropped down you need to increment the pointer so it points to the next byte... a to b. b to c. c to d. and d to f.

thats the only way i can think of doing it without using an if or switch statement.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@sam_l,
if you think iostream library is for const(ants) then you are wrong.

@wangyoung1991,
can you use ternary operator?

char grade = ((total >= 470) ? 'A' : (total >= 415) ? 'B' : (total >= 360) ? 'C' : (total >= 305) ? 'D' : 'F');


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
wangyoung1991

wangyoung1991

    Newbie

  • Members
  • PipPip
  • 19 posts
@sam
Hmmm our program should not include <iostream> yet...

@skippy
We cant use while loop either... sorry

@flying dutchman
Not really, I can't because our prof said that we cant go beyond the content of first three chapters which havent introduced ternary operator yet. I heard that there is something to do with ASCII code; i will try to figure it out. And... you love half-life dont you??

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Well, A's ASCII code is 65, B's 66, C's 67 and so on. I can't figure how ASCII code and your grade points could be connected.

EDIT: Ofcourse, after I asked a friend he immediately gave me this formula:
grade = 'A' - (gradePoints - 470) / 55
I feel really stupid.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
wangyoung1991

wangyoung1991

    Newbie

  • Members
  • PipPip
  • 19 posts

Flying Dutchman said:

Well, A's ASCII code is 65, B's 66, C's 67 and so on. I can't figure how ASCII code and your grade points could be connected.

EDIT: Ofcourse, after I asked a friend he immediately gave me this formula:
grade = 'A' - (gradePoints - 470) / 55
I feel really stupid.

Sorry, can u explain the formula a little bit? what's "(gradepoints - 470) / 5 "??

#8
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
even if that last formula did work, you still have a problem with the 'F' letter grade being an E. and it requires a conditional statement of some sort to change an E to F
And what happens when the grade is 0?

0 - 470 = -470

-470/55 = -8

'A' - (-8) = 'I'

Don't feel stupid. Your friend still didn't have a working solution.

@OP
what is this assignment for, and what is the reason you can't use conditional logic?

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#9
wangyoung1991

wangyoung1991

    Newbie

  • Members
  • PipPip
  • 19 posts
Thank you for your concern skippy. Fortunately, we only have letter grades from A to D. =]

It's a lab assignment for my introductory CS class. It's a class which teaches "raw" beginners like me. The lectures so far only covered first three chapters of the book and our professor stated in the lab assignment that we cannot use anything beyond these three chapters. So...

#10
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Oh, I didnt see that, that makes things much simpler.. kinda.

Are you guaranteed that the grade won't drop below 305?

or is it supposed to be "if the grade is less than 305, the grade is a D"

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I somehow don't understand why some teachers/professors give assignments like this. Sure, you might learn a trick or two but to me that's like if driving instructor said you can drive but don't use gearbox.

On a more serious note, if you'd need F aswell, you could print E and said there's a glitch on the screen. :)

#12
wangyoung1991

wangyoung1991

    Newbie

  • Members
  • PipPip
  • 19 posts
Yes, if the total is lower that 305, its a D.

I have another problem here:
If I use that equation and then execute the program and inputting 99 percent for each portion of grade. For letter grade, i got an "@" instead of "A". Why is that??




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users