580 likes | 1.66k Views
Control structures in C++. Lecture 7 By Zahoor Ahmad. Lecture outline. Write a C++ program which calculates grades of students Write a C++ program using switch control structure which takes section number and prints its weekly schedule
E N D
Control structures in C++ Lecture 7 By Zahoor Ahmad
Lecture outline • Write a C++ program which calculates grades of students • Write a C++ program using switch control structure which takes section number and prints its weekly schedule • Write a C++ program which takes a number as input and calculates factorial of the number • Write a C++ program which provides basic calculator functionality of addition, subtraction, multiplication, division • Write a C++ program which prints fabonacci sequence
if(condition){} else if{} else {}: Control Structure Example
Pseudo code • You can write C like code that may not exactly be C code but similar to that
Flowchart If(percent>80) N Y If(percent>70 and percent < 80) N Y If(percent<70)
/* • This program takes percentage of a student as an input and prints its grade. • */ • #include<iostream.h> • int main(void) { • int percentage; • cout << "\n Please enter your percentage marks: "; • cin >> percentage; • if(percentage > 80) { • cout << "\n You have got A grade \n"; • } • else if(percentage > 70 && percentage <= 80) { • cout << "\n You have got B grade \n"; • } • else if(percentage > 60 && percentage <= 70) { • cout << "\n You have got C grade \n"; • } • else { • cout << "\n You have got D grade \n"; • } • return 0; • }
If(section===A) Y N If(section===B) Y N If(section===C) ) Y Flowchart
/* • This program takes section name and prints its class schedule. • */ • #include<iostream.h> • int main(void) { • char section; • cout << "\n Please enter your section name: "; • cin >> section; • switch(section){ • case 'a': • case 'A': • cout << "There are 60 students in section A\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'b': • case 'B': • cout << "There are 61 students in section B\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'c': • case 'C': • cout << "There are 65 students in section C\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'd': • case 'D': • cout << "There are 60 students in section D\n"; • cout << "They have computer programming classes on tuesday and wednesday\n"; • break; • case 'e': • case 'E': • cout << "There are 56 students in section E\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • default: • cout << "You should register with some section if you wish to take this course"; • } • return 0; • }
Flowchart Condition N Y increment
/* • This program calculates factorial of the number which user enters. • */ • #include<iostream.h> • int main(void) { • // Variable Daclaration • int Num, Factorial; • // Variable initialisation • Factorial = 1; • cout << "\n Please enter a number for which you want to calculate factorial: "; • cin >> Num; • while(Num > 0) { • Factorial = Factorial * Num; • Num = Num - 1; • } • cout << "Factorial of the number is: " << Factorial; • return 0; • }
/* • This program presents user with a menu to select from. • */ • #include<iostream.h> • int main() { • // nSelection must be declared outside do/while loop • int nSelection; • nSelection = 0; • do • { • if(nSelection == 0){ • cout << " Make a selection from the menu below: \n"; • } • else{ • cout << " \nYou have made a valid selection\n "; • //Perform some actions according to selection like taking two inputs and perform arithmatic operation on them • } • cout << "Please make a selection: " << endl; • cout << "1) Addition" << endl; • cout << "2) Subtraction" << endl; • cout << "3) Multiplication" << endl; • cout << "4) Division" << endl; • cin >> nSelection; • } while (nSelection == 1 || nSelection == 2 || • nSelection == 3 || nSelection == 4); • return 0; • }
for(initialisation;condition;increment){}: Control Structure Example
/* • This program calculates facbonacci sequence. • 1,1,2,3,5,8,13... • */ • #include<iostream.h> • int main(void) { • // Variable Daclaration • int Num, first, second, temp; • // Variable initialisation • first = 1; • second = 1; • cout << first << ' '; • cout << second << ' '; • cout << "\n How many elements of fabonaci sequence you want to calculate: "; • cin >> Num; • for (int i = 2 ; i < Num ; i++){ • temp = first + second; • cout << temp << ' '; • first = second; • second = temp; • } • return 0; • }