Top Banner
(\ any symbol ) \ v \ b \ a \ t \ r \ n The symbol will appear as output ’\ to print )‘( ”\ to print )“( \\ to print (\) Vertic al tab Back space Aler t Move to the next tab Return to the start of current line New line Finding the output : bool char long double 19 digits Double 15 digits Float 7 digits long int 4 bytes int 4 bytes short int 2 bytes true or 1 False or 0 For character s Numbers with decimal points All for integers number BASIC All above should be between “ “
31

· Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Mar 19, 2018

Download

Documents

lyphuc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

(\any symbol)\v\b\a\t\r\n

The symbol will appear as output

’\to print)‘(

”\to print)“(

\\ to print (\)

Vertical tab

Back space

AlertMove to the next tab

Return to the start of current line

New line

Finding the output:

boolcharlong double19

digits

Double

15 digits

Float

7 digits

long int

4 bytes

int

4 bytes

short int

2 bytes

true or 1

False or 0

For charactersNumbers with decimal pointsAll for integers number

BASICS

All above should be between“ “ And we use them in cout statement

Page 2: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Assignment statement:

Variable=value; such as n=n+1;

Relational operators:

Not equalequalgreater than or equal

less than or equal

Greater thanLess than

!===>=<=<>We use them mostly in if statement.

Arithmetic operators:

)% , /,*,-,+ ( Find the output:Therefore, the question is:3*(4+1)/4-7%5Operator precedence:1. ()(4+1)=53*5/4-7%52. * % / from left to right.15/4-7%53-7%53. + - from left to right.3-21

*** % for integer numbers onnnllly*** Keep in ur mind that the answer of 15/4 is different than the answer of 15.0/4 or 15/4.0 in c++.15/4=3But 15.0/4 or 15/4.0 = 3.75So its means that if you want to do a floating points division .one way to write one of the two numbers the num in decimal point in type double or float from the beginning.The second way to cast one or both on the integers to double. As it shown:

Page 3: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

decrementIncrement

x-- (turtle)--x (rabbit)x++ (turtle)++x ( rabbit)

Return the old value then decrement by

one

decrement by one then return the new

value

Return the old value then Increment by

one

Increment by one then return the new

value

ANOTHER EXAMPLE

*** % for integer numbers onnnllly*** Keep in ur mind that the answer of 15/4 is different than the answer of 15.0/4 or 15/4.0 in c++.15/4=3But 15.0/4 or 15/4.0 = 3.75So its means that if you want to do a floating points division .one way to write one of the two numbers the num in decimal point in type double or float from the beginning.The second way to cast one or both on the integers to double. As it shown:

22.666667

4443

But when we include them in cout , assignment statement and arithmetic expression it work defferent but at the end the value of num will be 4

So when u write it alone they are the same ++num = num++ = num+1

k = 7;i = 0;k = i++;

k = 7;i = 0;k = ++i ;

Page 4: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

IF STATEMENTIf statement chooses between alternatives based on a test expression.

Some important symbol we deal with in if statement && , ||

symbol Condition 1 Condition 2 result Body execution

&&

√ √ √ √√ × × ×× √ × ×× × × ×

||

√ √ √ √√ × √ √× √ √ √× × × ×

Exa1:For example we will create a program for calculating absolute values: |x|

0 0 1

7 1 7 0

ik

ik

Return the old value then increment

Increment then return the new value

1

21

2

So her we have two prospect either the number is less than zero(negative) or greater than

zero(positive).. if it was a negative multiply it with -1 to be positive

otherwise keep it positive .

Page 5: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Exp2: A program that find the max and min of three different numbers:

Exp3:A program that classify grades according to these criteria:

A (90-100)

B(89-80)

C(79-70)

D(69-60)

F(59-0)

Exp4: A program that out put "hi" if it read "a" or "A" otherwise "bye":

(just to show you how we use "||"=or )

If statement deal with true or false if the condition inside the if statement is true the program will execute the body if not it well moveto the first sentence after the body so if the body was more than one statement you should put the body within { }

Nested if :

So here, we have (3*2=6) prospects .. I well deal with this like that two prospects y is the max ..two prospects x is the max.. two prospects z is the max so we

have 6 prospects now .

How to write x>y>z:

x>y&&y>z

2 3

output

Page 6: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

IDEA:

** the first else for the last if .

** if the first if is false we will move to the last else directly.

**if the first is true we will move to the second if and so on ^ ^.

LoopAn if statement chooses one of the alternative , but never causes any of the alternatives to be done more than once ,loops causes a loop in which a statement can be done more than once.

while loop:

If the condition is wrong, the program will not

Execute the body even once.

For loop:

The first value for the counter

The condition

The increment or decrement

*First the test is evaluated (1<=3)?? True!* execute the body*then increment by 1, i=2*go back to the condition (2<=3)?? True !*execute the body*then increment by 1,i=3*go back to the condition(3<=3)??True!*execute the body*then increment by 1, i=4

*go back to the condition(4<=3)??False!So it will stop execution the body of the loop.

1

12

123

The first value for the counterThe condition

The increment or decrement

*First the test is evaluated (1<=3)?? True!* execute the body*then increment by 1, i=2*go back to the condition (2<=3)?? True !*execute the body*then increment by 1,i=3*go back to the condition(3<=3)??True!*execute the body*then increment by 1, i=4

*go back to the condition(4<=3)??False!So it will stop execution the body of the loop.

1 2

3

4

1

2

3

4

1

12

123

Page 7: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

If the condition is wrong, the program will not

Execute the body even once.

Do..while loop:

Loop examples.

The main idea: ask the user to enter a positive number, if he does not continue asking for that until he do so.

Ask yourself:

The first value for the counter?There is no limitation (not v.imp in this question)

The increment or decrement?There is no limitation (not v.imp in this question)

The condition? value<0

(So the easiest way when the question doesn’t tell you how many time you should loop just tell you about the condition is using while loop.)

(We can do it by do ..while but there is small problem)>> كانت ما لو حتى تهريب بتدخل قيمة اول قلت ما مثلللحالة موافقه

The first value for the counter

The condition

The increment or decrement

1

2

3

4

*First i=1 execute the body.

*increment by 1,i=2.

*check the condition (2<=3)??True!

*execute the body

*increment by 1,i=3

*check the condition(3<=3)??True!

*execute the body.

*increment by one,i=4

*check the condition(4<=3)??False!

Stop executing the body!!!

1

12

123

At the first the program will not check the condition so تدخل تقدر قيمة اول يعني بالعربي

^^ يجيك شي اول الباقين عكس على تهريب اللوب !!الحاله

Suppose we want to input a positive integer and want to check that the user has entered a positive integer, re-prompt for, and input if he has not done so. write 3 programs using while and do while .

A solution

اول حق شرطي احطif = قيمة

Page 8: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Switch statement vs. nested if elseSyntax:

Exp1:Use the switch statement instead nested if-else :

Suppose we did not write the breaks:

Should be a constant such as 1, 2,'a','+'

Variable with Integer or character values

If the value of x doesn’t fit any of the cases

If the value of x fit the case break stop the program from checking other cases

:

The program will look forward the case which fit the value of x ohh its 4 then it will execute the statements in this case if it face a break it will stop executing but there is no breaks so it will execute all the statement down till it face a break.

The user enter 5.. program will look forward it oh this is 5 .. but just a minute there is nothing is it an error?? No its not continue ya al 7beeb continue .. it will move to 6 since there is no break there is nothing too ..move to 7 ohh finally there is a statement

execute then break !!

Page 9: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

FunctionsWhy do we need functions?? Suppose that you asked to write a program and you should find the average 10 times for certain amount Are you going to write the average program syntax 10 times!!!!!! Instead of that we can use functions .

Function

User-defined function#include<cmath>functiondouble cos(double x); Cos(30)?? cos(30*3.14/180) it should be in radiandouble sin(double x); sin(30)?? sin(30*3.14/180) it should be in radiandouble tan(double x) tan(30)?? tan(30*3.14/180) it should be in radiandouble atan(double x); atan(30)?? atan(30*3.14/180) it should be in radiandouble asin(double x); asin(30)?? asin(30*3.14/180) it should be in radiandouble acos(double x); acos(30)?? acos(30*3.14/180) it should be in radiandouble exp(double x); Returns e to power of xdouble log(double x); Returns natural logarithm of xdouble ceil(double x); For approximating ceil(2.9)=3,ceil(2.1)=3double floor(double x); For approximating,,floor(2.9)=2,floor(2.1)=2double fabs(double x); Absolute value , fabs(-3)double pow(double pow(2,3)=8

Any thing can be done by swich statement can also be done using a nested if-else but the reverse is not true .

You cannot write cases as condition never do that.

Try this program:

Pre-defined function

User-defined function

Page 10: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

x,double power)double sqrt(double x); sqrt(9.0)=3#include <cstdlib>#include<time> for time(NULL)=time(0)

