Lab1 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Data Structures and Analysis of Algorithms

EECE 330
Lab 1 – Fall 2020

This lab assignment introduces you to built in data types and numerical representation in C++,
and familiarizes you with C++ as a transition form Python by repeating problems you have already
done in EECE 230.

Guidelines
ˆ This programming assignment consists of 8 problems. You do not have to submit the first
two problems.

ˆ It is due on Friday, September 18, 11:55 pm.

ˆ Topics: transition from Python to C++, basic data types, assignments, conditionals, loops,
and arrays.

ˆ Readings: First three chapters of Stroustrup

? You are supposed to submit your own work. We have a zero tolerance policy for cheating.
Penalties may include one or more of the following: zero grades on programming assignments,
failing the course, disciplinary committee, Dean’s warning, and suspension .

? Lab attendance is mandatory. Violating this rule can lead to a failing grade.

Until we learn more about C++, you should consider the code below as a template to start your
programs.

#include <iostream>
using namespace std;

int main() {
// write your code here

return 0;
}

Shortly, the #include <iostream> line of code specifies that the program below will use the
input output stream library to interface with the system.
The using namespace std; line of code specifies that the system should consider std (short
for standard) as the current namespace. That is, the system will look for variables we use in our
program in the std namespace.
The main function is the entry function of a standalone C++ program. You will write your
C++ code in the context of the main function for the following problems. The function returns a
number to the system to declare its status when done. We will return 0 to declare success for now.

1
Problem 1: Variables and data types
A variable in a C++ program has the following attributes:

ˆ Name: determined by the developer.

ˆ Type: determined by the developer and is associated with a number of bytes and a method
of interpretation on the system.

ˆ Address: a location in memory determined by the system on run time.

ˆ Value: initially whatever was in the memory location designated for the variable, then the
value is determined by the assignment statements of the program.

The following code declares a variable of type int, and prints its name, type, size in number of
bytes (using sizeof), address (using the & operator), initial value, and then it stores in the variable
the value 1024. Then it prints the stored value. Then it increments the variable by 2 and prints
the result.

#include <iostream>
using namespace std;

int
main() {
int x;
cout << "Name: x" << endl;
cout << "Type: int" << endl;
cout << "Size: " << sizeof(x) << endl;
cout << "Address: " << &x << endl;
cout << "Initial value: " << x << endl; // this will be some random value in memory
x = 1024;//Store 1024 in x
cout << "Assigned value: " << x << endl;
x = x + 2; //Add 2 to the value in x and store the result in x.
cout << "Incremented value: " << x << endl;

return 0;
}

ˆ Compile and run the code above and make sure you understand it well.

– What is the minimum number that can be represented using int?


– What is the maximum number that can be represented using int?

ˆ Write code that performs the same operations for a variable ux of type unsigned int.

– What are the minimum and maximum numbers that can be represented using unsigned
int?

ˆ Write code that performs the same operations for a variable r of type double. Use −123.24
as a value.

2
Problem 2: Healthy life
Do Programming Exercise 4.19 [Malik, page 256 (6th edition)]: (reworded)
One way to determine how healthy a person is by measuring the body fat of the person.
The formulas to determine the body fat for females are the following.
a1 = (bw × 0.732) + 8.987
a2 = wrm/3.140
a3 = wam × 0.157
a4 = hm × 0.249
a5 = f am × 0.434
b = a1 + a2 − a3 − a4 + a5
Body fat = body weight − b
Body fat percentage = Body fat × 100/body weight
The formulas to determine the body fat for males are the following.
a1 = (bw × 1.082) + 94.42
a2 = wrm × 4.15
b = a1 − a2
Body fat = body weight − b
Body fat percentage = Body fat × 100/body weight
Where bw stands for body weight, wrm stands for wrist measurement at fullest point, wam
stands for waist measurement at navel, hm stands for hip measurement at fullest point, and f am
stands for forearm measurement at fullest point.
Write a program that takes the needed parameters from the user and computes the body fat of
a person.

Problem 3: Estimate π
The value of π can be approximated using the following formula.
(−1)i
π = 4Σ∞
i=0 .
2i + 1
Write a program that prompts the user for an approximation order n and then computes an
i
approximation of π using that order, i.e., it computes 4 ni=0 (−1)
P
2i+1
. If the user supplies a negative
n, your program should print an error message and exit. Otherwise, your program should print the
(−1)n+1
approximation, and an estimation of the error (which is the (n + 1)-th term: 4 2(n+1)+1 ).

