Introduction to Programming Using Visual Basic 6.0

advertisement
2/6/1435 h
Wednesday
Lecture 8
1
Q: What is a %(modulus) operator ?
1.
% (modulus) operator computes the
remainder resulting from integer
division
cout << 13 % 5;
2.
// displays 3
% requires integers for both operands
cout << 13 % 5.0;
// error
2
Q:What are Comments?
1.
2.
3.
4.


Used to document parts of the program
Are ignored by the compiler
Single-Line Comments Begin with //
through to the end of line:
Multi-Line Comments
Begin with /*, end with */
Can span multiple lines: example
3
/* this is a multi-line
comment
*/
Can begin and end on the same line:
int area;
/* calculated area */
-
4
Q:When does / (division) operator performs
integer division
If both operands are integers
cout << 13 / 5;
// displays 2
cout << 91 / 7;
// displays 13
Q:When does the / have a result as a
floating point
2. If either operand is floating point, the
result is floating point
Example:
cout<< 13/ 5.0;
//displays 2.6
cout<<91.0/ 7;
//displays 13.0
1.
5
Named Constants
Q: Define a named constant(constant variable)?
1.
2.
It is a variable whose content cannot be
changed during program execution
Used for representing constant values with
descriptive names:
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;
3.
Often named in uppercase letters
6
The cin Object
Q:What is a cin object?
1.
2.
3.
4.
5.
Standard input object
Like cout, requires iostream file
Used to read input from keyboard
Information retrieved from cin with >>
Input is stored in one or more variables
7
-
6.
Can be used to input more than one value:
cin >> height >> width;
7.
Multiple values from keyboard must be
separated by spaces
8.
Order is important: first value entered goes
to first variable, etc.
8
9.
cin converts data to the type that matches
the variable:
int height;
cout<< How tall is the room? ";
cin >> height;
9
Displaying a Prompt
Q: What is a prompt?
1.
2.
A prompt is a message that instructs the
user to enter data.
You should always use cout to display
a prompt before each cin statement.
cout << "How tall is the room? ";
cin >> height;
10
Algorithms and Flowcharts
Q: Define an algorithm?
Algorithm – A set of instructions
explaining how processing a
module/task leading to a solution is done
Q: Define a flowchart?
A Flowchart is :
 Visual illustration of an algorithm
 Represent logical flow of data through a
function
11
Flowchart Symbol
Algorithm Instruction
Flowchart Symbol
START / RETURN / END / EXIT
ENTER / PRINT / WRITE
(input/output statement)
Variable / Expression
(assignment statement)
PROCESS
(module call)
12
‫‪.‬‬
‫اتخاذ القرار‬
‫‪Flow lines‬‬
‫‪13‬‬
Q: Mention types of Logic Structures?
1.
2.
3.
Sequential logic structure
Selection logic structure
Loop logic structure
14
1. Sequential Logic Structure
Q: Write an algorithm ,design a flowchart then
write a program that represents a sequential
logic structure

Algorithm
1. Enter any three
numbers :A,B,C
2. Sum = A+B+C
3. Mult = A*B*C
4. Print Sum, Mult
5. End
Start
Enter A,B,C
Sum = A+B+C
Mult = A*B*C
PRINT NAME,
Sum, Mult
END
15
Program 4
Write a c++ program that read any three
integer numbers and prints their sum and
multiplication
#include<iostream>
using namespace std;
void main( )
{
int a, b, c, sum, mult;
cout<<“Enter any three numbers”<<endl;
cin>>a>>b>>c;
16
sum=a+b+c;
.
mult=a*b*c;
cout<<“sum=“<<sum<<endl<<“mult=“<<mult<<endl;
system(“pause”);
return;
}
The output screen:
Enter any three numbers
7
6
8
Sum=21
Mul= 336
17
Program 5
Write a c++ program that converts the temperature
degree from Fahrenheit to Centigrade
#include<iostream>
using namespace std;
void main( )
{
int centigrade, Fahrenheit;
cout<<“enter Fahrenheit ”;
cin>> Fahrenheit;
centigrade = 5/9(fahrenheit-32);
18
.
cout<<“centigrade =“<<centigrade;
system(“pause”);
return;
}
The output screen :
Enter Fahrenheit 95
Centigrade is 35
19
Program 6
Q: Write a c++ program that calculates the rectangle’s
area and displays the value on the screen
Note: the program ask the user to enter the length and
width
#include<iostream>
using namespace std;
int main()
{
int length, width, area
Cout<<“this program calculates the area of a rectangle\n”;
Cout<<“enter the length of the rectangle”;
Cin>>length;
20
0
cout<<“enter the width of a rectangle”;
cin>>width;
Area = length*width;
cout<<“ the area of the rectangle is: “<< area<<endl;
system(“pause”);
return 0;
}
Program output with example of input
this program calculates the area of a rectangle
enter the length of the rectangle 10[enter]
Enter the width of a rectangle 5 [enter]
The area of the rectangle is: 50
21
Making Decisions
Control Structures
1. Selection Structure
2. Loop Structure
1.
Selection Structure
The if Statement
23
What is an if Statement?
It allows statements to be conditionally
executed or skipped over
Example:
 "If it is raining, take an umbrella."
 "If it is cold outside, wear a coat."