User-defined function:

There are methods for writing prototype:

Int max(int x,int y) Int max(void) void max(int x,int y) Void max (void)

cin (main)cout (main)

cin (function define)cout (main)

cin (main)cout (function define)

cin (function define)cout (function define)

int rand ():Returns an integer between 0 and RAND_MAX=32767.it generate a sequence of int number.rand() [0,32767] rand%the amount of the numbers+ shiftrand()%100 [0, 99 ] 100 numbers

rand()%100+1 [1,100]

void srand (unsigned int seed)

srand(time(NULL)) here the seed is time(NULL)= computer time , it helps to generate different random numbers each time we run the programe.

If you don’t seed it will be generated the same random number each time we run the program.

+1 +1

Prototype

Prameters

Page 11: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

** void means that there is no return in the function define so Instead of void we write cout.

** we must declare the variable before cin.

Local variable vs. global variable

Suppose that these two cylinder are open from the top: we will use the previous example parameter

Find the outputWe always start from main():Suppose that user enter 3 4.1. Then we have function call move to the function define .2. Open a new memory location for z and a which are local variable for the function define .. and transform the values from x to z and y to a and open memory location for max.3. Move to the if statement check the condition (3<4)??True . so y is the max return the value of y as max so go back to function call which the value of it is 4.

