Grading Program works. I have a few questions.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

55 Replies - 2370 Views - Last Post: 27 December 2017 - 11:45 AM Rate Topic: -----

#16 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 27 October 2017 - 12:13 PM

Ok, I was able to solve the grading program using math and a switch statement (per the authors original request). This is how I solved the program and it works.

#include <iostream>
#include<string>
using namespace std;

int main()
{
	string grades[5]={"F","D","C","B","A"};
	int entergrade;

	cout<<"Enter your grade as an integer:\n"<<endl;
	cin>>entergrade;
	switch(entergrade/10)
	{
	case 9:
		cout<<grades[4]<<endl;
		break;
	case 8:
		cout<<grades[3]<<endl;
		break;
	case 7:
		cout<<grades[2]<<endl;
		break;
	case 6:
		cout<<grades[1]<<endl;
		break;
	default:
		cout<<grades[0]<<endl;
		break;
	}
}


Was This Post Helpful? 0
  • +
  • -

#17 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 654
  • View blog
  • Posts: 2,712
  • Joined: 13-June 14

Re: Grading Program works. I have a few questions.

Posted 27 October 2017 - 12:18 PM

Do you need the switch? There seems to be a relationship between the value used in the case statements and the array index, so this code could be simplified.
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 27 October 2017 - 12:42 PM

View Postndc85430, on 27 October 2017 - 02:18 PM, said:

this code could be simplified.

Like I did in post 8 and post 12? :neat:
Was This Post Helpful? 1
  • +
  • -

#19 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 29 October 2017 - 05:25 PM

It doesn't need a switch. The author gave the parameters of the lesson and said:

Grading Program
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)

http://www.cplusplus...articles/12974/

I previously made the program using a while loop. I then went back and made it with a switch statement and then modified my program.

He (the author) mentioned a switch statement. From what I can tell he wanted the program to be interactive (The user inputs a number and the program tells you what you grade is). I agree with CTphpnwb and I am working on a new program to use what he suggested, but make it interactive (per the authors instructions). I'm going to use CTphpnwb suggestion as well as a fizz buzz to make it work using only math.
Was This Post Helpful? 0
  • +
  • -

#20 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 18 November 2017 - 03:52 PM

I have a new question. Working on the for loop using integer math to print the results for the grades and I'm really having difficulties. I have an idea how to use integer math and the for loop to make the program work. My idea is that when the user inputs an integer, the for loop will only print the value of i which pertain to the grade. Could you tell me if I'm on the right track?. I can't seem to figure out why it keeps giving me F no matter what integer I key in to the program.
This is my program so far.

#include<iostream>
#include<string>
using namespace std;


int main()
{
	int yourgrade;
	string grades[5] = {"F", "D", "C", "B", "A"};
	for(int i = 0; i < 101; i++) {
		// Taking advantage of integer math.
		int index = i/10 - 5;
		// 100 would result in 5, going past the end of the array
		if(index > 4) index = 4;
		// 0 - 49 will result in negative values.
		if(index < 0) index = 0;
		//cout << i << " = " << grades[index] << endl;
		cout<<"Enter your grade as an integer.\n"<<endl;
		cin>>yourgrade;
		cout<<yourgrade<<" = " << grades [index] <<endl;
	}
}

Was This Post Helpful? 0
  • +
  • -

#21 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,416
  • Joined: 27-December 05

Re: Grading Program works. I have a few questions.

Posted 18 November 2017 - 04:28 PM

Your theory about integer division is OK, but how do your calculations involving index have any relationship to the value of yourgrade that the user enters?

And what's the purpose of the for loop?
Was This Post Helpful? 0
  • +
  • -

#22 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 05:57 PM