Problem 4. Bill
A group of friends would like to share a restaurant bill. The bill includes two items: food and
drinks. The friends would also like to add a 15% tip to the total for the excellent service they
received. Write a C++ script that performs the following:
1. Prompt the user to enter the price of the food and drinks and stores them in appropriate
variables. You may assume the values entered by the user are positive, non-zero integers
representing the price in Lebanese pounds (LBP).

3
2. Prompt the user to enter the number of friends and stores it in an appropriate variable. You
may also assume this value is a positive, non-zero integer.

3. Compute the values of following quantities and store them in appropriate variables: the total
before tip, the tip, and the total after including the tip.

4. Compute the equal share for each friend after rounding it up to the next multiple of 1000
LBP integer. To round up, suitably use the ceil() function in the cmath library.

5. Display the rounded share

6. Remember that you need to declare the variables you use with the appropriate types.

7. Use cout to display your prompt text.

8. Use cin to read values from the user.

Sample Input/Output:

Enter price of food: 74500


Enter price of drinks: 16000
Enter number of people: 5
Share = 21000 LBP

Problem 5. Time format


Write a C++ program which asks the user to enter the elapsed time in seconds. Your program should
then convert the time into hours, minutes, and seconds, and display the results as hours:minutes:seconds.
(Hints:

ˆ Use a variable for hours, a variable for minutes, and a variable for seconds.

ˆ Use the remainder (modulo %) and integer division (/) operators.

Sample Input/Output:

Enter elapsed time in seconds: 3607


Converted time: 1 : 0 : 7

Problem 6. Quadratic equations solver


Write a C++ program which first asks the user to enter three real numbers a, b and c. Your program
should solve for the real roots of the quadratic equation ax2 + bx + c = 0.
Recall that we have three cases depending on the √
sign of the discriminant ∆ = b2 − 4ac. If
∆ > 0, then the equation has two distinct roots: −b±2a ∆ . If ∆ = 0, then the equation has one root:
b
− 2a . If ∆ < 0, then the equation has no real roots. You may need to use the sqrt or the pow
functions from the cmath library to compute the roots.
Sample Input/Output:

4
Enter a:1.3
Enter b:2.1
Enter c:-15.7
The equation has two roots: 2.7601207396559415 and -4.3755053550405565
-------------

Enter a:1
Enter b:2
Enter c:1
The equation has one root: -1.0
-------------

Enter a:1
Enter b:2
Enter c:3
The equation has no roots

Problem 7. Max in a sequence


Write a C++ program which asks the user to enter:

ˆ an integer n, and

ˆ a sequence of n integers.

Your program is supposed to find the maximum integer in the input sequence. If n = 0, your
program should display the message “Empty sequence”.
For example, if n = 6 and the sequence of integers is

10 1 100 2 7 5,

then the max is 100.

Problem 8. Compare arrays


a) Check if identical using a loop. Write a program which asks the user to enter two
sequences of non-negative integers terminated by −1. The program checks whether or not the
two sequences are identical.
Sample Input/Output:

Enter the integers in the first sequence separated by spaces: 1 2 10 3 -1

Enter the integers in the second sequence separated by spaces: 1 2 10 3 -1


Sequences are equal

-------------
Enter the integers in the first sequence separated by spaces: 1 2 10 3 -1

5
Enter the integers in the second sequence separated by spaces: 2 1 10 3 -1
Sequences are not equal
-------------
Enter the integers in the first sequence separated by spaces: 1 2 10 3 -1

Enter the integers in the second sequence separated by spaces: 2 20 -1


Sequences are not equal

b) Check if permuted. Write a program which asks the user to enter two sequences of integers
and checks whether or not the second sequence is a permutation (reordering) of the first.
Hint: think of a solution more efficient than O(n2 ).

Sample Input/Output:

Enter the integers in the first sequence spearated by spaces: 1 2 10 3 10 10 10 -1

Enter the integers in the second sequence spearated by spaces: 10 10 2 10 1 3 10 -1


YES: second sequence is a permutation of the first
-------------
Enter the integers in the first sequence spearated by spaces: 1 2 -1

Enter the integers in the second sequence spearated by spaces: 1 1 -1


NO: second sequence is not a permutation of the first
-------------
Enter the integers in the first sequence spearated by spaces: 1 2 3 -1

Enter the integers in the second sequence spearated by spaces: 2 3 -1


NO: Second sequence is not a permutation of the first

You might also like