Write a function quality Points that inputs a student's average and returns 4 if a student's average is \(90-100,3\) if the average is \(80-89,2\) if the average is \(70-79,1\) if the average is \(60-69\) and 0 if the average is lower than 60 .

Short Answer

Expert verified
The qualityPoints function evaluates a student's average and returns 4 for an average of 90-100, 3 for 80-89, 2 for 70-79, 1 for 60-69, and 0 for an average below 60.

Step by step solution

01

Define the qualityPoints function

Start by defining the function qualityPoints that takes a single argument, which is the student's average. This function will return an integer value based on the average.
02

Check for average 90-100

Within the function, use an if statement to check if the average is between 90 and 100 (inclusive). If it is, return 4.
03

Check for average 80-89

Use an elif (else if) statement to check if the average is between 80 and 89 (inclusive). If it is, return 3.
04

Check for average 70-79

Next, use another elif statement to check if the average is between 70 and 79 (inclusive). If it is, return 2.
05

Check for average 60-69

Use another elif statement to check if the average is between 60 and 69 (inclusive). If it is, return 1.
06

Check for average below 60

Finally, use an else statement for any other case which means that the average is below 60. In this case, return 0.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Programming Logic
When we discuss programming logic, we are referring to the sequence of instructions that dictates how a computer program behaves and makes decisions. Logic forms the foundation of every program you write, and understanding it is crucial to becoming an effective programmer.

In relation to our exercise on implementing a C++ function, programming logic dictates the flow of execution in determining quality points for a student's average score. For instance, it begins with the reception of input (the student's average) and processes it through a series of 'if' and 'else' conditional statements. These conditionals are used to compare the input against predefined thresholds (90-100, 80-89, and so on) and to produce the intended output based on the input's value.

Getting this logic right is essential. Using pseudocode or flowcharts ahead of actual coding can help visualize the logic. Refining programming logic can often lead to more efficient and readable code, which is especially important in complex programs beyond our straightforward example.
Control Structures
In the realm of computer programming, control structures are elements which dictate the flow of control through a program. These include loops and conditional statements, among others, which allow programs to be dynamic and responsive.

In our example function qualityPoints, control structures help direct the program to execute specific blocks of code depending on the student's average score. This ensures that, rather than running every single command sequentially, our program selectively runs only the relevant sections - a fundamental efficiency in coding.

In terms of exercise improvement advice, make sure that each conditional statement is appropriately structured and that no two conditions can be true at the same time. The use of 'else if' after an initial 'if' statement helps to ensure mutual exclusivity of conditions, leading to clear and predictable code execution paths.
Conditional Statements
The cornerstone of decision-making in code are conditional statements. These statements allow a program to react differently based on varying input or other conditions, essentially providing a way for your code to 'think' and make choices.

In the qualityPoints C++ function, conditional statements are used to assess the student's average and respond with the corresponding quality point. Starting with an 'if' statement checks the first condition (average between 90 and 100), and subsequent 'else if' statements check the remaining ranges down to the 'else' statement for averages below 60.

Understanding 'if-else if-else'

These conditionals are arranged in a descending order of precedence, where the first true condition will terminate the rest of the checks. This order is intentional and crucial in preventing overlapping conditions and ensuring the exclusive execution of only one block of code. Remember, once a true condition is found, none of the following conditions are even evaluated - efficiency in action.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write function distance that calculates the distance between two points \((x I, y l)\) and \((x 2, y 2) .\) All numbers and return values should be of type double.

Find the error(s) in each of the following program segments, and explain how the error(s) can be corrected (see also Exercise 6.48): a) int g() { cout << "Inside function g" << endl; int h() { cout << "Inside function h" << endl; } } b) int sum( int x, int y ) { int result; result = x + y; } c) int sum( int n ) { if (n== 0 ) return 0; else n+sum(n- 1 ); } d) void f( double a ); { float a; cout << a << endl; } e) void product() { int a; int b; int c; int result; cout << "Enter three integers: "; cin >> a >>b>> c; result = a *b* c; cout << "Result is " << result; return result; }

Write a function integerPower(base, exponemt) that returns the value of base \(^{exponent}\) For example, integerPower \((3,4)=3: 3\) is 3 . Assume that exponent is a positive, nonzero integer and that base is an integer. Do not use any math library functions.

Give the function header for each of the following functions: a) Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result. b) Function smallest that takes three integers, x, y and z, and returns an integer. c) Function instructions that does not receive any arguments and does not return a value. [Note: Such functions are commonly used to display instructions to a user.] d) Function intToDouble that takes an integer argument, number, and returns a double- precision, floating-point result.

In this chapter, you studied functions that can be easily implemented both recursively and iteratively.. In this exercise, we present a problem whose recursive solution demonstrates the elegance of recursion, and whose iterative solution may not be as apparent. The Towers of Hanoi is one of the most famous classic problems every budding computer scientist must grapple with. Legend has it that in a temple in the Far East, priests are attempting to move a stack of golden disks from one diamond peg to another (Fig. 6.35). The initial stack has 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from one peg to another under the constraints that exactly one disk is moved at a time and at no time may a larger disk be placed above a smaller disk. Three pegs are provided, one being used for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts. Let’s assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that prints the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, attacking this problem with recursion in mind allows the steps to be simple. Moving n disks can be viewed in terms of moving only n – 1 disks (hence, the recursion), as follows: a) Move \(n-1\) disks from peg 1 to peg \(2,\) using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the \(n-1\) disks from peg 2 to peg \(3,\) using peg 1 as a temporary holding area. The process ends when the last task involves moving \(n=1\) disk (i.e., the base case). This task is accomplished by simply moving the disk, without the need for a temporary holding area. Write a program to solve the Towers of Hanoi problem. Use a recursive function with four parameters: a) The number of disks to be moved b) The peg on which these disks are initially threaded c) The peg to which this stack of disks is to be moved d) The peg to be used as a temporary holding area Display the precise instructions for moving the disks from the starting peg to the destination peg. To move a stack of three disks from peg 1 to peg 3, the program displays the following moves: \(1 \rightarrow 3\) (This means move one disk from peg 1 to peg \(3 .\) ) \\[ \begin{array}{l} 1 \rightarrow 2 \\ 3 \rightarrow 2 \\ 1 \rightarrow 3 \\ 2 \rightarrow 1 \\ 2 \rightarrow 3 \\ 1 \rightarrow 3 \end{array} \\]

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free