Question

In: Computer Science

Modify the following C++ program to count the number of correct and incorrect responses typed by...

Modify the following C++ program to count the number of correct and incorrect responses typed by the student. After the student types 5 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75 percent, your program should print “Please ask for extra help” and then terminate. If the percentage is 75 percent or higher, your program should print “Good work!” and then terminate. The program is as follows:

#include<iostream>

#include<iomanip>

#include<cstdlib>

#include<time.h>

using namespace std;

int main()
{
const int no_runs = 5;
int run, n1,n2,answer;

srand((unsigned int)time(NULL));

for(run = 0;run<no_runs;run++)
{
n1 = rand()%9 + 1;
n2 = rand()%9 +1;
while(true)
{
cout<<"What is the product of "<<n1<<" and "<<n2<<": ";
cin>>answer;
if(answer==n1*n2)
break;
cout<<"No. Please try again."<<endl;
}
cout<<"Very good!"<<endl;
}
system("pause");
return 0;
}

Solutions

Expert Solution

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<time.h>

using namespace std;

int main()
{
    const int no_runs = 5;
    int run, n1,n2,answer, correct;
    double percent;
    
    srand((unsigned int)time(NULL));

    // Looping through no_runs times
    for(run = 0;run<no_runs;run++)
    {
        // Generating random number between 1 to 9
        n1 = rand()%9 + 1;
        // Generating random number between 1 to 9
        n2 = rand()%9 +1;
        // Printing a message asking for product of n1 and n2
        cout<<"What is the product of "<<n1<<" and "<<n2<<": ";
        // Getting answer from user
        cin>>answer;
        // Comparing the user answer with actual answer
        if(answer==n1*n2)
            // Incrementing the correct variable to track the current correct answer count
            correct++;
    }
    
    // Calculating percentage of correct answers
    percent = (1.0*correct)/no_runs;
    
    // Checking if the correct answer percentage is less than 75
    if(percent < 75)
        // Printing a message 
        cout<<"Please ask for extra help"<<endl;
    // if the correct answer percentage is greater than or equals to 75
    else
        // Printing a message
        cout<<"Very good!"<<endl;
    system("pause");
    return 0;
}


Related Solutions

In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Modify the program so that after it reads the line typed on the keyboard, it replaces...
Modify the program so that after it reads the line typed on the keyboard, it replaces the ‘\n’ character with a NUL character. Now you have stored the input as a C-style string, and you can echo it with: Explain what you did. #include <unistd.h> #include <string.h> int main(void) { char aString[200]; char *stringPtr = aString; write(STDOUT_FILENO, "Enter a text string: ", strlen("Enter a text string: ")); // prompt user read(STDIN_FILENO, stringPtr, 1); // get first character while (*stringPtr !=...
C++ Here is a program that to validate the ISBN number from user typed and output...
C++ Here is a program that to validate the ISBN number from user typed and output "Valid" if the number is valid, and "Invalid" otherwise. I want to change this program to read the file, and to validate the ISBN number from the file. Please add comments. #include <iostream> #include <string> using std::cin; using std::cout; using std::string; // remove dashes convert letters to upper case string normalize(const string &isbn) { string ch; for (char i : isbn) { if (i...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
Modify the following code to count the number of recursive calls made for the Manhattan-path problem...
Modify the following code to count the number of recursive calls made for the Manhattan-path problem we studied earlier. Next, modify to include stored values and see how that reduces the number of calls made. public class ManhattanWithCallCount { public static void main (String[] argv) { // Test case 1: go from (2,2) to (0,0) int r = 2; int c = 2; int n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + "...
In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
Correct any answers that have the incorrect number of significant figures.
Correct any answers that have the incorrect number of significant figures.       (a) (908.4 - 3.4) / 3.52 x 10⁴ = 0.026       (b) (1206.7 - 0.904) x 89 = 1.07 x 10⁵       (c) (876.90 + 98.1) / 56.998 = 17.11       (d) (4.55 / 407859) + 1.00098 = 1.00210
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT