Docsity

CS1 Practice coding with answers, Exercises of Computer science

Toggle sidebar
bg1
Student 1
Student’s Name
Professor’s Name
Computer Science 1
Date
Problem Solving: Tasks and Solutions
Task 1. Write pseudocode for a program that asks the user for a temperature in
Fahrenheit and converts it to Celsius. The formula is C = (F − 32) × 5/9.
Solution
BEGIN
DISPLAY "Enter temperature in Fahrenheit: "
INPUT fahrenheit
SET celsius = (fahrenheit - 32) * 5 / 9
DISPLAY "Temperature in Celsius: " + celsius
END
Explanation. This pseudocode follows the input-process-output (IPO) model. The
program first prompts the user and reads a Fahrenheit value. It then applies the conversion
formula, storing the result in a variable called celsius. Finally, it displays the converted value.
The formula subtracts 32 from the Fahrenheit input and multiplies the difference by five-ninths,
which is the standard mathematical relationship between the two temperature scales. Each step
maps to a fundamental programming operation: input captures data from the user, assignment
stores a computed result, and output communicates the answer back.
Task 2. A store gives a 10% discount on purchases over fifty dollars. Write pseudocode
that reads the purchase amount, determines whether the discount applies, calculates the final
price, and displays it.
Solution
BEGIN
DISPLAY "Enter purchase amount: "
pf3
pf4
pf5

Partial preview of the text

Download CS1 Practice coding with answers and more Exercises Computer science in PDF only on Docsity!

Student’s Name Professor’s Name Computer Science 1 Date Problem Solving: Tasks and Solutions Task 1. Write pseudocode for a program that asks the user for a temperature in Fahrenheit and converts it to Celsius. The formula is C = (F − 32) × 5/9. Solution BEGIN DISPLAY "Enter temperature in Fahrenheit: " INPUT fahrenheit SET celsius = (fahrenheit - 32) * 5 / 9 DISPLAY "Temperature in Celsius: " + celsius END Explanation. This pseudocode follows the input-process-output (IPO) model. The program first prompts the user and reads a Fahrenheit value. It then applies the conversion formula, storing the result in a variable called celsius. Finally, it displays the converted value. The formula subtracts 32 from the Fahrenheit input and multiplies the difference by five-ninths, which is the standard mathematical relationship between the two temperature scales. Each step maps to a fundamental programming operation: input captures data from the user, assignment stores a computed result, and output communicates the answer back. Task 2. A store gives a 10% discount on purchases over fifty dollars. Write pseudocode that reads the purchase amount, determines whether the discount applies, calculates the final price, and displays it. Solution BEGIN DISPLAY "Enter purchase amount: "

INPUT purchaseAmount IF purchaseAmount > 50 THEN SET discount = purchaseAmount * 0. SET finalPrice = purchaseAmount - discount ELSE SET finalPrice = purchaseAmount END IF DISPLAY "Final price: $" + finalPrice END Explanation. This solution uses a selection structure (IF-THEN-ELSE) to branch the program logic based on a condition. If the purchase exceeds fifty dollars, the program computes ten percent of the amount and subtracts it. Otherwise, the price stays unchanged. The IF-THEN- ELSE construct is one of the three fundamental control structures in programming, alongside sequence and iteration. The condition purchaseAmount > 50 is a Boolean expression that evaluates to either true or false, directing the flow of execution down exactly one of the two paths. Task 3. Trace the following pseudocode and show the value of x after each line executes. SET x = 5 SET x = x + 3 SET x = x * 2 SET x = x - 4 Solution Line 1: x = 5. The variable x is initialized with the value 5. Line 2: x = 8. The current value of x (5) is added to 3, and the result (8) is stored back in x. Line 3: x = 16. The current value of x (8) is multiplied by 2, producing 16. Line 4: x = 12. The current value of x (16) has 4 subtracted, giving a final value of 12.

INPUT score INPUT score INPUT score SET average = score1 + score2 + score3 / 3 DISPLAY average END Solution The logic error is on the line that calculates the average. Because of operator precedence, division is performed before addition. The expression score1 + score2 + score3 / 3 divides only score3 by 3 and then adds score1 and score2 to that result, which does not produce a true average. The corrected line uses parentheses to force the addition to happen first: BEGIN INPUT score INPUT score INPUT score SET average = (score1 + score2 + score3) / 3 DISPLAY average END Explanation. This task tests understanding of operator precedence, sometimes called the order of operations. In most programming languages and in standard mathematics, multiplication and division are evaluated before addition and subtraction unless parentheses override that order. For example, if the three scores are 80, 90, and 100, the buggy version computes 80 + 90 + (100 / 3) = 203.33 instead of the correct (80 + 90 + 100) / 3 = 90. Parentheses are the standard tool for controlling evaluation order in any language. Task 6. Explain the difference between a syntax error and a logic error. Provide one example of each in pseudocode. Solution

A syntax error occurs when the code violates the grammatical rules of the programming language. The program cannot run at all until every syntax error is fixed because the compiler or interpreter does not understand the instruction. A logic error occurs when the code runs without crashing but produces an incorrect result because the programmer wrote instructions that do not match the intended behavior. Syntax error example: DISPLAY "Hello World The closing quotation mark is missing. A compiler would flag this line and refuse to execute the program. Logic error example: SET area = length + width If the goal is to compute the area of a rectangle, this line should use multiplication, not addition. The program will run and produce a number, but that number will be wrong. Explanation. Distinguishing between error types is a foundational debugging skill. Syntax errors are easier to fix because the development environment identifies the exact location and nature of the violation. Logic errors are more dangerous because the program appears to work but the output is incorrect. Finding logic errors requires testing the program with known inputs and comparing actual output against expected output, a process called verification.