How can I make this shorter?( and more efficient)

May 9, 2019 at 8:53pm
Trying to make this more efficient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if ( score < 60 )

    { grade = 'F'; }

if ( score >= 90 )

    { grade = 'A'; }

if ( score >= 80 && score < 90 )

    { grade = 'B'; }

if ( score >= 70 && score < 80 )

    { grade = 'C'; }

if ( score >= 60 && score < 70 )

    { grade = 'D'; }
Last edited on May 9, 2019 at 8:53pm
May 9, 2019 at 9:13pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
	int score=75;
 	char grade;
 	if(score>=90) grade='A';
 	else if (score>=80) grade='B';
 	else if (score>=70) grade='C';
 	else if (score>=60) grade='D';
 	else grade='F';
 	
 	cout << "grade= " << grade << "\n";
return 0;
}
May 9, 2019 at 9:41pm
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
   int score;
   cout << "Input score [0-100]: ";   cin >> score;
   cout << "Grade = " << ( score >= 60 ? "DCBAA"[ ( score - 60 ) / 10 ] : 'F' ) << '\n';
}
May 9, 2019 at 9:43pm
@anup32 That is the same as the OP’s code, just with less whitespace.
Sorry, my comment was not nice, nor correct. (See a few posts down for more.)


@Alien000
You should not worry too much about efficiency unless you can prove that it is an issue.

For shorter, you want to use a simple array lookup: see line 12:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cmath>
#include <iostream>

int main()
{
  int score;
  {
    std::cout << "score? ";
    std::cin >> score;
  }
  
  std::cout << "FDCBA"[ std::max( 5, std::min( score / 10, 9 ) ) - 5 ] << "\n";
}

The score divided by 10 is one of 0, 1, 2, …, 9, 10, …
Next we use min and max to make sure it is clamped to the range 5..9.
Next we subtract 5 to index the beginning of our grades array ("FDCBA").

This is also “more efficient”, but unless you are processing several hundred thousand student grades, you won’t notice the difference.

Your current strategy is much more readable, and therefore a better answer.

Hope this helps.


[edit]
@lastchance
Timejumper!
You and I had the same brain wave.
Last edited on May 9, 2019 at 11:29pm
May 9, 2019 at 9:48pm
more efficient
Not more efficient in view of execution speed, but maybe for maintenance (for example a future revision may also contain grade E):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	int score=75;
 	char grade('A');
 	
 	if (score < 90) grade='B';
 	if (score < 80) grade='C';
 	if (score < 70) grade='D';
 	if (score < 60) grade='F';  // grade E missing?
 	
 	cout << "grade= " << grade << "\n";
// return 0;
}
May 9, 2019 at 11:02pm
>
same as the OP’s code, just with less whitespace

not exactly same, op's code has 8 comparisons, while mine has 4.
and using 'else if' instead of 'if' for every grade, it becomes much more readable.
May 9, 2019 at 11:17pm
much more readable
One should not underestimate this aspect. Possibly a teacher has to maintain it ;)
May 9, 2019 at 11:27pm
@anup32
You are right. I will fix my comment above. Sorry. :^\
May 10, 2019 at 1:02am
Using max/min is sooo cliché... Let's remove those pesky if/elses, after all, things are always easier without decisions! :D

1
2
3
4
5
6
7
8
9
10
11
#include <cmath>
#include <iostream>

int main()
{
    int x;
    std::cout << "Input score: ";
    std::cin >> x;
    x = std::pow(99.99/(89.99-x),10.0)/(std::pow(99.9/(x+10),10.0)+std::pow(99.99/(89.99-x),10.0))*(x/10.0-5)+0.01; // note from dev who no longer works here: DO NOT CHANGE
    std::cout << "Grade = " << "FDCBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"[x] << '\n';
}

The "AAAAAAAAAAAA" stands for what the program will do if you go out of bounds.

(I was trying to find a way to do this in the form of A sin(B x + C) + D, but couldn't find a valid solution...)

In serious: Both Mike's and Anup's code is equally "maintainable", IMO. But Duthomhas and lastchance break even on the code golfing award (and Duthomhas handles a greater input range).

Last edited on May 10, 2019 at 1:19am
May 10, 2019 at 1:58am
among all these codes lastchance's program is the fastest. and the latest program is the slowest(by a big gap lol)
Topic archived. No new replies allowed.