You see originally the author of the C++ beginner exercises wanted me to use logic statements and switch statements (Which I did, I used for, while, do while loops as well as a switch). One of the experienced forum members suggested I use integer math (post #3) instead of how I solved the problem with a switch. I've been stuck trying to figure out how to make his program interactive (which is what the author wanted). The example I previously showed was the closest that came to mind. I've been stuck on this program for a while and this was the only solution that I thought of.

This is what the author of the beginner problems wanted me to do for this particular problem.

Grading Program
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
Was This Post Helpful? 0
  • +
  • -

#23 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,416
  • Joined: 27-December 05

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 06:40 PM

Hmm ... how to give you a nudge without giving you the entire solution?

You seem to understand how to perform some simple calculations to select the appropriate element of the letter grades array. But why are you doing that with every possible numerical grade from 0 - 100 but essentially
ignoring the actual grade that the user inputs?
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5949
  • View blog
  • Posts: 20,393
  • Joined: 05-May 12

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 07:45 PM

Seems like a lack of imagination in the part of the exercise creator. That or he/she belongs to the school of programming that believes in explicit and/or procedural approaches instead of the implicit and/or functional approaches.
Was This Post Helpful? 0
  • +
  • -

#25 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 08:18 PM

View Postr.stiltskin, on 21 November 2017 - 08:40 PM, said:

...but essentially
ignoring the actual grade that the user inputs?

It's a copy/paste error. My code was meant as a demonstration to build from, but it was taken as a working solution.
Was This Post Helpful? 0
  • +
  • -

#26 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 08:51 PM

Because when I was helped back in post 3 and I ran the program it displayed this:
(This is the code he suggested I base my program on for this lesson)

#include<iostream>
#include<string>
#include<unistd.h>
using namespace std;

int main()
{
	string grades[5] = {"F", "D", "C", "B", "A"};
	for(int i = 0; i < 101; i++) {
		// Taking advantage of integer math.
		int index = i/10 - 5;
		// 100 would result in 5, going past the end of the array
		if(index > 4) index = 4;
		// 0 - 49 will result in negative values.
		if(index < 0) index = 0;
		cout << i << " = " << grades[index] << endl;
	}
}



When I run it, this is what displays on the screen.

0 = F
1 = F
2 = F
3 = F
4 = F
5 = F
6 = F
7 = F
8 = F
9 = F
10 = F
11 = F
12 = F
13 = F
14 = F
15 = F
16 = F
17 = F
18 = F
19 = F
20 = F
21 = F
22 = F
23 = F
24 = F
25 = F
26 = F
27 = F
28 = F
29 = F
30 = F
31 = F
32 = F
33 = F
34 = F
35 = F
36 = F
37 = F
38 = F
39 = F
40 = F
41 = F
42 = F
43 = F
44 = F
45 = F
46 = F
47 = F
48 = F
49 = F
50 = F
51 = F
52 = F
53 = F
54 = F
55 = F
56 = F
57 = F
58 = F
59 = F
60 = D
61 = D
62 = D
63 = D
64 = D
65 = D
66 = D
67 = D
68 = D
69 = D
70 = C
71 = C
72 = C
73 = C
74 = C
75 = C
76 = C
77 = C
78 = C
79 = C
80 = B
81 = B
82 = B
83 = B
84 = B
85 = B
86 = B
87 = B
88 = B
89 = B
90 = A
91 = A
92 = A
93 = A
94 = A
95 = A
96 = A
97 = A
98 = A
99 = A
100 = A

I may have misunderstood, but from what I can tell he wanted me to use integer math and gave me a nudge with his sample program.

Using his suggestion, my idea is to make whatever the user inputs the loop will print the number and letter pertaining to what grade he received. So, say the user scored a 92, it will print the value of i from 90 to 100. Thats the only idea that came to mind with the suggestion he gave me. I looked everywhere and no one has done that with C++. Honestly, I don't know if that means it isn't possible or whether it is and no one has tried to do it.
Was This Post Helpful? 0
  • +
  • -

#27 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 08:56 PM

Did you read post 12?
Was This Post Helpful? 0
  • +
  • -

#28 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,416
  • Joined: 27-December 05

Re: Grading Program works. I have a few questions.

Posted 21 November 2017 - 09:06 PM

View Postalbert003, on 21 November 2017 - 11:51 PM, said:

... I looked everywhere and no one has done that with C++. Honestly, I don't know if that means it isn't possible or whether it is and no one has tried to do it.

First, the fact that you didn't find a solution by looking everywhere isn't an indicator that it can't be done. You should assume that anything you can do with pencil and paper can also be done with C++ (or any other programming language for that matter). This may not be 100% true, but it's probably a good starting assumption.

CTphpnwb gave you that hint, and now you have a program that demonstrates how to compute the letter grade for EVERY POSSIBLE numerical grade from 0 to 100. So it shouldn't be too much of a leap to apply that same reasoning to compute the letter grade for just one specific numerical grade. Think about it.
Was This Post Helpful? 2
  • +
  • -

#29 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 409
  • Joined: 15-December 14

Re: Grading Program works. I have a few questions.

Posted 27 November 2017 - 01:31 PM

D'oh, I hang my head in shame. You're right, I saw it and put a thumbs up and completely forgot about that post. I used the example you gave me and finally got what I think you meant. It works exactly how I wanted it to work. I couldn't figure out a way to make it work with a switch statement other than the way I previously showed you guys. I agree that this method is easier than using a switch statement.

#include<iostream>
#include<string>
using namespace std;

string letterGrade(int numericGrade);

int main()
{
	int numericGrade;

	switch(numericGrade)
	{
		cout<<"Enter your grade as an integer.\n"<<endl;
		cin>>numericGrade;
		letterGrade(numericGrade);

	}
}

string letterGrade(int numericGrade)
{
	const string grades[5] = {"F", "D", "C", "B", "A"};
	// Taking advantage of integer math.
	int index = numericGrade/10 - 5;
	// 100 would result in 5, going past the end of the array
	if(index > 4) index = 4;
	// 0 - 49 will result in negative values.
	if(index < 0) index = 0;


}


Enter your grade as an integer.

80
80 = B

This post has been edited by albert003: 27 November 2017 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#30 jimblumberg  Icon User is online

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: Grading Program works. I have a few questions.

Posted 27 November 2017 - 01:50 PM

What is the purpose of that switch?


Jim
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4