Grading Program works. I have a few questions.

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

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

#31 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 - 02:09 PM

The author of the lesson wanted the person writing the program to write it using if statements and then try to make it again using a switch statement.

This is what he wanted the person taking the lesson to do.

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

I still don't understand why he included having the person taking the lesson use a switch statement. It was easier using a switch statement.
Was This Post Helpful? 0
  • +
  • -

#32 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,732
  • Joined: 25-December 09

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

Posted 27 November 2017 - 02:31 PM

Look at this snippet:
int main()
{
	int numericGrade;

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

	}
}


That switch() statement is not doing anything useful so it is pointless.

Quote

I still don't understand why he included having the person taking the lesson use a switch statement. It was easier using a switch statement.

You have appeared to answer your own question. The purpose of this exercise was probably to introduce you to if() statements and switch() statements so doing the problem the way you're now doing it only exposes you to if() statements. Your present code doesn't really demonstrate the proper use of a switch() statement. While going the "math" route is a way of solving this problem you may still want to try to accomplish the assignment using a switch() statement or two.


Jim
Was This Post Helpful? 0
  • +
  • -

#33 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 01 December 2017 - 08:05 PM

Because the author of the lesson mentioned using if statements as well as a switch. So, I figured it meant he wanted me to find ways to make the program using both ways. I prefer the program without the switch statement and how I figured it out in post 29.
Was This Post Helpful? 0
  • +
  • -

#34 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,732
  • Joined: 25-December 09

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

Posted 01 December 2017 - 09:43 PM

Quote

I prefer the program without the switch statement and how I figured it out in post 29.

Post #29 has a switch statement, one that is doing basically nothing as I pointed out in post #32. That code would work the same without that useless switch, which is what I was pointing out. The last code you posted that contained a switch statement that actually accomplished something was post #16.

The following code and the code in post #32 are equivalent:
int main()
{
    int numericGrade;

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

}



Notice the lack of the switch statement. Your current switch statement lacks any case statements so really all you have is a block as shown above. And really the block is not even necessary the code will operate the same without the block.

So to reiterate if the purpose of the assignment is to teach the use of a switch statements and if statements, IMO, you are not properly accomplishing the objectives of the assignment. I suggest you rework your program to use a correct form of a switch statement and if/else statements instead of your current approach.

Quote

So, I figured it meant he wanted me to find ways to make the program using both ways.

Okay, so then you probably should actually be using a switch() statement and also possibly using if()/else or if()/else if() statements to solve the same type of problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#35 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 02 December 2017 - 08:26 PM

Sorry about taking so long to respond, I thought I replied to you earlier. I was in the middle of trying to figure out how to use a switch statement with the program and in my haste I accidentally put the wrong program as a post. I meant to put this one on as a post to show you that I am taking your suggestions and troubleshooting the program. I am still working on using a switch statement for the program. This is the program I meant to post.
#include<iostream>
#include<string>
using namespace std;


string letterGrade(int numericGrade);

int main()
{
	int 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;

	if (index <=59)
	{
		cout<< numericGrade<<" = "<< grades[index]<<endl;
	}
	else if(index <=60 && index >=69)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=70 && index >=79)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=80 && index >=89)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
}



Enter your grade as an integer.

66
66 = D
Was This Post Helpful? 0
  • +
  • -

#36 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,732
  • Joined: 25-December 09

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

Posted 02 December 2017 - 09:45 PM

Okay, do you realize that if you use the proper logic you only need one comparison in each if() statement?

Edit: Also you don't really need to compute the index using the math and the array, since those operations could be done in the if() chain.

Jim

This post has been edited by jimblumberg: 02 December 2017 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#37 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 03 December 2017 - 06:54 AM

Jim's right that you only need one comparison, but as an important exercise, you should try to explain how this will ever be true: (index <=60 && index >=69).
Was This Post Helpful? 0
  • +
  • -

#38 ndc85430  Icon User is online

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

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

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

Posted 03 December 2017 - 07:03 AM

