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

Lab 7

Here are 3 programs using for loops to: 1. Calculate the average age of 5 people: #include <iostream> using namespace std; int main() { int ages[5]; int total = 0; for(int i = 0; i < 5; i++) { cout << "Enter age " << i+1 << ": "; cin >> ages[i]; total += ages[i]; } float average = total / 5.0; cout << "Average age is: " << fixed << setprecision(1) << average; return 0; } 2. Get minimum and maximum ages of 10 students: #

Uploaded by

Ahmad Prince
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)
30 views

Lab 7

Here are 3 programs using for loops to: 1. Calculate the average age of 5 people: #include <iostream> using namespace std; int main() { int ages[5]; int total = 0; for(int i = 0; i < 5; i++) { cout << "Enter age " << i+1 << ": "; cin >> ages[i]; total += ages[i]; } float average = total / 5.0; cout << "Average age is: " << fixed << setprecision(1) << average; return 0; } 2. Get minimum and maximum ages of 10 students: #

Uploaded by

Ahmad Prince
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/ 7

Lesson Set For LOOP

7
Purpose 1. Understand the purpose of for repetition structure.
2. Understand the usage of for repetition structure.
3. Understand the syntax and working of for looping statement.

Procedure 1. Students should read the Pre-lab Reading Assignment before coming
to lab.
2. Students should complete the Pre-lab Writing Assignment before
coming to lab.
3. In the lab, students should complete Labs 6.1 through 6.4 in sequence.
4. Your instructor will give further instructions as to grading and
completion of the lab.
5. Students should complete the set of lab tasks before the next lab and
get them checked by their lab instructor.

Contents Pre-requisites Completion Page


Time Number

Pre-lab Reading Assignment - 20 min 2

Pre-lab Writing Assignment Pre-lab Reading 10 min 6

Lab 7

Lab 7.1 Basic 30 min 7


For Loop (Sum of numbers understanding of
from 1 to n) looping

Lab 7.2 Basic 30 min 8


For Loop (Factorial) understanding of
looping

Lab 7.3 Basic 30 min 9


For Loop (Squares of understanding of
numbers) looping

Lab 7.4 Understanding of 60 min 10


Lab Tasks While and do-while
loop
PRE-LAB READING ASSIGNMENT

The For Loop Often in programming one needs a statement or block of statements to repeat
during execution. This can be accomplished using a loop. A loop is a control
structure that causes repetition of code within a program. C++ has three types
of loops. In the list of loops the second one is the for loop. The syntax is the
following:

for (initialization; condition; update)


{
statement_1;
statement_2;
:
:
:
statement_n;
}

Here initialization and update arguments are optional. If there is only one
statement, then the curly braces can be omitted. When a for loop is
encountered during execution, the expression is tested to see if it is true or
false. The block of statements is repeated as long as the expression is true, &
loop ends when condition evaluates to false. Consider the following:

#include <iostream>
using namespace std;
int main()
{
int num = 2;
int Temp;
for(int i = 1; i<=10; i++)
{
Temp = num * i;
Cout<<num <<” x ”<< i << ” = ” << Temp <<endl;
}

system(”pause”);
return 0;
}

This program calculates and displays the table of number 2 from 1 to 10. Note
how the while loop controls the execution. Here ‘i’ is initialized with a 1 and is
changed by until it becomes 10 and the execution stops as soon as ‘i’
becomes 11. It calculates and prints the result of each calculation at each
iteration. For loop is often used for applications that require a counter. For
example, suppose we want to find the average (mean) of the first n positive
integers. By definition, this means that we need to add 1 + 2 + 3 + . . . + n and
then divide by n. Note this should just give us the value in the “middle” of the
list 1, 2, ., n. Since we know exactly how many times we are performing a
sum, the for loop is the natural choice.
Initialization The initialization expression is typically used to initialize a counter that must
have a starting value. This is the first action performed by the loop and is
done only once.

Condition The test expression, as with the while and do-while loops, is used to control
the execution of the loop. As long as the test expression is true, the body of
the for loop repeats. The for loop is a pre-test loop which means that the test
expression is evaluated before each iteration.

Update Expression The update expression is executed at the end of each iteration. It typically
increments or decrements the counter.
Lab 7

Lab 7.1  Once you have opened the visual studio copy the following code in the
code editor, try to run the code and observe the output. Write down the
output that appears on the screen in the space given below

#include <iostream>
using namespace std;
int main()
{
int value;
int total = 0;
int number;
float mean;
cout << "Please enter a positive integer" << endl;
cin >> value;
if (value > 0)
{
for (number = 1; number <= value; number++)
{
total = total + number;
}

mean = static_cast<float>(total) / value;

cout << "The mean average of the first " << value
<< " positive integers is " << mean << endl;
}
else
cout << "Invalid input " << endl;

return 0;
}
Lab 7.2  Create a new project with new file called lab6.2.cpp, copy the following
code it and try to fill the missing code of the program. Observe the
output that appears and write it in the box given below

#include <iostream>
using namespace std;

int main()
{
int n, factorial = 1;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= n; ++i) {


factorial = factorial * i;
}

cout<< "Factorial of "<<n<<" = "<<factorial;


return 0;
}
Lab 7.3  Create a new project called 6.3.cpp, copy the following code it and try
to run the program. Observe the output that appears and write it in the
box given below

#include <iostream>
using namespace std;
int main ()
{

for(int i = 1; i<=50; i++)


{
cout<< “Square of Number ”<< i << “ is “<< i*i<<endl;
}

system(“pause”);

return 0;

}
Lab 7.4 Lab Tasks

1. Create a program that allows the user to enter the ages (in years) of five people. The program
should display the average age. Use the “for” statement. Display the average age with one
decimal place.
2. Create a program that takes the ages of ten students as input and at the end it displays the
minimum and the maximum ages entered..

You might also like