assignment c++ (nur syahira binti mohd sa'ady)

14
ASSIGNMENT: C++ PROGRAMMING QUESTIONS 1. Who is written C++ ?  Bjarne Stroustrup is written C++.He born on December 30, 1950 is a computer scientist  and creator of the C++ programming language.He is a Danish computer scientist , most notable for the creation and development of the widely used  C++ programming language. [4]  He is a Distinguished Research Professor and holds the College of Engineering Chair in Computer Science at  Texas A&M University, [5]  is a visiting professor at Columbia University , and works at Morgan Stanley as a Managing Director in New York . [6][7][8]   Educations of Bjarne Stroustrup is Stroustrup has a master's degree in mathematics and computer science (1975) from Aarhus U niversity, Denmark, and a Ph.D. in computer science (1979) from the University of Cambridge, England. [5]  His thesis advisor in Cambridge was David Wheeler . [9][10]   Care er of him is Stroustrup began developing C++ in 1978 (then called "C with Classes"), 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." [11]  Stroustrup also wrote a textbook for the language,  The C++ Programming Language. Stroustrup was the head of   AT&T B ell Labs'  Large-scale Programming Research department, from its creation until late 2002. Stroustrup was elected member of the  National  Academy of Eng ineering in 2004. He is a Fellow of the   ACM (1994) and an IEEEFellow. He works at Texas A&M University as a Distinguished Professor where he holds the College of Engineering Endowed Chair in Computer Science. [12][13]  He is also a visiting faculty in Computer Science Department at Columbia University. [14]  ITMO University noble doctor since 2013 [15]0 

Upload: syira-saadyyy

Post on 06-Jul-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 1/14

ASSIGNMENT: C++ PROGRAMMING

QUESTIONS

1. Who is written C++ ?

  Bjarne Stroustrup is written C++.He born on December 30, 1950 is a computer

scientist and creator of the C++ programming language.He is a Danish computer scientist, 

most notable for the creation and development of the widely used C++ programming

language.[4] He is a Distinguished Research Professor and holds the College of Engineering

Chair in Computer Science at Texas A&M University,[5] is a visiting professor at Columbia

University, and works at Morgan Stanley as a Managing Director in New York.[6][7][8] 

  Educations of Bjarne Stroustrup isStroustrup has a master's

degree in mathematics and computer science (1975) from  Aarhus University, Denmark, and

a Ph.D. in computer science (1979) from the University of Cambridge, England.[5]

 His thesisadvisor in Cambridge was David Wheeler .[9][10] 

  Career of him is Stroustrup began developing C++ in 1978 (then called "C with Classes"),

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."[11] 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 (1994) and an IEEEFellow. He

works at Texas A&M University as a Distinguished Professor where he holds the College of

Engineering Endowed Chair in Computer Science.[12][13] He is also a visiting faculty in

Computer Science Department at Columbia University.[14] ITMO University noble doctor since

2013[15]0 

Page 2: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 2/14

2. State statements 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. 

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 .

Page 3: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 3/14

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> 

usingnamespace s td ; 

int  main() { 

float  num , average, sum =0. 0; 

int  i ,  n ; 

cout <<"Maximum number of inputs: "; 

ci n>> n; 

fo r(i=1; i <=  n;+ +i){  

cout << "Enter n"<<i<< ": "; 

cin>> nu m; 

if (num <0. 0){  goto jump;/* Control of the program moves to jump; */  

sum += n um; 

 ju mp : 

average=su m/( i-1);  

cout<< "\nAverage = "<< average; 

return0; 

}

B. While

There are 3 types of loops in C++ Programming:

  for Loop

  while Loop

  do...while Loop

  The for loop

The “for loop” loops from one number to another number and increases by a specified value each time.  

The “for loop” uses the following structure: 

   for (Start value; continue or end condition; increase value)

statement;

Page 4: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 4/14

  Look at the example below:

#include<stdio.h>

int main()

{

int i;

for (i = 0; i < 10; i++)

{

printf ("Hello\n");

printf ("World\n");

}

return 0;

}

  The while loop

The while loop can be used if you don’t know how many times a loop must run. Here is an example: 

#include<stdio.h>

int main()

{

int counter, howmuch;

scanf("%d", &howmuch);

counter = 0;

while ( counter < howmuch)

{

counter++;

printf("%d\n", counter);

}

return 0;

}

Let’s take a look at the example: First you must always initialize the counter before the while loop starts (

counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. Ifthe input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is toincrement the counter inside the loop (counter++). If you forget this the loop becomes infinitive.

Page 5: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 5/14

As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix

incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment

while loop example:

#include<stdio.h>int main(void) {

int i;

i = 0;

while(i++ < 5) {

printf("%d\n", i);

}

printf("\n");

i = 0;

while(++i < 5) {

printf("%d\n", i);}

return 0;

}

The output of the postfix and prefix increment example will look like this:

1

2

3

4

5

1

2

3

4

i++ will increment the value of i, but is using the pre-incremented value to test against < 5.That’s why we get 5 numbers.++i will increment the value of i, but is using the incremented value to test against < 5. That’s why we get4 numbers.

Page 6: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 6/14

  The do while loop

The “do while loop” is almost the same as the while loop. The “do while loop” has the following form: 

do

{do something;

}

while (expression);

Do something first and then test if we have to continue. The result is that the loop always runs once.(Because the expression test comes afterward). Take a look at an example:

#include<stdio.h>

int main()

{

int counter, howmuch;scanf("%d", &howmuch);

counter = 0;

do

{

counter++;

printf("%d\n", counter);

}

while ( counter < howmuch);

return 0;

}

Note: There is a semi-colon behind the while line.

Page 7: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 7/14

C. Break and Continue

Break and continueTo exit a loop you can use the break statement at any time. This can be very useful if you want to stop

running a loop because a condition has been met other than the loop end condition. Take a look at the

following example:

#include<stdio.h>

int main()

{

int i;

i = 0;

while ( i < 20 )

{

i++;if ( i == 10)

break;

}

return 0;

}

In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is anif statement that states that if i equals ten the while loop must stop (break).

With “continue;” it is possible to skip the rest of the commands in the current loop and start from the

top again. (the loop variable must still be incremented). Take a look at the example below:

#include<stdio.h>

int main()

{

int i;

i = 0;

while ( i < 20 )

{

i++;

continue;printf("Nothing to see\n");

}

return 0;

}

  In the example above, the printf function is never called because of the “continue;”. 

Page 8: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 8/14

D. While True

These loops are used when one wants to loop forever and the breaking out condition from loop is not knowCertiain conditions are set inside the loop along with either break or return statements to come out of theloop. For example:

while(true){//run this code 

if(condition satisfies)

break; //return; 

}

These loops are just like any other while loop with condition to stop the loop is in the body of the

while loop otherwise it will run forever (which is never the intention of a part of the code until required so). depends upon the logic of the programmer only what he/she wants to do.

E. Do / While

Difference between while and do-while loop?

->do-while loop is similar to while loop, however there is one basic difference between them – do-while runs a

least one even if the test condition is false at first place. let’s understand this with an example –  

Using while loop: 

main(){

int i=0 

while(i==1){

printf("while vs do-while");}

printf("Out of loop");

Output:Out of loop

Same example using do-while loop 

main()

{int i=0 

do {

printf("while vs do-while\n");

}while(i==1);

printf("Out of loop");}

Page 9: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 9/14

Output:while vs do-while 

Out of loop

F. Jump / Loop

  Cause a certain piece of program to be executed a certain number of times. Consider thesescenarios:

-You want to execute some code/s certain number of time.

-You want to execute some code/s certain number of times depending upon input from user.

Example :

Output :

Enter a positive integer : 5

Factorial of 5 = 120

Page 10: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 10/14

G. If / Else

An if  statement can be followed by an optional else statement, which executes when the booleanexpression is false.

Syntax:

The syntax of an if…else statement in C++ is : 

If (Boolean_expression)

{

// statement (s) will excute if the Boolean expression is true

}

else

{

// statement (s) will execute if the Boolean expression is false}

If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of

code will be executed.

Page 11: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 11/14

Example:

#include <iostream>

using namespace std;

int main ()

{

// local variable declaration:

int a = 100;

// check the boolean condition

if( a < 20 )

{

// if condition is true then print the following

cout << "a is less than 20;" << endl;

}

else{

// if condition is false then print the following

cout << "a is not less than 20;" << endl;

}

cout << "value of a is : " << a << endl;

return 0;

}

When the above code is compiled and executed, it produces the following result:

a is not less than 20;

value of a is : 100

The if...else if...else Statement:

An if  statement can be followed by an optional else if...else statement, which

very usefull to test various conditions using single if...else if statement.

When using if , else if , else statements there are few points to keep in mind.

  An if can have zero or one else's and it must come after any else if's.

  An if can have zero to many else if's and they must come before the else.

  Once an else if succeeds, none of he remaining else if's or else's will be tested.

Page 12: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 12/14

Syntax:The syntax of an if...else if...else statement in C++ is:

if (boolean_expression 1) 

{  // Executes when the boolean expression 1 is true 

else if ( boolean_expression 2) 

// Executes when the boolean expression 2 is true 

else if ( boolean_expression 3) 

// Executes when the boolean expression 3 is true 

else 

// executes when the none of the above condition is true. 

Page 13: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 13/14

Example:#include <iostream> 

using namespace std; 

int main () 

// local variable declaration: 

int a = 100; 

// check the boolean condition 

if ( a == 10 ) 

// if condition is true then print the following 

cout << "Value of a is 10" << endl; 

else if ( a == 20 ) 

{ // if else if condition is true 

cout << "Value of a is 20" << endl; 

else if ( a == 30 ) 

// if else if condition is true

cout << "Value of a is 30" << endl; 

else 

// if none of the conditions is true cout << "Value of a is not matching" << endl; 

cout << "Exact value of a is : " << a << endl; 

return 0; 

When the above code is compiled and executed, it produces the following result:

Value of a is not matching

Exact value of a is : 100 

Page 14: ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)

http://slidepdf.com/reader/full/assignment-c-nur-syahira-binti-mohd-saady 14/14