0% found this document useful (0 votes)
5 views2 pages

Example Repetition Algorithm

The document provides a pseudocode algorithm for converting Fahrenheit temperatures to Celsius, processing 15 temperatures and displaying a completion message. It also includes a C++ program that sums the first five odd numbers input by the user, along with a trace of variable values throughout the program's execution. The final output of the program indicates the sum of the five odd numbers entered.

Uploaded by

rizwan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Example Repetition Algorithm

The document provides a pseudocode algorithm for converting Fahrenheit temperatures to Celsius, processing 15 temperatures and displaying a completion message. It also includes a C++ program that sums the first five odd numbers input by the user, along with a trace of variable values throughout the program's execution. The final output of the program indicates the sum of the five odd numbers entered.

Uploaded by

rizwan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Example algorithm (pseudocode) for the following problem:

Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A


program is to be written which will accept each Fahrenheit temperature, convert it to
Celsius and display the converted temperature to the screen. After 15 temperatures have
been processed, the words "All temperatures processed" are to be displayed on the screen.

Pseudocode (remember this is not the only solution)


Fahrenheit_Celsius_conversion
Set temperature_count to zero
WHILE temperature_count < 15
prompt operator for f_temp
get f_temp
compute c_temp = (f_temp - 32) * 5/9
display c_temp
add 1 to temperature_count
END WHILE
Display "All temperatures processed" to the screen
END

Example trace of following program


Using the input stream of 5 3 2 5 4 1 7 8 trace the following program. i.e. track each
variable through the entire program showing the values of the variables at the end of each
iteration , also show all output . Write a statement explaining what the program does.

#include <iostream >


using namespace std;

int main ()
{
int count, sum, number;
bool lessThanFive ;
count = 0;
sum = 0;
number = 0;
lessThanFive = true;
while (lessThanFive)
{
cin >> number;
if (number % 2 = = 1)
{
count++;
sum = sum + number;
lessThanFive = (count < 5);
}
}
cout<< "The sum of the five odd numbers is :" << sum << "." << endl;
return 0;
}

Tracing the variables number, count, sum, lessThanFive showing each iteration of
the while loop:

Variabl Ini Itera Itera Itera Itera Itera Itera Itera


es tial tion tion tion tion tion tion tion
Val 1 2 3 4 5 6 7
ue
number 0 5 3 2 5 4 1 7
count 0 1 2 2 3 3 4 5
sum 0 5 8 8 13 13 14 21
lessTha T T T T T T T F
nFive
Following iteration 7: count is 5 ending while loop. Final output: The sum of the five odd
numbers is 21. Count refers to the number of odd integers summed, not the total number
of integers examined.

This program sums the first 5 odd numbers entered by the user.

You might also like