0% found this document useful (0 votes)
4 views

Lab-3

This document outlines a lab focused on C++ programming, specifically selection and repetitive statements. It includes activities for creating console applications that involve user input to determine grades, find maximum and minimum values, perform arithmetic operations, and compute averages. Each activity contains tasks that require modifications to the initial code to enhance functionality and address potential issues.

Uploaded by

zerefq15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab-3

This document outlines a lab focused on C++ programming, specifically selection and repetitive statements. It includes activities for creating console applications that involve user input to determine grades, find maximum and minimum values, perform arithmetic operations, and compute averages. Each activity contains tasks that require modifications to the initial code to enhance functionality and address potential issues.

Uploaded by

zerefq15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 03: Selection statements and repetitive statements

Lab Objectives
• To understand basic problem-solving techniques.
• To be able to develop algorithms through the process of top-down, stepwise refinement.
• To be able to use the selection statements of C++ language
• Understand switch statement
• Understand looping/repetitive statements (while, for, do…while)
• Understand increment and decrement Operators
• Finding unacceptable behaviours using simple test cases
• Understand software corrections.

switch statement
The switch statement is used when there are multiple values of a single variable are expected.

Activity 1
Create a new Console Application and add a new C++ file with the code below. This program
reads a mark from the user and prints the corresponding grade.
#include <iostream>
int main() {
int mark;
std::cout << "Enter the mark" << std::endl;
std::cin >> mark;
if (mark >= 90) {
std::cout << "A" << std::endl; }
else if (mark >= 80) {
std::cout << "B" << std::endl; }
else if (mark < 60) {
std::cout << "F" << std::endl; }
return 0;
}

Task 1.1: There are two grade specifications missed in the last program: C grade and D grade.
Modify the code in Task 1.1 to allow these two grades to be counted.
C: 80 > mark ≥ 70
D: 70 > mark ≥ 60
Activity 2
Create a new Console Application and add a C++ file with the code below. This code finds the
maximum number of three integers entered by the user.
#include <iostream>
int main() {
int a, b, c;
std::cout << "Enter 3 numbers: ";
std::cin >> a >> b >> c;

// Finding the maximum


int max = a; // Assume 'a' is the largest initially

if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
std::cout << "Max = " << max << std::endl;
return 0;

Task 2.1: Modify the previous code in Task 2.1, so it would be able to find the minimum
number of three numbers beside the maximum number.

Activity 3
Create a new project and add a new C++ file with the code below. This code asks the user to
select one of the operations (+, -, /, ×) and enter two numbers to perform the calculation.
#include <iostream>

int main() {
char input;
std::cout << "Enter an operation (+, -, x, /, %): ";
std::cin >> input;

int a, b;
std::cout << "Enter two numbers for calculation: ";
std::cin >> a >> b;

switch (input) {
case '+':
std::cout << "Adding = " << (a + b) << std::endl;
break;
case '-':
std::cout << "Subtracting = " << (a - b) << std::endl;
break;
default:
std::cout << "The operation is not valid." << std::endl;
}

return 0;
}

Task 3.1: The previous code in Task 3.1 is missing the implementation of three more operations:
multiplication, division, and modulo operations (x, /, %). Do the necessary to add these
operations in the code.
In the case of a division operation, make sure that you do not allow the division on zero. In the
case of modulo operation, make sure that you have the bigger number over the small number.

Activity 4
Create a new project and add a new C++ file with the code below. This program computes the
average of 10 grades entered by the user.
#include <iostream>
#include <iomanip> // For std::setprecision

using namespace std;

int main() {
double total = 0.0, grade = 0.0;
int counter = 1;

while (counter <= 10) {


cout << "Enter grade: ";
cin >> grade;
total += grade;
counter++;
}

double average = total / 10.0;


cout << "Class average is " << fixed << setprecision(2) << average << endl;

return 0;
}
Task 4.1: The previous code always computes the average of exactly 10 grades entered by the
user. How may you change the code to make it flexible and add ‘numberOfGrades’ variable that
allows the user to enter beforehand the number of grades that he/she wants for computation.
Task 4.2: Replace the while loop with a for loop and make necessary changes.

Activity 5
Create a new project and add a C++ file with the following code. This program asks the user to
enter as many grades as he/she desire and then computes the total and the average of all entered
𝒕𝒐𝒕𝒂𝒍 𝒔𝒖𝒎
grades ( 𝒂𝒗𝒈 = ). The user can always enter -1 at any time to terminate the loop of
𝒄𝒐𝒖𝒏𝒕
entering grades.
#include <iostream>
#include <iomanip> // For std::setprecision

using namespace std;

int main() {
double total = 0.0;
int counter = 0;
double grade = 0.0;

cout << "Enter grade (-1 to end): ";


cin >> grade;

while (grade != -1) {


total += grade;
counter++;

cout << "Enter grade (-1 to end): ";


cin >> grade;
}
double average = total / counter; cout << "Class average = " << fixed
<< setprecision(4) << average << endl;

return 0;
}

Task 5.1: Test the previous program with the following inputs. Describe any behaviours.
Test 1 Test 2 Test 3
10 9 -1
9 -1
8
-1
Task 5.2: How might you improve the code to overcome any unacceptable behaviors in the
program in Task 2.1?
Task 5.3: The user wants also to find the minimum and the maximum of all entered grades. How
might you modify the code to allow this new requirement?
Hint: assume the biggest number for the minimum variable and the smallest number for the
maximum variable with which you start.

You might also like