24
Q: .Mention Types of IF statements
1.
2.
3.
4.
If statement with a null false branch
The if/else Statement
Nested if Statements
The if/else if Statement
25
Draw a Flowchart for an if statement with a
null false branch
26
Flowchart for Evaluating a Decision
27
Q:Write a general format for an if Statement
with a null false branch?
if (expression)
statement;
28
if Statement Notes
1.
2.
Do not place ; after (expression)
Place statement; on a separate line after
(expression), indented:
if (score > 90)
grade = 'A';
29
Q:How an if Statement being evaluated
To evaluate:
if (expression)
statement;
1. If the expression is true, then
statement is executed.
2. If the expression is false, then
statement is skipped.
30
Program 7
Q:Write a c++ program that get three test score and
display the average, then
If the average is greater than 95
display(congratulation! This is a high score)
--------------------------------------------------------if
P
else
31
The Program is:
#include<iostream>
using namespace std;
int main()
{
int score1, score2, score3<<endl;
double average;
cout<<“enter test scores:”;
cin>> score1>> score2>> score3;
average = (score1+score2+score3)/3;
32
Cout<<“your average is:”<<average<<endl;
if (average is >95)
cout<<“congratulation! This is a high score\n”;
system(“pause”);
return 0;
}
Program output
Enter test scores: 80 90 70[enter]
Your average is 80
---------------------------------------------------------------------------Program output with another example of data:
Enter test scores: 100 100 100
Your average is 100 congratulation! This is a high score
33
Expanding the if Statement
Q:Define an expanding if statement?
To execute more than one statement as part of an if
statement, and we have to enclose them in { }:
Q:Write a general format of the Expanding if Statement
if (expression)
{
statement1;
statement2;
}
if (score > 90)
{
grade = 'A';
cout << "Good Job!\n";
}
34
2. The if/else Statement
Q:Define an if/else statement?
1.
2.
Provides two possible paths of execution
Performs one statement if the expression
is true, otherwise performs another
statement .
35
Q: Write a general format for the if/else
statement?
General Format:
if (expression)
statement1;
else
statement2;
36
Q:Draw a flowchart, then write a c++ program that
determines whether a number is odd or even. Then show the
output screen)
37
Program 8
#include<iostream>
using namespace std;
int main()
{
int number;
cout<<“enter any number and I will tell you if
it is even or odd \n”;
cin>>number;
if (number % 2 ==0)
cout<<number <<“is even \n”;
38
else
cout<<number<<“ is odd \n”;
system(“pause”)
return o;
}
pProgram output with a sample of input
enter any number and I will tell you if it is even or odd
17[enter]
17 is odd
Press any key to continue
39
Program 9
no Q:Write a c++ program to enter a salary, if the salary is
>= 5000 then tax is 30%,otherwise tax is 10%
#include<iostream>
using namespace std;
int main()
{
int salary;
float tax;
cout<<“enter salary”;
cin>>salary;
if (salary>=5000)
{
tax = salary*0.3;
cout<<“tax is:”<<tax;
}
40
else
{
tax = salary*0.1;
cout<<“tax is :”<<tax;
}
system(“pause”);
return 0;
}
Program output with a sample of input
enter salary 3500[enter]
tax is 350
------------------------------------------------------------another sample of input
enter salary 5000[enter]
tax is 1500
41
3. Nested if Statements
Q: Define a Nested if Statements
An if statement that is nested inside another if
statement
Q: When does Nested if statement used?
 Nested if statements can be used to test
