Thread: Exam Question

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Exam Question

    I just took my first exam in my programming class. I'm new at this so bear with me. I had trouble with a this question about a program for calculating GPA:
    write a program that asks for letter grades (A,B,C,D, or F -- no +/-) from 5 classes, where A = 4 points, B = 3, C = 2, D = 1, and F = 0.

    What I am having trouble with is using the char data type for the grades, and then assigning integer values to those grades. It was the first exam, so we are only using if-else and switch statements, not while or for or increments or anything like that.

    I would greatly appreciate if someone could explain this to me, it's driving me crazy!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean like
    if ( grade == 'A' ) points = 4;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Quote Originally Posted by Salem View Post
    You mean like
    if ( grade == 'A' ) points = 4;
    so does that mean you would have to write if statements for grade1, grade2, grade3, etc, where they == A, B, C, etc...?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by dano9700 View Post
    so does that mean you would have to write if statements for grade1, grade2, grade3, etc, where they == A, B, C, etc...?
    Since you haven't been taught loops, arrays or functions, yes you would have to do that. But the approach to the problem can be different, for example:
    1) Lay out the different if statements, one for every possibility
    2) Have a single if for each grade, and check the value inside it by chaining the comparison with "&&",
    3) Take advantage of the fact that the alphabet is coded sequentially in ASCII, therefore you can check if each grade is between 'A' and 'F' inclusive. I'll let it as an exercise to you how to manipulate the value to obtain what you want. Hint: you can subtract two characters to get the distance between them
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam Question Help!
    By Ameteur hour in forum C Programming
    Replies: 6
    Last Post: 07-24-2014, 11:26 AM
  2. Exam question
    By Fahad Ch in forum C Programming
    Replies: 6
    Last Post: 04-27-2012, 08:34 AM
  3. Exam question help: Take 2
    By ÉireKarl in forum C Programming
    Replies: 11
    Last Post: 05-02-2010, 02:12 PM
  4. Exam question help
    By ÉireKarl in forum C Programming
    Replies: 116
    Last Post: 05-02-2010, 12:03 PM
  5. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM

Tags for this Thread