3 4x y

3 4

zy a

4

max

x y z a

Main Function define

Global variable:J

Local variable Local variable

So as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main to function define it cannot see the x,y any more, it's the same as when you're moving from one room to another ,you cannot see the people in the previous room any more.. so how we deal with this problem in writing the function we use prototype which contains the parameters that tells the program that x is the same value of z and y is the same value of a.. what about the global variable J ?? as we mention that the cylinders are open from the top so the program can

Page 12: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Even if we wrote program like this there is no relationship between the b in main or b in the function define:

What if I write it like that?

So as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main to function define it cannot see the x,y any more, it's the same as when you're moving from one room to another ,you cannot see the people in the previous room any more.. so how we deal with this problem in writing the function we use prototype which contains the parameters that tells the program that x is the same value of z and y is the same value of a.. what about the global variable J ?? as we mention that the cylinders are open from the top so the program can

Global variable

local variable

0

a

7

b

a=0b=7

11

b

local variable

1

a=1b=7

*it's important to know that the first b is a local variable for main and the second b is a local variable for function define so there is no relationship between them and the prototype do not says that.

*it's important also to know that a is a global variable so any change in any part of the program can effect on the value of it.

Wow there is something new in prototype &b? that’s one called call by reference here when you move to the function define there is no need to open new memory location for b because here b int main and b in the function define are the same ,it like that one memory locatin with two names b(local variable for main) and b(local variable foe function define)

The first cout is clear ..I will start from function call , program will move to the function define , it will open new memory location called b and from prototype we knows that the value of it, is the same value of b (local variable in the main) which is 7, in the function define the value of b(local variable of function define) has changed to 11 and "a" becomes 1, there is no cout in function define so the function call here does nothing except changing the value of the global variable. When program go back to the main to the second cout the value of b(local variable of main) doesn’t change and remember that b in the main and b in function define have different memory location because we call b by value.