more than one condition
42
4. The if/else if Statement
Q:What is an if/else if Statement
1.
2.
It tests a series of conditions until one is
found to be true
Often simpler than using nested if/else
statements
43
Q: Write a general format of if/else if
if (expression)
statements ;
else if (expression)
statements ;
.
// other else ifs
else if (expression)
statements ;
else
44
Q: Draw a flowchart for a Nested if statement
.
score>=
n
90
n
y
score>=
80
Score>=
70
A
n
y
B
y
Score>=
60
C
n
y
D
F
45
Program 10
no Q: Using IF statement write a c++ program
that enters a score and displays the following
grades:
Score >= 90 displays 'A'
Score >= 80 displays 'B'
Score >= 70 displays 'C'
Score >= 60 displays 'D'
Score <60 displays (Fail)
46
.#include<iostream>
using namespace std;
int main()
{
int score;
cout<<“enter score: “;
cin>>score;
if (score>=90)
cout<<“A\n”;
else if(score>=80)
cout<<“B\n”;
47
else if(score>=70)
.
cout<<“C\n”;
else if(score>=60)
cout<<“D\n”;
else
cout<<“Fail/n”;
system(“pause”);
return 0;
}
48
Switch Statement
Q:Define switch statement?
It is used to select a statement from several
alternatives.
Program 11
Q: Using Switch statement write a c++ program that enters a
score and displays the following grades:
Score >= 90 displays 'A'
Score >= 80 displays 'B'
Score >= 70 displays 'C'
Score >= 60 displays 'D'
Score < 60 displays (Fail)
49
#include<iostream>
.using namespace std;
int main( )
{
char choice;
cout<<“enter A or B or C”
cin>>choice;
switch(choice)e
{
case’A’:cout<<“you entered A \n”;
break;
case’B’:cout<<“you entered B \n”;
break;
case’C’:cout<<“you entered C \n”;
break;
default:cout<<“you did not entered A or B or C”
}
system(“pause”);
return 0
}
50
2. Loop Structure
First we have to define a counter
Q:Define a counter?
It is a variable that is incremented or
decremented according to the loop counter.
Q: When do we initialize the counter?
The counter must be initialized before
entering the loop.
51
.
Q: Define a loop?
It is a control structure that causes statements
to be executed more than once
Q: Mention the types of loop structure?
1. While
2. Do-while
3. for
52
1. While Loop Structure
Q: Write a general format for a while loop
structure?
while (expression)
Statements;
Q:Design a flowchart that represents a while loop structure?
expression
false
true
Loop body
53
Q: How does while loop work?
1. The expression is evaluated
2. If it is true, the statements in the body of the
loop is executed
3. The expression is evaluated again and again till
it becomes false then the loop stops
54
Program 12
Q:Using while loop write a c++ program that displays numbers from 110, and their squares?
#include<iostream>
using namespace std;
int main( )
{
int count = 1;
while(count<=10)
{
cout<< count<<“ “<<count*count;
count++
}
system(“pause”);
return 0;
}
55
Program 13
Write a c++ program that allows the user to enter
scores for 4 subjects; then displays the average?
#include<iostream>
using namespace std;
int main( )
{
sum = 0;
float average;
float score;
cout<<“Enter the score1”;
cin>> score1;
cout<<“Enter the score2”;
cin>> score2;
cout<<“Enter the score3”;
cin>> score3;
56
.
cout<<“Enter the score4”;
cin>> score4;
sum = score1+score2score3+score4;
average = sum/
cout<<“average is”<<average<<endl;
system(“pause”);
return 0;
}
57
Program 14
Using while loop, write a c++ program that allows the user to enter
scores for 4 subjects; then displays the average?
#include<iostream>
using namespace std;
int main( )
{
int count = 1;
sum = 0;
float average;
float score;
while(count<=4)
{
cout<<“Enter the score”;
58
.cin>> score;
sum = sum+score;
counter =counter +1;
}
cout<<endl;
average = sum/4;
cout<<“average is”<<average<<endl;
system(“pause”);
return 0;
}
-------------------------------------------------------------------------
59
2. DO-While loop
Q: Define do-while loop?
It tests the condition after executing the body of the loop.
Q:Draw a flowchart for the do-while loop?
Body of the loop
expression
false
true
60
Q: Write a general format for do-while loop?
Do
{
Statement 1;
Statement 2 ;
.
.
While(condition)
Q:Using flowcharts , compare between [While] and [Do..While]
control loop
Q: Define an infinite loop?
It is a loop that never finished
61
Program 15
Q: Using do-while loop, write a c++ program that prints
numbers from 1-10
#include<iostream>
using namespace std;
int main( )
{
int counter = 1
Do
{
cout<<counter<<“ “;
count++
}
while ( counter<=10)
system(“pause”);
return 0;
}
62
3. For Loop
Q: Write a general format of the For loop?
For(initial value; expression; updating statement)
-----------------------------------------------------------------------Program 16
Q: Write a c++ program that displays the numbers from 1-10
using for loop?
#include<iostream>
using namespace std;
Int main( )
{
for(int counter = 1; counter<=10; counter++)
cout<<counter<<endl;
System (“pause”);
63
Program 17
Q: Write a c++ program that displays the numbers from 101 using for loop?
#include<iostream>
using namespace std;
int main( )
{
for(int counter = 10; counter<=1; counter++)
cout<<counter<<endl;
system {“pause”);
return 0;
}
64
Functions
Q: Define a function?
1. A function is a collection of statements that is to be
performed.
2. The definition includes:
Return type
Function name
Parameter lists
Body of the function
Q: Write a general format of defining a function?
Return type Function name (parameter lists)
{
Body of the function;
}
65
Q: Mention the benefit of using functions?
The function divides a big problems to smaller ones.
Q:What is a void data type function ?
It is a function that returns no value.
Q: Write a general format of calling a function?
Function name( )
Program 18
Q: Write a c++ program that have the following functions:
1. Main
2. First
3. Second
Then show the out put screen
66
.#include<iostream>
using namespace std;
void First( )
{
cout<<“Now iam inside the first function”<<endl;
}
void second( )
{
cout<<“Now iam inside the second function”<<endl;
}
int main( )
{
cout<<“Iam in the main function”<<endl;
67
.First( ); // call a first function
Second( ); // call the second function
cout<<“Back to the main function”<<endl;
system(“pause”);
return 0;
}
--------------------------------------------------------------
Iam in the main function
Now Iam inside the first function
Now Iam inside the second function
Back to the main function
68
Program 19
Q; Using a function sum(), write a c++ program that add any
two integer numbers
#include<iostream>
using namespace std;
int sum(int, int);
int main( )
{
int val1= 50,val2=90, Total;
Total=sum(val1,val2);
cout<<“the sum of val1 and val2 is : “<<Total<<endl;
system(“pause”);
return 0;
}
69
.
Int sum(num1, num2)
{
return num1+num2;
}
--------------------------------------------------------------------------
70
Program 20
.Q:Write a c++ program using a function square( ), to
calculate the square of any integer number entered
by the user
#include<iostream>
using namespace std:
int main( )
{
int a;
cin>>a;
cout<<“square of”<<a<<“is”<<square(a)<<endl;
system(“pause”);
return 0;
71
.
int square(int a);
{
return a*a;
}
----------------------------
72
Arrays
Q: Define an array?
An array is a variable that can store multiple values
of the same type.
We have to deal with:
1. One dimensional array
2. Two dimensional array
Q:Write a general format of declaration of one
dimensional array?
Data type
Array Name [Array Size];
73
Q:Write a general format declaring two
dimensional array?
. Data type Array name [number of rows][number of columns];
Program 21
Q: Write a c++ program that enable you to enter any 8 inte
and display them in one dimensional array form
#include<iostream>
using namespace std;
int main
{
int A[8];
74
.for(int i=1; i<=8; i++)
{
cout <<“enter A[i]”<<endl;
cin>>A[i];
}
for(int i=1; i<=8; i++)
{
cout A[i];
}
system(“pause”)
return 0;
}
75
Program 22
Q:Write a c++ program that displays the sum of the
elements of the array defined as:
int sum [4] = {34, 30, 20, 56};
#include<iostream>
using namespace std;
int main( )
{
int s[4]={34, 30, 20, 56};
int sum = 0;
for(int i=1; i<=4;i++)
sum = sum+s[i];
76
cout<<“sum is: “<<sum;
system(“pause”);
return 0;
}
--------------------------------------------
77
Program 23
Write a c++ program that calculates the sum of two
given arrays
#include<iostream>
using namespace std;
void main()
int a[4]={2,4,6,8}, i,b[4]={1,3,5,6},c[4];
for(i=0;i<=3;i++)
{c[i]=a[i]+b[i];
cout<<c[i]<<" ";
}
system (“pause”)
return
}
78
Pointers
Q:Define pointers?
Pointers are special type of variables that do not
contain values, but contain addresses of these
values.
Q: Write a general format of declaring pointers?
<variable type> * <variable name>;
Example:
int *xptr
79
Q: How can we assign address to pointer variable/

Assigning an address to a pointer variable:
int *intptr;
intptr = #

Memory layout:
num
intptr
25
0x4a00
address of num: 0x4a00
80
‫‪.‬‬
‫وباهلل التوفيق‬
‫‪81‬‬
Download