0% found this document useful (0 votes)
48 views4 pages

160 Assign1

Uploaded by

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

160 Assign1

Uploaded by

akashpahwa46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

APSC 160 – Assignment 1

Elementary Programming

Introduction
This assignment helps you learn elementary programming by analyzing, writing algorithms and C++ programs.

Objectives
After completing this assignment, you should be able to:
• Understand and analyze a problem statement.
• Write an algorithm to solve a problem.
• Write and run a C++ program to solve a problem.

Instructions
The assignment has 2 parts:
1. Practice Questions: Write your answers to these questions in a text editor.
2. Problem Statements: For each question, write a C++ program in Visual Studio, debug and test it. Add
comments where needed. Make sure to use a comments header to reflect the intention of your program and
name of the author (you) and the date the program was written. Copy your program in the same text file.
To start with, you may follow the steps in question 6 (no need to submit the details). Note that the sample
runs show the format of the input/output and may be used for testing. However, your program should work
for all the valid input values.

Practice Questions
1. [4] Variables. Write declarations (datatype variableName) for storing the following quantities. Choose
between integers and floating-point numbers (double). Declare constants when appropriate. Note that you
have to assign values to constants when you declare them.
a. The number of days per week
b. The number of days until the end of the semester
c. The number of centimeters in an inch
d. The height of the tallest person in your class, in centimeters
2. [4] Reorder a Program. The following C++ program is supposed to compute the area of a wall with two
windows, but the statements are not in the correct order. Rearrange the statement to correctly calculate the
area (wall without windows). Make sure the comments correctly describe the following lines. Write and test
your program in a code editor (remember to add the libraries). Write the C++ program with the correct order
of statements as your answer.

double wall_height = 8;
int windows = 2;
int main()
double window_width = 3;

