A Case of Projection

 

Modules - Structured Programming 1, Structured Programming 2

 

Teacher’s Notes:

========================================================================

This is a exercise that gives some students with IF statements embedded within a loop.

 

Assignment:

========================================================================

 

A contestant in a reality show receives a grade for their performance in a task.  The grade can be from 0 to 100.  Given the first two grades (n1 & n2) a contestant receives, predict the next grade (n3).  You can assume that the contestant's progress will be linear.  

The program will prompt for the first two inputs.  The first two inputs will be the first two grades, n1 and n2.  The program will then output the projected grade n3.  Any score that is less than 0 will be predicted to be a 0.  Any score that is greater than 100 will be predicted to be 100.

After the program makes each predicted grade, it asks if there are more contestants (y = yes / n = no).  The program will repeat the process as long there are contestants remaining.  Error trapping is not required.

Sample Execution:

Enter the first grade: 35
Enter the second grade: 15
0

Are there more contestants (y = yes, n = no)? y


Enter the first grade: 40
Enter the second grade: 65
90


Are there more contestants (y = yes, n = no)? n

 

Explanation:

The difference between the first two scores is -20.   The projected score is then 15-20  = -5.  Since a score below zero counts as zero, the output is 0.

The difference between the first grade and the second is +25.  The projected score is then 65 + 25 = 90.

 

Solution (Python):

========================================================================

 
repeat = 'y'
 
while repeat == 'y':
    n1 = (int)(input("Enter the first grade: "))
    n2 = (int)(input("Enter the second grade: "))
 
    n3 = n2 + (n2-n1)

    if n3 < 0:
        n3 = 0
    elif n3 > 100:
        n3 = 100
 
    print (n3)
    repeat = input("Are there more contestants (y = yes, n = no)? ")

 

 

Solution (C++):

========================================================================

 
#include <iostream>
 
using namespace std;
 
int main()
{
    int n1, n2, n3;
    char repeat;
    do
    {
        cout << "Enter the first grade: ";
        cin >> n1;
        cout << "Enter the second grade: ";
        cin >> n2;
 
        n3 = n2 + (n2-n1);
 
        if(n3 < 0)
            n3 = 0;
        else if (n3 > 100)
            n3 = 100;
 
        cout << n3 <<  endl;
        cout << "Are there more contestants (y = yes, n = no)? ";
        cin >> repeat;

    }while(repeat == 'y');
    return 0;
}