The document provides an agenda for an introduction to computers lab covering repetition structures (loops), introduction to Visual Studio, and C++ programs. It discusses while, do-while, and for loops through flowcharts and C++ code examples. It also covers solutions containers, projects containers, program components in Visual Studio, and provides examples of C++ programs for various algorithms including determining if a number is positive/negative, even/odd, in a range, and baby weight classification. Exercises include finding maximum of numbers, GCD calculation, decimal to binary conversion, and factorial calculation.
4. Repetition Structure
• A repetition structure represents part of the
program that repeats. This type of structure is
commonly known as a loop.
4
5. Repetition Structure
• Notice the use of the diamond symbol. A loop tests a
condition, and if the condition exists, it performs an
action. Then it tests the condition again. If the
condition still exists, the action is repeated. This
continues until the condition no longer exists.
5
6. Repetition Structure
• In the flowchart segment, the question “is x < y?” is
asked.
• If the answer is yes, then Process A is performed.
• The question “is x < y?” is asked again. Process A is
repeated as long as x is less than y.
• When x is no longer less than y, the repetition stops
and the structure is exited.
x < y? Process A
YES
6
NO
7. Repetition Structure
• The flowchart segment below shows a
repetition structure expressed in C++ as a
while loop.
while (x < y)
x++;
Flowchart C++ Code
x < y? Add 1 to x
YES
7
NO
8. Controlling a Repetition
Structure
• The action performed by a repetition structure
must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
• In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
x < y? Display x
YES
8
QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop? NO
9. Controlling a Repetition
Structure
• ANSWER: By adding an action within
the repetition that changes the value of x.
x < y? Display x Add 1 to x
YES
9
NO
10. A Pre-Test Repetition
Structure
• This type of structure is known as a pre-test
repetition structure. The condition is tested
BEFORE any actions are performed.
x < y? Display x Add 1 to x
YES
10
NO
11. A Pre-Test Repetition
Structure
• In a pre-test repetition structure, if the
condition does not exist, the loop will
never begin.
x < y? Display x Add 1 to x
YES
11
NO
12. A Post-Test Repetition
Structure
• This flowchart segment shows a post-
test repetition structure.
• The condition is tested AFTER the
actions are performed.
• A post-test repetition structure always
performs its actions at least once.
Display x
Add 1 to x
YES
x < y?
12
NO
13. A Post-Test Repetition
Structure
• The flowchart segment below shows a post-test
repetition structure expressed in C++ as a do-while
loop.
do
{
cout << x << endl;
x++;
} while (x < y);
Flowchart
C++ Code
Display x
Add 1 to x
YES
x < y?
13
NO
14. WHILE Expression
Action
ENDWHILE
1. WHILE Loop
Logical expression that
determine whether the
action is to be executed
Action to be
iteratively
performed until
logical expression
is false 14
15. Expression
Action
true
false
Expression is
evaluated at the start
of each iteration of
the loop
If expression is
false, program
execution
continues with next
statement outside
the loop
If expression is
true ,Action is
executed
15
16. Exercise 1
• Write down an algorithm and draw a flowchart to find and
print the largest of N numbers.
16
17. Print max
Set max = x ,
counter = 1
counter<n
Input x
counter=counter+1
x>max
START
max = x
True
Stop
Input n , x
True
false
false
17
18. Exercise 2
• Write a program that prompts the user to enter two positive
integers and finds their greatest common divisor.
• The greatest common divisor of integers 16 and 24 is 8.
So , how do you find the greatest common divisor ?
• Let the two input integers be n1 and n2. Number 1 is a
common divisor, but it may not be the greatest commons
divisor.
• So, you can check whether k (for k = 2, 3, 4, and so on) is
a common divisor for n1 and n2, until k is greater than n1
or n2.
18
20. 2. Do .. While
DO
Action
WHILE Expression
ENDWHILE
Execute Action
If Expression is true then
execute Action again ,
Repeat this process until
Expression evaluates to
false
20
22. Exercise 3
• Write down an algorithm and draw a flowchart to convert from
Positive decimal number to binary number.
22
23. START
Input N
remainder= N% 2
N = N/2
Print remainder
N <> 0
END
false
True
23
X=1
binaryNum = 0
binaryNum += remainder*x
X *= 10
24. 3. For Loop
FOR( initialization , condition , increment )
statements
ENDFOR
The increment portion is
executed at the end of
each iteration
The statements are executed until
this condition becomes false
The initialization is executed once
before the loop begins
24
25. Condition is evaluated at the start
of each iteration of the loop
If condition is true the
Action is executed
Initialization
Action
Increment
Condition
Executed once at the beginning of
for loop
True
False
If condition is false ,
program execution
continues with next
statements.
After the action has
completed , the increment is
evaluated , then the next
iteration of the loop starts.
25
29. What is Visual Studio?
• Visual Studio is a complete set of development tools for
building ASP.NET Web applications, XML Web Services,
desktop applications, and mobile applications. Visual
Basic, Visual C++, Visual C#, and Visual J#.
29
30. Containers
( Solutions and Projects )
• Visual Studio provides two containers to help you
efficiently manage the items that are required by your
development effort, such as references, data
connections, folders, and files. These containers are
called solutions and projects.
30
31. Solutions as Containers
• A solution can contain multiple projects and a project
typically contains multiple items (Items can be files and
other parts of your project such as references, data
connections, or folders).
• Visual Studio automatically generates a solution when
you create a new project. As needed, you can then add
other projects to the solution.
31
32. Projects as Containers
• Visual Studio projects are used as containers within a
solution to logically manage, build, and debug the items
that comprise your application.
• The output of a project is usually an executable program
(.exe), a dynamic-link library (.dll) file or a module,
among others
32
33. Program components
• Header Files : (file.h) contain constant, variable, and
function declarations needed by a program.
• Source Files : (file.cpp) consists of the program
statements comprising a C++ or other programming
language program.
36. Exercise 1
• Algorithm for program that reads a number and
determines whether it is positive, negative or zero.
36
37. Exercise 1: Solution
#include<iostream>
using namespace std;
int main()
{
int number ;
cout<<"Please enter number: ";
cin>> number;
if (number>0)
cout<< "Number is positiven";
else if(number == 0)
cout<< "Number is zeron";
else
cout<< "Number is Negativen";
return 0;
}
37
38. Exercise 2
• Algorithm for program that reads a number and
determines whether it is even or odd.
38
39. Exercise 2: Solution
#include<iostream>
using namespace std;
int main()
{
int number ;
cout<<"Please enter number: ";
cin>> number;
if (number%2 == 0)
cout<< "Number is evenn";
else
cout<< "Number is oddn";
return 0;
}
39
40. Exercise 3
• Algorithm for program that checks whether an input
number lies between specified range. For example: If
user enters range (20, 50) and queried number 34, the
program should display “In range”. On the other hand, if
he enters 76, the program should display “Not in
Range”.
40
41. Exercise 3: Solution
#include<iostream>
using namespace std;
int main()
{
int start,end,number;
cout<<"Please enter range: ";
cin>> start >> end;
cout<<"Please enter number: ";
cin>> number;
if(number >= start )
{
if (number <= end)
cout<<"In rangen";
else
cout<<"Not in rangen";
}
else
cout<<"Not in rangen";
return 0;
}
41
42. Exercise 3: Another Solution
#include<iostream>
using namespace std;
int main()
{
int s,e,number;
cout<<"Please enter range: ";
cin>> s >> e;
cout<<"Please enter number: ";
cin>> number;
if(number>= s && number <= e)
cout<<"In rangen";
else
cout<<"Not in rangen";
return 0;
}
42
43. Exercise 4
• Algorithm for program that determines whether a baby’s
weight is normal or not. For girls, normal babies weight
are 2.5 to 4.5 KG. On the other hand, for boys the
normal weights are 4 to 5.5 KG.
43
45. Exercise 5
• Write an algorithm for electricity company, which charges
customers according to their usage rank (1,2 or 3) and
reading (watt). In rank 1, customers pay 1 L.E./100 watt
with minimum 10 L.E. In rank 2, customers pay 2
L.E./100 watt, but pay at maximum 400 L.E. In rank 3,
customers pay 2.5 L.E./100 watt. Program reads the
usage rank and reading and displays the charged
amount.
45
46. #include<iostream>
using namespace std;
int main()
{
int rank , reading;
cout <<"Enter rank: ";
cin>> rank;
cout<<"Enter Reading watt: ";
cin>> reading;
float price = 0;
switch (rank)
{
case 1:
price = (reading/100.0) *1 ;
if(price < 10)
` cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anthor rankn";
break;
case 2:
price = (reading/100.0)*2;
if(price <= 400)
cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anther rankn";
break;
case 3:
price = (reading/100.0)*2.5;
if(price > 400)
cout<<"Total cost is:
"<<price<<endl;
else
cout<<"The custmer is in
anther rankn";
break;
default:
break;
}
return 0;
}
Exercise 5: Solution
46
47. Exercise 6
• Write a program for a calculator that works on integer
numbers. The user enters two numbers to perform only
one of three basic arithmetic operations (+, -, * and /).
• The interaction with the user might look like this: Enter
your Fractional expression: 1 + 2 The result = 3
• Hint: The user is allowed to do only one operation at a
time.
47
48. Exercise 6: Solution
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op)
{ case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default: // If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
48
49. Lab Exercise
• A program to swap the values of two integers
using third variable.
49
50. Lab Exercise: Solution
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
50