1
Dimensions of the wall and
number and dimensions of the windows
cout << "Area of wall with 2 windows is: " << area;
/*
double window_height = 4;
area = area – windows * window_width * window_height;
*/
double wall_width = 30;
*/
/*
This program computes the area of a wall with two
windows.
// Computation and display of the result
double area = wall_width * wall_height;
{
}
return 0;

3. [4] Printout. Given the following C++ program, write your calculations to evaluate x, y, and z after lines 5, 7,
and 9. Write the final printout of the program.

1 #include <iostream>
2 using namespace std;
3 int main(){
4 int x = 4, y = 2, z = 5;
5 y *= x * 4 / (z++ - 2) + x - 13 / 3;
6 cout << "First printout: " << y << endl;
7 z = (2 * z - y % 3 + (x + 5) / z);
8 cout << "Second printout: " << z << endl;
9 x -= 2.6 * z + y / 5 * (x - y);
10 cout << "Third printout: " << x << endl;
11 return 0;
12 }

4. [6] Troubleshooting. The following C++ program is supposed to read the radius for a circle, calculate, and
display the area of the circle. Identify the errors in the program, determine their type (syntax, logic), and write
the correct form.

1 #include <iostream>
2 using namespace std;
3 int main {
4 double radius;
5 Const int PI = 3.14;
6 cout << Enter the radius: ;
7 cin >> radius >> endl;
8 PI = 3.14159;
9 int area_of_circle = radius ^ 2 * PI;
10 cout << "The area is " << area-of-circle << endl;
11 return 0;
12 }
2
5. [4] Algorithm. The following algorithm is supposed to read a value for miles, convert it to kilometer and print
the results. Translate the algorithm into a C++ program and write it as your answer.
Step 1: Declare a double variable named miles and get a value from the user for
miles.
Step 2: Declare a double constant named KILOMETERS_PER_MILE with value 1.609.
Step 3: Declare a double variable named kilometers, multiply miles and
KILOMETERS_PER_MILE, and assign the result to kilometers.
Step 4: Display kilometers to the console along with an informative message.
6. [8] Problem Solving. You are asked to calculate the amount a customer owes. The customer buys two items.
One item is 20% off and the other one is at regular price. The amount of tax is 12% for all items and is added
after the discount. You should ask for the prices of the two items and display the final price after discount and
tax. Write your answers for each of the following steps:
Step 1 - What are the inputs, outputs, constants (if any) and intermediate variables (if any)? Declare the
variables and constants with meaningful names and correct data types.
Step 2 - What is the process to solve the problem? Write the correct equation and calculate the output with
at least two different input values. Determine all the valid and invalid input values.
Step 3 - Write an algorithm that reads the input values (clearly ask the user to input valid values), assigns the
constants and intermediate variables, calculates the results, and displays the results with an informative
message. In the algorithm, you don't need to declare variables, but you may write them as comments.
Step 4 - Turn the algorithm into a C++ program. Add comments where needed. Run and test your program
with typical values.
Note that your program should easily work for different values of tax and discount. You only need to change
the values where you declared the variables.

Problem Statements
7. [6] Wind-chill Temperature. How cold is outside? The temperature alone is not enough to provide the
answer. Other factors including wind speed, relative humidity, and sunshine play important roles in
determining coldness outside. In 2001, the National Weather Service (NWS) implemented the new wind-chill
temperature to measure the coldness using temperature and wind speed. The formula is:

t 𝑤𝑐 = 35.74 + 0.6215𝑡𝑎 − 35.75v 0.16 + 0.4275𝑡𝑎 v 0.16


where 𝑡𝑎 is the outside temperature measured in degrees Fahrenheit and v is the speed measured in miles
per hour. t 𝑤𝑐 is the wind-chill temperature. The formula cannot be used for wind speeds below 2 mph or
temperatures below –58°F or above 41°F (For now you do not have to test this!).
Write a C++ program that prompts the user to enter a temperature between –58°F and 41°F and a wind
speed greater than or equal to 2 and displays the wind-chill temperature. Use pow(a, b) to compute v 0.16.
(You need to include cmath library to your program.)
Here is a sample run:
Enter the temperature in Fahrenheit (must be between -58°F and 41°F): 5.3
Enter the wind speed miles per hour (must be greater than or equal to 2): 6
The wind chill index is -5.56707

3
8. [6] Cost of Driving. Write a C++ program that prompts the user to enter the distance to drive, the fuel
efficiency of the car in miles per gallon, and the price per gallon, and displays the cost of the trip. Here is a
sample run:
Enter the driving distance: 900.5
Enter miles per gallon: 25.5
Enter price per gallon: 3.55
The cost of driving is $125.36
9. [6] Friction Coefficient. The force pushing or pulling an object is related to the object's mass, acceleration,
and a coefficient of friction in the following formula:
F = u × m × g + m × a
where:
F is the force applied to push or pull an object in Newtons (N)
u is a coefficient of friction (uk is small for a smooth surface and large for a rough surface)
m is the object's mass in kilograms (kg)
g is the acceleration due to gravity, which is a constant 9.8 m/s2 (meters per square second)
a is the object's acceleration in meters per square second (m/s2)
Write a C++ program that prompts the user to enter input for F, m, and a, and displays the coefficient of
friction.
Here is a sample run:
Enter the friction force in Newtons: 150
Enter the object's mass in kg: 24.5
Enter the object's acceleration in m/s^2: 4.5
The coefficient for friction is 0.165556
10. [8] Sum of Digits. Write a C++ program that calculates the sum of digits in a given integer. The program
should prompt the user to enter an integer between 0 and 999 (No need to check the range of numbers).
Hint: Use the % operator to extract digits and use / operator to remove the extracted digits.
Here is a sample run:
Enter a number between 0 and 999: 999
The sum of the digits is 27

Submission
Write all the answers to Practice Questions and all the C++ codes for Problem Statements in one text file
(Notepad or Notepad++), save as yourName_Assign1.txt and submit on Moodle.
Please do not submit more than one file.
Please note that Assignments must be the result of your original and individual work although you are
encouraged to discuss with your classmates. You might be tested on your submission in class.

Marking Scheme
The marks are given in square brackets for each question. Total mark is 56.

You might also like