Also, you need to think about how you want the function letterGrade to look. Do you want it to simply compute the value (in which case it should return it and not do any printing), or is it supposed to be printing (in which case you want to check the return type)? Arguably, the first option is better design since that means the function does a single thing.
Was This Post Helpful? 0
  • +
  • -

#39 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 04 December 2017 - 11:06 AM

jimblumberg Ok, I fixed the logic and you're right, it does work. I honestly didn't know I could do it that way.

ndc85430 I think I understand your statement and if I misunderstood please correct me. I like it this way, one function handles the input that the user keys in and the other prints out the results.


I do have a few questions and please forgive me if they're dumb questions.

1. What did you mean by Edit: Also you don't really need to compute the index using the math and the array, since those operations could be done in the if() chain?.
2. What did CTphpnwb mean by you should try to explain how this will ever be true: (index <=60 && index >=69)?.
Was This Post Helpful? 0
  • +
  • -

#40 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1408
  • View blog
  • Posts: 4,459
  • Joined: 20-March 10

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

Posted 04 December 2017 - 11:10 AM

Well say it out loud in english

if index is less than or equal to 60 and index is more than or equal to 69..

sound right to you ?
Was This Post Helpful? 0
  • +
  • -

#41 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5952
  • View blog
  • Posts: 20,405
  • Joined: 05-May 12

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

Posted 04 December 2017 - 11:42 AM

In American English, some time people say "and" when they really mean "or", particularly when listing things or conditions. I've managed to get out of a few things by rulebook lawyering that the rulebook says "and", even though the intent of the rule was really "or".
Was This Post Helpful? 0
  • +
  • -

#42 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,732
  • Joined: 25-December 09

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

Posted 04 December 2017 - 11:45 AM

Quote

1. What did you mean by Edit: Also you don't really need to compute the index using the math and the array, since those operations could be done in the if() chain?.


Post your current code so we can be sure we're all on the same page.

Jim
Was This Post Helpful? 0
  • +
  • -

#43 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 04 December 2017 - 12:06 PM

snoopy11 I did fix that problem and I realised that jimblumberg was right. I wasn't aware I could do it that way. Which I thanked him.
skydiver Ooooooooooh I get what you mean!!!!. Ok.


This is my current code.
#include<iostream>
#include<string>
using namespace std;


string letterGrade(int numericGrade);

int main()
{
	int 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;

	if (index <=59)//f
	{
		cout<< numericGrade<<" = "<< grades[index]<<endl;
	}
	else if(index <=69)//d
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=79)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=89)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}

}




65
65 = D
Was This Post Helpful? 0
  • +
  • -

#44 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,732
  • Joined: 25-December 09

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

Posted 04 December 2017 - 12:21 PM

Quote

1. What did you mean by Edit: Also you don't really need to compute the index using the math and the array, since those operations could be done in the if() chain?.


Look at the function in question:

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;

	if (index <=59)//f
	{
		cout<< numericGrade<<" = "<< grades[index]<<endl;
	}
	else if(index <=69)//d
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=79)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else if(index <=89)
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}
	else
	{
		cout<< numericGrade << " = " << grades[index]<<endl;
	}

}



First you promised that you were going to return something, so you must return something.

Second you should strive to to keep your functions simple and to separate your program logic from the user interface whenever possible. Your functions should be doing as little as possible, usually one thing. This program should just be computing the letter grade, like the function name suggests. The printing should be done elsewhere.

Here is what I meant by the above quote:

string letterGrade(int numericGrade)
{
    string grade;
    if (index < 60) 
    {
        grade = "F";
    }
    else if(index < 70)
    {
        grade = "D";
    }
...
    else
        grade = "A";

    return grade;
}




Edit: Also note the use of the less than operator instead of the less than or equal, keep the logic as simple as possible.

Edit 2: index in the above snippet should be numericGrade.


Jim

This post has been edited by jimblumberg: 04 December 2017 - 01:36 PM

Was This Post Helpful? 1
  • +
  • -

#45 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 07 December 2017 - 04:19 PM

I worked on the snippet and I can't figure out how to have main print the result. I've tried everything I could think of and I still can't figure out how to pass the value the value back to main. Can I get a hint?
Was This Post Helpful? 0
  • +
  • -

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