7

b

7

b

0

a

1

11

b main b func

01

a

Page 13: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

ARRAY1D array:

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

A[0] ………………………………………………………………………………………………….. A[size-1]

Q: What's the different between A[0] and A0?

A[0] its element in array but A0 is a variable.

Note: it’s hard to write a program that find you the max and min for 10 variables , you will need 20 if statement or more but it more easier to find the max and min in array.

Array size must be fixed and an integer, index must be an integer, value of array element could be integer or float or character .

Array best friend is for loop so we will use it a lot.

You can write the index as arithmetic expression A[i*2]=0;

How to pass values to an array?

Wow there is something new in prototype &b? that’s one called call by reference here when you move to the function define there is no need to open new memory location for b because here b int main and b in the function define are the same ,it like that one memory locatin with two names b(local variable for main) and b(local variable foe function define)

7

11

type array-name[size];

int A[10];

Page 14: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

1. int A[5]={20,30,40,40,60};

20 30 40 40 60A[0] A[1] A[2] A[3] A[4]

int A[5]={20,30,40};

20 30 40 0 0A[0] A[1] A[2] A[3] A[4]

You can pass less than or equal to the array size values but not greater than the size it will be an error!!

2.

Int A[5];

A[2]=200;

A[0]=-30;

A[5]=60;//THERE IS NO ELEMENT WITH INDEX 5

A[3]=A[2]+A[0];

-30 0 200 170 0A[0] A[1] A[2] A[3] A[4]

3.PASSING VALUES TO ALL ELEMENT

const int size=5;

int A[size];

for(int i=0;i<size;i++)

{

cin>>A[i];

}

ثابته ! هالصيغة

Exp1: write c++ program ,define 1D array name weight of size 10 read the weight of these element and find:

1.the max and min.

By default

*we say that the size is fixed so we add const before the data type.

*the first index is 0 so i=0 at the first the last index is 4 so the for loop well run if i<5 then it will not execute any thing because the last index is size-1.

Page 15: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

2.the sum of all elements.

3.the sum of even index elements.

4.how many odd numbers and even numbers.

Sol:

Read the wight:

const int size=10;

int w[size];

for(int i=0;i<size;i++)

{

cin>>w[i]; }

1.

int max=w[0]; for(int i=0;i<size;i++) { if(w[i]>=max); max=w[i]; } cout<<max; int min=w[0]; for(int i=0;i<size;i++) { if(w[i]<min) min=w[i]; } cout<<min;

2.

int sum=0; for(int i=0;i<size;i++) { sum=sum+A[i]; } cout<<sum;

3. int sum2=0; for(int i=0;i<size;i+=2) {

First we will declare an int called max and its first value is the first element we need for loop to pass all elements in the array so if an element is greater than max which is the same value of the first element let max equal to it.

We will do the same as max but here if the value of element is less than the min let min equal to it.

First declare a variable called sum then use for to pass all the elements and add them together.

Always anf for ever the first value for sum will be 0 and max ,min = ..[0], how ever even index so we will play with the increment to be i=i+2 the same as i+=2

So a[0],a[2],a[4],a[6],a[10]

Page 16: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

sum2=sum2+A[i]; } cout<<sum2;

4.int even=0; int odd=0; for(int i=0;i<size;i++) { if(A[i]%2==0) even++; else odd++; } cout<<even<<endl; cout<<odd<<endl;

if I ask you to write each five values in seprate line ??

for(int i=0;i<size;i++) { cout<<A[i]; if((i+1)%5==0) cout<<endl; }

How to find reverse??

How to find even number even numbers are numbers if you divided it by 2 the remainder well be zero so we used if

statement her.

تحفظها حاول هاي الحسابيه العمليات! عدل حطيتها اللي هالبرامج تفهم وحاول

so the first thing I think about it that I will use mod because it said that after five elements so when I REACH to A[5]

start new line A[10] strat new line so when i%5==0 start new line but we will face a problem which is zero (0%5==0)

