ASSIGNMENT C++
-
Upload
mohd-fahmi -
Category
Documents
-
view
4 -
download
0
description
Transcript of ASSIGNMENT C++
ASSIGNMENT C++ PROGRAMMING
1. Who is written C++?
Bjarne Stroustrup is a Danish computer scientist, most notable for the creation and development of the widely used c++ programming language. He is a distinguished research professor and holds the college of engineering chair in computer science at Texas a&m university, a visiting professor at Columbia university, and works at Morgan Stanley. Stroustrup began developing c++ in 1978 and in his own words, invented c++, wrote its early definitions, and produced its first implementation. Chose and formulated the design criteria for c++, designed all its major facilities, and was responsible for the processing of extension proposals in the c++ standards committee. Stroustrup also wrote a textbook for the language, the c++ programming language. Stroustrup was the head of at & t bell labs' large-scale programming research department, from its creation until late 2002. Stroustrup was elected member of the national academy of engineering in 2004. He is a fellow of the acm. He works at texas a & m university as a distinguished professor where he holds the college of engineering endowed chair in computer science. He is also a visiting faculty in computer science department at Columbia university. ITMO university noble doctor since 2013. In 2015, he was made a fellow of the computer history museum for his invention of the c++ programming language.
2. State statement below and give an example application in C++ program.
A) GO TO
In C++ programming, goto statement is used for altering the normal
sequence of program execution by transferring control to some other part of the
program.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
In syntax above, label is an identifier. When goto label; is encountered, the control
of program jumps to label: and executes the code below it.
Example 1: goto Statement/* C++ program to demonstrate the working of goto statement. *//* This program calculates the average of numbers entered by user. *//* If user enters negative number, it ignores that number and calculates the average of number entered before it.*/
# include <iostream>using namespace std;int main() { float num, average, sum = 0.0; int i, n; cout<<"Maximum number of inputs: "; cin>>n;
for(i=1; i <= n; ++i) { cout<<"Enter n"<<i<<": "; cin>>num; if(num < 0.0) { goto jump; /* Control of the program moves to jump; */ } sum += num; } jump: average=sum/(i-1); cout<<"\nAverage = "<<average; return 0;}
Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95
You can write any C++ program with use of goto statement and it is generally
considered good idea not to use goto statement.
B) WHILE
How while loop works in C++ Programming?
The while loop checks whether the test expression is true or not. If it is true, code/s
inside the body of while loop is executed,that is, code/s inside the braces { } are
executed. Then again the test expression is checked whether test expression is true or
not. This process continues until the test expression becomes false.
Example 1: C++ while Loop
C++ program to find factorial of a positive integer entered by user. (Factorial of n =
1*2*3...*n)
#include <iostream>using namespace std;
int main() { int number, i = 1, factorial = 1; cout<< "Enter a positive integer: "; cin >> number; while ( i <= number) { factorial *= i; //factorial = factorial * i; ++i; }
cout<<"Factorial of "<<number<<" = "<<factorial; return 0;}
Output
Enter a positive integer: 4
Factorial of 4 = 24
In this program, user is asked to enter a positive integer which is stored in variable
number (supposed user entered 4). Here is the working of while loop in this program:
1. Initially, i = 1, test expression i <= number is true, factorial becomes 1.
2. Variable i is updated to 2, test expression is true, factorial becomes 2.
3. Variable i is updated to 3, test expression is true, factorial becomes 6.
4. Variable i is updated to 4, test expression is true, factorial becomes 24.
5. Variable i is updated to 5, test expression is false and while loop is
terminated.
C++ do...while LoopThe do...while Loop is similar to while loop with one very important difference. In while
loop, check expression is checked at first before body of loop but in case of do...while
loop, body of loop is executed first then only test expression is checked. That's why the
body of do...while loop is executed at least once.
Syntax of do...while Loop
do {
statement/s;
}
while (test expression);
How do...while loop works?
The statement/s inside body of loop is executed at least once, that is, the statement/s
inside braces { } is executed at least once. Then the test expression is checked. If the
test expression is true, the body of loop is executed. This process continues until the
test expression becomes false. Since the body of loop is placed before the test
expression in do...while loop, the body of loop is executed at least once.
C) BREAK AND CONTINUE
There are two statements (break; and continue;) built in C++ programming to alter the normal flow of program.
Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. Thebreak; statement is also used to terminate switch statement.
C++ break Statement
The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears.
Example 1: C++ break
C++ program to add all number entered by user until user enters 0.
// C++ Program to demonstrate working of break statement
#include <iostream>using namespace std;int main() { float number, sum = 0.0;
while (true) { // test expression is always true cout<<"Enter a number: "; cin>>number; if (number != 0.0) { sum += number; } else { break; // terminating the loop if number equals to 0.0 }
} cout<<"Sum = "<<sum; return 0;}
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
In this C++ program, the test expression is always true. The user is asked to enter a number which is stored in variable number. If the user enters any number other than 0, that number is added to sumand stored to it and again user is asked to enter another number. When user enters 0, the test expression inside if statement is false
and body of else is executed which terminates the loop. Finally, the sum is displayed.
D) WHILE TRUEIn C++,a while loop is a control flow statement that allows code to be
executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.
Syntax
while ( condition ) { Code to execute while the condition is true}
Example Program
/* Example Program For While Loop In C++ little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include<iostream>#include<conio.h>
using namespace std;
int main(){ // Variable Declaration int a;
// Get Input Value cout<<"Enter the Number :"; cin>>a;
int counter = 1; //while Loop Block while (counter <= a) { cout<<"Execute While "<<counter<<" time"<<endl; counter++; }
// Wait For Output Screen getch(); return 0; }
Sample Output:
Enter the Number :4Execute While 1 timeExecute While 2 timeExecute While 3 timeExecute While 4 time
E) DO/WHILEIn C++, a do while loop, sometimes just called a do loop, is a control flow
statement that allows code to be executed repeatedly based on a given Boolean condition.
Example Program
/* Example Program For Do..While In C++ little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include<iostream>#include<conio.h>
using namespace std;
int main(){ // Variable Declaration int a;
// Get Input Value cout<<"Enter the Number :"; cin>>a;
int counter = 1; //Do while Loop Block do { cout<<"Execute Do While "<<counter<<" time"<<endl; counter++; }while (counter <= a);
// Wait For Output Screen getch(); return 0;
Sample Output
Enter the Number : 6Execute Do While 1 timeExecute Do While 2 timeExecute Do While 3 timeExecute Do While 4 timeExecute Do While 5 timeExecute Do While 6 time
F) JUMP/LOOPIn C++ a for loop is a programming language statement which allows code to
be repeatedly executed. A for loop is classified as an iteration statement.
Syntax:
for ( variable initialization; condition; variable increment/assignment ) { Code to execute while the condition is true}
Example Program
/* Example Program For for Loop In C++ little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include<iostream>#include<conio.h>
using namespace std;
int main(){
// Variable Declaration int a;
// Get Input Value cout<<"Enter the Number :"; cin>>a;
//for Loop Block for (int counter = 1; counter <= a; counter++) { cout<<"Execute "<<counter<<" time"<<endl;
}
// Wait For Output Screen getch(); return 0; }
Sample Output:Enter the Number :5Execute 1 timeExecute 2 timeExecute 3 timeExecute 4 timeExecute 5 time
G) IF/ELSEThe if statement checks whether the test condition is true or not. If the
test condition is true, it executes the code/s inside the body of if statement. But it the test condition is false, it skips the code/s inside the body of if statement.
Example 1: C++ if Statement
C++ Program to print integer entered by user only if that number is positive.
#include <iostream>using namespace std;
int main() { int number; cout<< "Enter an integer: "; cin>> number;
if ( number > 0) { // Checking whether an integer is positive or not. cout << "You entered a positive integer: "<<number<<endl; }
cout<<"This statement is always executed because it's outside if statement."; return 0;
}
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement.
Output 2
Enter a number: -5
This statement is always executed because it's outside if statement.
C++ if...elseThe if...else executes body of if when the test expression is true and executes the
body of elseif test expression is false.