Adblock breaks this site

C program problem

Discussion in 'Programming General' started by fball56, Feb 5, 2012.

  1. fball56

    fball56 Active Member

    Joined:
    Nov 8, 2011
    Posts:
    179
    Referrals:
    0
    Sythe Gold:
    0
    C program problem

    I have an assignment to input grades and then output the letter grade for whatever is input. So if i would input 65 then the score is 65/100 or 65% and would be a D. And a 90 would be an A. My question is how to produce a letter when a number is input? I can not use if then statements. If I do then I get a zero on the assignment. I am lost on how else to do it.

    Thank you.
     
  2. Darthatron

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0
    C program problem

    Is this for C++, or C#? They are completely different languages. Your last problem was for C++, so that's why I'm asking.
     
  3. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    C program problem

    Regardless of what language he is using, the functionality the he is seeking will be the same regardless of whether he uses C, C++, or C# for this.

    Easiest way to do it is to just use if statements like so.

    Code:
    if (score == 65)
    {
          //letter grade is D
    }
    else if (score is < 65)
    {
         //letter grade is F
    }
    
    Hopefully you get the idea. You can also check for a range like this:

    Code:
    if (score is < 100 && score > 70) { ... }
    
    Good luck, if you need any more help let me know.
     
  4. fball56

    fball56 Active Member

    Joined:
    Nov 8, 2011
    Posts:
    179
    Referrals:
    0
    Sythe Gold:
    0
    C program problem

    Thanks but my original post said I can't use if then statements or if else. Same thing. And its just C as far as I know lol. All the class material just says C programming.
     
  5. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    C program problem

    My mistake. Well the only other way I can think of doing this is to use a hash table or multi dimensional array. But that would only work if you know the exact grade (it wouldnt work for ranges).

    Store it something like this, variable["65"]["D"] if you're using a multi dimensional array. Then you can just go, variable[score][0] and get the letter D, but that's not a very good way of doing it.

    If/else would be the best option if you were allowed to use them.
     
  6. fball56

    fball56 Active Member

    Joined:
    Nov 8, 2011
    Posts:
    179
    Referrals:
    0
    Sythe Gold:
    0
    C program problem

    ok. I'm not allowed to use them. So the way I have to do it is I run the program and then i have to input a grade for whatever the grade is. So what would the program look like?
     
  7. Darthatron

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0
    C program problem

    It's just cruel to not let you use an If Statement for something like this. But I think your best option would be to create an array of letters.

    Code:
    	char letter[101];
    	int i = 0;
    	for (i; i < 60; i++)
    	{
    		letter[i] = 'F';
    	}
    	for (i; i < 65; i++)
    	{
    		letter[i] = 'D';
    	}
    	for (i; i < 75; i++)
    	{
    		letter[i] = 'C';
    	}
    	for (i; i < 90; i++)
    	{
    		letter[i] = 'B';
    	}
    	for (i; i <= 100; i++)
    	{
    		letter[i] = 'A';
    	}
    Then get the letter from the array
    Code:
    std::cout << letter[grade]
     
  8. fball56

    fball56 Active Member

    Joined:
    Nov 8, 2011
    Posts:
    179
    Referrals:
    0
    Sythe Gold:
    0
    C program problem

    Hey I just wanna let you guys know how I solved it.
    I went and got help from a TA fyi. I didn't get this on my own lol
    Code:
    int letter(int a)
    {
      int x;
      x = (70 - (( a / 55) * 2) - ( a / 65) - ( a / 75) - ( a / 85));
      return(x);
    }
    So this outputs the ascii code for the letter and then it displays the right letter.

    Thanks so much for the help though!!
     
< C++ Console App (Strings) | Spot the error lol >


 
 
Adblock breaks this site