that is mean it will cout new line when it reach A[0] too and that’s wrong we will use cheating here we will ignore A[0] so

we will start with i+1 .

So we will output A[9] first i=9; and the last one will be A[0] so i>=0

Page 17: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

for(int i=9;i>=0;i--) { cout<<A[i]<<" "; }

How to pass 1D array to function ??

The way of writing the functions remain the same but the idea here how to pass an array to the function .

How do we write it as a parameter in prototype ?

How do we write it in function call?

First :Passing the all elements of an array (call by reference )

Type namefunction(type ArrayName[ ] , ……)

Namefunction( Arrayname,arraysize)

1

2

1

2

2 3

b[0] b[1]

Call by reference

a[0] a[0]

0sum

2

5

At the first we have an integer called s1 and an array called b with fixed size 2 we will open memory location for two elements b[0]=2,b[1]=3..then we have function call with an array and its size and its important to know that its call by reference >> move to the function define .. we will not open new memory location (call by reference.. go through the fuction define oh finding the sum open new memory location for sum now start with for loop:

Sum= 0+a[0]=0+2=2, sum=2

Sum=2+a[2]=2+3=5,sum=5

Return value of sum to function call then cout s1.

Page 18: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

What about this program?

second :Passing specific elements of an array (call by value )

We will start from function call so we move to the function define don’t forget its call by reference so no need to open new memory location we have foe loop which says add two for each element yalla add two 2+2=4, 3+2=5 so the value of b[0] and b[1] which are another name for a[0] and a[1] because its call by reference has changed no return no cout for function call we will move to cout b[0] and b[1].2 3

b[0] b[1]

a[0] a[1]

4 5

Page 19: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

2D ARRAY:

Exp1:

*We will not go a lot through this but In general when you want to pass a specific elements in array so when you will write the proto type keep the side which you will write the element in function call

empty except from the data type .

*in function call no need for the size but I write it.

**remember that element as a parameter are call by values so when you move tp the function define open new memory location for n1 , n and we need return here to cout the value of the function call.

A[0][0] A[0][2] A[0][3]

A[1][0] A[1][2] A[1][3]

TYPE ArrayName[row size][column size]

int A[3][3];

Page 20: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

1.define 2D array of size 4*4 integer name B.

2.read the element.

3.find and print the largest & smallest element.

4.find the average of all elements in the array.

5.find the average of all elements in row 2.

7. find the sum of all elements in the main diagnol.

In 2D array we have 2 index for row and col .. so we need two for loop the outer loop for rows and the inner loop for columns HOW DO THEY WORK?

1.First we are in the outer loop i=0, 0<4??True!!

2.execute the body ohh there is another loop

j=0 ,check the condition 0<4??True . move to the body.

3. read the value of b[0][0]. Then where should I go to the outer or INNER loop ?? we will stick with inner loop until the condition becomes wrong conditions so we will read b[0][1],b[0][2],b[0][3].

4.then we will move to the outer loop increment I by one i=1,1<4 and go back again to the inner loop so you will read b[1][0],b[1][1],b[1][2],b[1][3].

5. so we are going row by row until we read all

elements.

1,2

Page 21: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Average = sum / number of elements so we need to find the submission of all element first usinf for loop then divided by 16.0 and I write like that because I want a floating point number if I write it 16 or r*c it

will give me the integer only .

So we can use just to for loop to find the max and min and other step exactly the same as 1D array

3

4

Here the question specified the row number so we will keep it as constant and we need one just one loop for the column.

Note always when they specify an index keep it as constant and use loop for the other

5

Page 22: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

StringsWhat if you want to write a full sentence in your program how do you do that ? we have two option either use 1D array or string.

1. assign values (1D array) 4ways:

*char A[10]=”hello”;

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

h e l l \0 ? ? ? ? ?

*Or char A[10]={’h’,’e’,’l’,’l’,’o’,’\0’};

* or we can use cin to assign values to array too.

Main diagnol i=j

6

V.IMP INDICATE THAT THE STRING END

WE OFTEN USE LARGE SIZE AND YOU CAN KEEP IT EMPTY

Page 23: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

cin>>arrayName;

char A[12];

cin>>A;

NOTE: array from charachtr type differs from array from integer type integer type we were using for loop to assign values to the array here there is no need .

*But for several reason one of them is that cin doesn’t consider spaces as character cin.get line() is more useful in strings.

cin.getline(ArrayName,size)

char A[12];

cin.getline(A,12);

1.assign values strings 4ways:*

For declaring variable we often do:

Data type variable;

Here the data type is (string)

string A;

string=”hi”;

or

string A=”hi”

or

string A (“hi”);

or

cin>>A;

Page 24: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

2.1D array some functions:The C-string library contains many C-string handling functions. If you use any of these then you must have the following preprocessor directive in your program.

#include <cstring>•The 4 most used functions in the c-string library are –strcpy, –strcat, –strcmp –strlen. * char A[12]=” hello”;

char B[13]=”hi”;

cout<< strcpy(A,B);

*char A[12]=” hello”;

char B[13]=”hi”;

cout<<strcat(A,B);

* char A[12]=” hello”;

char B[13]=”hi”;

cout<<strcmp(A,B);

* char A[12]=” hello”;

char B[13]=”hi”;

cout<<strlen(A);

strcpy( array 1,array 2)>> it copy the value of B to A so A will be the same as B ,B remain the same and the out put will be the NEW VALUE of A.

The output: hi

Strcat(array1,array 2)>>it put the value of B to the end of the value of A

, B remain the same and output will be the NEW VALUE of A.

The out put: hellohi

Strcmp(array1,array 2)>> it comper the values charchter by character if the first character are same then moves to the second and so on ,if the two arrays have the same values so it will output 0, if the compiler reach to different character between A,B and the character in A GREATER than the character in B it eill cout npositive number and if it was LESS than B it eill cout negative number and her e is less than I so the output will be negative.

strlen(array)>> it indicate to the length

the output: 5

Page 25: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

2 string:*instead of strcpy:

string A="hello"; string B="hi"; A=B; cout<<A; cout<<B;*instead of strcat: string A="hello"; string B="hi"; string c; c=A+B; cout<<c;*instead of strcmp:string A="hello"; string B="hi"; if(A>B) cout<<+1; else if(A<B) cout<<-1; else cout<<0;*instead of strlen: string A="hello"; string B="hi"; cout<<A.size();

Write a program to find for searching certain character in a string:

Page 26: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Write a program that count how many times certain character appear:

Recursion

Her we use cin.getline to read the string and it’s much better because it consider spaces .. then we initialize a variable found with value 0.. we’ve used for loop to pass all elements to search about character s if its found so the value of found become 1 (we use found as assistance instead of writing found each time the character appear ) so if the character is fount the value of found become 1 and if it’s one cout found

Instead of found we’ve initialize counter called count always the first value of counter is 0; then we use for loop to pass all elements if s is found increment counter by one then cout counter.

Page 27: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Special case of the function ,the function call it self.

We can write the previous program without recursion:

Write a c++ program that reads two integers b,e & compute be.

In recursion its important to have a stop point which depend in math . in this example the stop point is n=1.

We will start from fuction call considering that the value of x=4 then will move to function define ,open new memory location for n=4,then there is if statement is n==0 ?false is n==1?false,move to else we have return ohh another function call the function call it self how we deal with this? Here it says that

4!= 4 *3! n==0 ?false,,n==1?false

3!= 3 *2! n==0 ?false,,n==1?false

2!= 2 *1! n==0 ?false,,n==1?true

1

4=!4*3*2*1=24

The output =24;

In recursion its important to have a stop point which depend in math . in this example the stop point is e=1.

We will start from fuction call considering that the value of b=2,e=3

Page 28: · Web viewSo as it shown x,y are local variables in main function and z,a are local variables in function define, its important to know that when the program moves from function main

Write the program without using a function:

In recursion its important to have a stop point which depend in math . in this example the stop point is e=1.

We will start from fuction call considering that the value of b=2,e=3