CS131 - Assignment 1 - Fall'24.
CS131 - Assignment 1 - Fall'24.
Assignment 1
Instructions:
1. Submission deadline 4 Oct 2024.
2. If you copy from others zero will be given.
3. Assignment should be written with neat handwriting on proper A4 papers.
4. Upload the handwritten assignment in the blackboard, and submit the written
Assignment physically.
Input Unit: This is where data is fed into the system for processing. Input devices like
keyboards, mice, and scanners convert user input into a form that the computer can
process.
Output Unit: This is where processed data is converted into a readable format. Devices
like monitors, printers, and speakers serve as output units, showing the results of
computation.
Memory Unit: It is responsible for storing data and instructions. There are two types of
memory:
o Primary Memory (RAM, Cache): Temporary storage that is fast but volatile.
o Secondary Memory (HDD, SSD): Permanent storage used for large data sets and files.
Arithmetic and Logic Unit (ALU): This performs all arithmetic operations (addition,
subtraction, multiplication, division) and logical operations (comparisons like greater
than, equal to, less than).
Control Unit (CU): It directs the operation of the processor by telling it how to execute
program instructions. It doesn’t process data but manages how instructions are handled.
Central Processing Unit (CPU): The CPU includes the ALU and CU. It’s the brain of
the computer, processing instructions from the input and sending results to the output.
2. Explain all the symbols used to draw the flowchart. Write an algorithm, and draw
the flowchart to check a given number is prime from a given range of numbers.
Flowcharts are diagrams that represent algorithms, workflows, or processes. The following
are common symbols:
3. Write the algorithm and draw the flowchart to convert height in feet to meter,
weight in pounds to kilogram. Then find the BMI and display the following
message depending
BMI values
Less than 18.5 Underweight
Between 18.5 and 24.9 Normal
Between 25 and 29.9 Overweight
Greater than 29.9 Obese
Algorithm:
1. Start.
2. Input height in feet and weight in pounds.
3. Convert height from feet to meters and weight from pounds to kilograms.
weight
4. Compute BMI using the formula: BMI = 2
height
5. Display the BMI category based on:
o BMI < 18.5: Underweight
o 18.5 ≤ BMI ≤ 24.9: Normal
o 25 ≤ BMI ≤ 29.9: Overweight
o BMI > 29.9: Obese
6. End.
Flowchart:
1. Start → Input height/weight → Conversion → BMI calculation → Check BMI range → Display
category → End.
4. Explain about primary data types with their memory size. Write a C++ program to
demonstrate the memory size of the basic datatypes
C++ provides several primary (or fundamental) data types, which can vary in size depending on
the system:
C++, primary data types are basic types of variables, and each has a specific memory size. Here
are the most commonly used primary data types along with their typical memory sizes (in bytes):
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of bool: " << sizeof(bool) << " byte" << endl;
return 0;
}
5. Write a statement (or comment) to accomplish each of the following (assume that
using declarations have been used for cin, cout and endl):
a) Document that a program calculates the power of two integers.
b) Input unsigned int variable x and y with cin in single statement.
c) Declare unsigned int variable i and initialize it to 1.
d) Declare unsigned int variable power and initialize it to 1.
e) Multiply variable power by x and assign the result to power.
f) Preincrement variable i by 1.
g) Determine whether i is less than or equal to y.
h) Output integer variable power with cout and <<.
Document that a program calculates the power of two integers:
unsigned int i = 1;
power *= x;
Preincrement variable i by 1:
++i;
if (i <= y)
a)Assign the product of the current value of x and y to z and postdecrement the value of x:
z = x-- * y;
b)Determine whether the value of the variable count is divisible by 10. If it is, print "Count is divisible by
10.":
if (count % 10 == 0) {
std::cout << "Count is divisible by 10." << std::endl;
}
c)Postincrement the variable x by 1, then add it to the variable total:
total += x++;
d)Calculate the remainder after q is divided by divisor and assign the result to q. Write this statement in
two different ways:
1. Using the modulus operator:
q = q % divisor;
b) if ( c => 7 )
cout << "c is equal to or greater than 7\n";
c) if ( age >= 65 )
cout << "Age is greater than or equal to 65" << endl;
else;
cout << "Age is less than 65 << endl";
Error 1: The semicolon after the if condition ends the statement prematurely.
if (c < 7)
cout << "c is less than 7\n";
Error 2: The comparison operator should be >= (greater than or equal to).
if (c >= 7)
cout << "c is equal to or greater than 7\n";
8. Write the code to check whether a given number is positive or negative using
switch and if..else. Explain dangling else problem.
C++ Code
#include <iostream>
using namespace std;
int main() {
int number;
return 0;
}
• It checks if the number is greater than zero, less than zero, or equal to zero, and prints the
corresponding message.
3. Using switch:
• It first checks if the number is greater than zero. If true, it prints that the number is positive.
• If false, it performs another switch to check if the number is less than zero and prints the
corresponding message.
In this example, it is unclear whether the else corresponds to the first if or the second if. This ambiguity
can cause logical errors in the program.
a)
int a = 5, b = 2;
b = a - b; // b becomes 5 - 2 = 3
b -= 1; // b becomes 3 - 1 = 2
a = a / b; // a becomes 5 / 2 = 2 (integer division)
cout << a; // Output: 2
switch (ch) {
case 'a':
cout << "Vowel";
break;
case 'e':
cout << "Vowel";
break;
case 'u':
cout << "Vowel"; // This case matches since ch is 'u'
break;
default:
cout << "Consonant";
}
Since ch is 'u', the third case matches, and it will print "Vowel".
Summary of Outputs
10. Write a complete C++ program to calculate car insurance premium based on the
driving record of the driver. The program should calculate the insurance premium
using the following criteria:
If the driver has more than 3 accidents in the past year, add a surcharge of 25%
to the base premium.
If the driver has two accidents in the past year, add a surcharge of 10% to the
base premium.
If the driver has only one accident, add a surcharge of 5% to the base
premium.
If the driver has zero accidents, apply a discount of 10% to the base premium.
Here are the steps to write the program:
1. Prompt the user to enter the number of accidents in the past year.
2. Based on the entered driving record, find the appropriate surcharge or discount
to the base premium.
3. Calculate and display the final insurance premium by adding up the base
premium and any surcharges or discounts.
Your program should include appropriate variable declarations, conditional statements,
and input/output operations to achieve the desired functionality.
#include <iostream>
using namespace std;
int main() {
// Declare variables
const float basePremium = 1000.0; // Base insurance premium
int accidents; // Number of accidents in the past
year
float finalPremium; // Final premium after applying
surcharges/discounts
return 0;
}
Explanation:
1. Variables:
o basePremium: The base premium is fixed at 1000 SAR.
o accidents: The number of accidents entered by the user.
o finalPremium: The premium calculated based on the accident history.
2. Input:
o The program prompts the user to input the number of accidents in the past year.
3. Conditions:
o If the user has more than 3 accidents, a 25% surcharge is applied.
o If the user has exactly 2 accidents, a 10% surcharge is applied.
o If the user has exactly 1 accident, a 5% surcharge is applied.
o If the user has no accidents, a 10% discount is applied.
4. Output:
o The program calculates the final premium by adjusting the base premium and prints the
result.
Example: Assuming a base premium of 1000 SAR, two accidents in the past year
would pay (1000 + 0.1 * 1000) = 1100 SAR.