Whiteboardsss-3
Whiteboardsss-3
C++
#include<iostream>
using namespace std;
int main()
{
cout<<”PROGRAMMING IS EASY\n”;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x,y,z; float k;
cout<<” enter the 3 numbers”;
cin>>x>>y>>z;
k=(x+y+z)/3;
cout<<” the average of the 3 numbers = ”<< k;
return 0;
}
CONTROL STRUCTURES
1. Selection
a) Simple if statement (only gives an output if the condition met otherwise it
does nothing)
General form: if(condition is true) { output},
Example: #include<iostream>
using namespace std;
int main()
{
float marks;
if(marks>=40)
{
cout<<”passed”;
}
return 0;
}
b) If else statement (Gives an output whether the condition met OR not)
General form: if(condition) { cout<<” true output”;
else cout<<” false output”; }
Example: #include<iostream>
using namespace std;
int main()
{
float marks;
if(marks>=40)
{
cout<<”passed”;
}
else
{
cout<<”failed”;
}
return 0;
}
c) Nested if statement (used when the conditionalities are more than 2, e.g
when grading in an exam)
70 to 100 Distinction
50 to 69 Credit
40 to 49 Pass
0 to 39 Fail
>100 and < 0 Invalid entry
#include<iostream>
using namespace std;
int main()
{
float marks;
cout<<” ENTER MARKS\n”;
cin>>marks;
if(marks>=70&&marks<=100)
cout<<” Distinction\n”;
else if(marks>=50&&marks<=69)
cout<<” Credit\n”;
else if(marks>=40&&marks<=49)
cout<<” Pass\n”;
else if(marks>=0&&marks<=39)
cout<<” Fail\n”;
else cout<<” Invalid Entry\n”;
return 0;
}
2. Sequence
a. Switch statement
3. Iteration
a. for loop
return 0;
}
#include <iostream>
using namespace std;
int main(){
int score;
cout << "Enter the test score: ";
cin >> score;
switch (score/10) {
case 10:
case 9: cout << 'A' << endl; break;
case 8: cout << 'B' << endl; break;
case 7: cout << 'C' << endl; break;
case 6: cout << 'D' << endl; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout << 'F' << endl; break;
default: cout << "Error: score is out of range.\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int marks;
cout << "Enter the test marks: ";
cin >> marks;
switch (marks) {
case (marks>=70&&marks<=100): cout<< “ distinction”<<endl; break;
case (marks>=60&&marks<=69): cout<< “ credit”<<endl; break;
case (marks>=40&&marks<=59): cout<< “ pass”<<endl; break;
case (marks>=0&&marks<=39): cout<< “ fail”<<endl; break;
default: cout << "Invalid Enntry.\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int marks;
do {
cout << "Enter student's mark: ";
cin >> marks;
} while (true);
return 0;
}
Work: using nested for loop, write a C++ program to display a 10 by 12 multiplication table.
The program must be tested , actually submit the code and the output
cols
1 2 3 4 5 6 7 8 9 10
1 1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main()
{
int col, row, colmax, rowmax; colmax=10; rowmax=12;
for(row=1;row<=rowmax;row++)
{
for(col=1;col<=colmax;col++)
{
cout<<row*col<<”\t ”;
}
cout<<endl;
}
return 0;
}
b. while loop
Example: number=10000;
while(number>=50)
{
cout<<number<<endl;
number/=2;
}
c. do .. while loop
Example: number=100;
{
cout<<number<<endl;
number+=1;
}while(number>=1000);
number=1000;
{
cout<<number<<endl;
number+=1;
}while(number<=10);
Types
1. System defined functions int main() sgrt(), printf(), pow(),
2. User defined functions
x = averagesss(2,4,7,88,56,67,55,44);
y= averagesss(12,23,22,333,555,66,777);
z = averagesss(44,55,9,0,0,0,0);
Declare functions:
{
function body;
return ();
}
#include<iostream>
using namespace std;
int maxnum(int a, int b)
{
If(a>b)
cout<<” the maximum number is”<<a<<endl;
else
cout<<”the maximum number is”<<b<<endl;
}
int main()
{
int t,u,v,g,j,k;
t=maxnum(22, 54);
u=maxnum(1000, 505);
v=maxnum(2, 5);
g=maxnum(x,y);
j=maxnum(-65, -200);
k=maxnum(22, -54);
Datatypenamearray[size];
int kenyanvoter2027[23000000];
flaot marks[40];
int BBIT2021CAT1[6];
0 1 2 3 4 5
23 20 28 6 16 20
int BBIT2021CAT1[0] = 23
int BBIT2021CAT1[1] = 20
int BBIT2021CAT1[2] = 28
int BBIT2021CAT1[3] = 6
int BBIT2021CAT1[4] = 16
int BBIT2021CAT1[5] = 20
BBIT2021CAT1[0] = BBIT2021CAT1[0]-3
BBIT2021CAT1[1] = BBIT2021CAT1[1]-3
BBIT2021CAT1[2] = BBIT2021CAT1[2]-3
BBIT2021CAT1[3] = BBIT2021CAT1[3]-3
BBIT2021CAT1[4] = BBIT2021CAT1[4]-3
BBIT2021CAT1[5] = BBIT2021CAT1[5]-3
#include<iostream>
using namespace std;
int main()
{
int BBIT2021CAT1[6] = { 23,20,28,6,16,20};
int n, totmarks, avgmarks; totmarks = 0; avgmarks = 0;
for(n=0;n<6;n++)
{
totmarks = totmarks+BBIT2021CAT1[n];
}
avgmarks = totmarks/6;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int BBIT2021CAT1[6];
int n, totmarks, avgmarks; totmarks = 0; avgmarks = 0;
cout<< “ enter the six values of the array”<<endl;
for(n=0;n<6;n++)
{
cin>>BBIT2021CAT1[n]>>endl;
}
for(n=0;n<6;n++)
{
totmarks = totmarks+BBIT2021CAT1[n];
}
avgmarks = totmarks/6;
return 0;
}