13
The Name Of Allah C++ Course Lesson 2 Arrays: We was talking about variables and its benefit to us but now we will try to make a program make this operation: 1 * 2 = 2 1* 3 = 3 And loop it till 1 * 20. And I want to save my results to use it again in anther operation how we will make it??. May be some of us will think that we need to declare 20 variables to make it..it is correct but imagine that I will this operation till 1000000 what will the solution??? Will you declare this number??noooo. So they decide to make a thing look like a close which we use to save our clothes to save this values and reuse it again in any time..this called " Array". And We declare it as: int x[2] ; look it is an array has 2 elements or like 2 variables or int. and we access it as: x[0] = 0 ; x[1] = 2 ; you can see that we begin with 0 in the coding not with 1 and the number of element which array could save equal the number which written within the square prickets but begin with zero so we can't access the last element.

C++ Course - Lesson 2

Embed Size (px)

Citation preview

The Name Of Allah

C++ Course

Lesson 2

Arrays:

We was talking about variables and its benefit to us but now we will

try to make a program make this operation:

1 * 2 = 2

1* 3 = 3

And loop it till 1 * 20.

And I want to save my results to use it again in anther operation how

we will make it??.

May be some of us will think that we need to declare 20 variables to

make it…..it is correct but imagine that I will this operation till 1000000

what will the solution??? Will you declare this number??noooo.

So they decide to make a thing look like a close which we use to save

our clothes to save this values and reuse it again in any time…..this

called " Array".

And We declare it as:

int x[2] ; look it is an array has 2 elements or like 2 variables or int.

and we access it as:

x[0] = 0 ;

x[1] = 2 ;

you can see that we begin with 0 in the coding not with 1 and the

number of element which array could save equal the number which

written within the square prickets but begin with zero so we can't

access the last element.

For Example:

X[3] has 3 element x[0] , x[1] , x[2] ,……..we can't access x[3].

And arrays could be declared as any data type float , double ,

char…….char??.

Yes …if you can declare char ch = 'K' ; and char ch2 = 'H' ……..and till

reach that 6 variables to show "KHALED" so I can declare an array of

char as:

Char x[6] ;

X[0] = 'K' ;

X[1] = 'H';

X[2] = 'A';

X[3] = 'L' ;

X[4] = 'E' ;

X[5] = 'D' ;

And I tried this code

for(int I = 0 ; I < 6 ; i++)

{

cout << x[i] ;

}

It will show KHALED without declaring 6 variables.

And think that the shape of array as it:

X[0] x[1] x[2] x[3] x[4] x[5]

'K' 'H' 'A' 'L' 'E' 'D'

And you can think that we can make every element of this array is

anther array Like Matrix:

0 1 2

0

1

Look To this shape in first it may be x[3] for 0 , 1 , 2.

And every element is an array of 2

As: x[0] has 2 element so we will declare the 2 dimension as:

int x[n][m];

and m , n is integers. If m = 3 , n = 2 so it will be:

if x[0][0] = 5;

x[1][2] = 6;

5

6

And I can initialize the array by more than one way first if its capacity is

small we can make it:

Int x[5] = {1 , 2 , 3 , 4 , 5};

OR

Int x[] = {1 ,2 , 3 ,4 ,5};

And we can put any value within the square prickets as if we want to

declare an array that the value of first element equals the value of the

second element we will write it as:

Int x[4];

X[0] = 1 ;

X[1] = x[0] ;

1 3 5

2 4 6

OR X[X[0]] = X[0] ;

We will solve a simple problem with us:

Assume that we need program save the even numbers from 2 to 20.

The Solution:

1 #include <iostream>

2 using namespace std ;

3 int main()

4 {

5 int x[10];

6 x[0] = 2 ;

7 for(int i = 1 ; i < 20 ; i++)

8 {

9 x[i] = x[i-1]+2 ;

10 }

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

12 {

13 cout <<x[i]<<endl;

14 }

15 system("pause");

16 return 0 ;

17 }

in line 5 we declared the array and in line 6 we initialize the

first element with 2 and in line 9 in loop every element from 1

will equal the value of its pre-element plus 2 as x[1] = x[0]+2

as x[0] = 2 so x[1] = 2+2 4 and try this for all elements.

Functions:

Do You Remember that in last lesson I said that there is a

shape of code and I will explain it as int main…..?

What was it mean??.

It is function the program compile it when we call it but main

is the body of the code and the compiler it use it to see what it

will do and in main the program will know the functions which

it will call. We made it to make Sami-tools I use it when I need

all of us who made last assignment make a simple interface

…it was choices for the user imagine the you can make a

function or method with any name like InterFace(); and when

you call it as InterFace(); it will show your Interface without

write it again can you imagine???

We will make it now but first we must know that any Function

need data type and if you want to know why ??Because at

end of working of function may be I return a value …say I will

make a function that calculate the Factorial of any number

It will take the number and make its calculations and finally it

should return the result of it and the data type of this result

will be the data type of the Function …..but if You make a

function just show your interface what will be its data type??

It will be void.

That void used to non returned value.

There are 2 types of functions:

1: Take Parameters.

2: Not Need Parameters.

Parameter: is the value which Function need to do its work as

in last example Factorial Code…the Number which we will

calculate it factorial say the Function Name Will be Fact and

we will passing the parameter as Fact(5);

And Now we will try to coding the Function which not need

parameters. But before it we must say when we will write the

code of function it will be before the main or declare it before

the main and write its definition after the main and I will

explain it in this example:

#include <iostream>

using namespace std ;

void InterFace()

{

cout << "\t\t\tThe Name Of Allah\n";

}

int main()

{

InterFace();

system("pause");

return 0 ;

}

it was small example for making a Function Show "The Name Of

Allah".

We make it before the main but we can make it after the main as:

#include <iostream>

using namespace std ;

void InterFace();

int main()

{

InterFace();

system("pause");

return 0 ;

}

void InterFace()

{

cout << "\t\t\tThe Name Of Allah\n";

}

It is the same example but I declare the function InterFace() before

main and define it after main. It was the same but it is called

Coding Style.

Second Kindwith parameters:

If you need to make Function take 2 numbers and multiply it and

return the result but imagine when I passing a parameter to

function as I will give you some fruits in your hand it will be

impossible to eat all it in the same moment so you need to put it in

something and take from it one by one or as you Like.

So in function we will declare variables to put the parameters in it

so we will try to code the function which I explain :

#include <iostream>

using namespace std ;

int Multipling(int x , int y)

{

int z ;

z = x * y ;

return z ;

}

int main()

{

cout <<"The Result : "<<Multipling( 5 , 6)<<endl;

system("pause");

return 0 ;

}

As you see int Multipling(int x , int y)

Because we need to passing 2 parameters to the function so I

declared x and y all its mission is take and save the passed values

from main.

return z ;

I will return the result and the data type of the returned variable

will be the data type of the function so the data type of function will

be int. and if the function return value so it enable me to do it:

int x = Multipling( 5 , 6);

It means that the program will calculate the result of his function

and put the result in x.

The number of parameters is not limited so I can make function all

its mission it take 3 variables and multiply its and return the result

of it. It will be:

#include <iostream>

using namespace std ;

int Multipling(int x , int y , int w)

{

int z ;

z = x * y * w;

return z ;

}

int main()

{

cout <<"The Result : "<<Multipling( 5 , 6 , 7)<<endl;

system("pause");

return 0 ;

}

Let us solve this problem with us :

Try to write a function take an array and Sum all its elements

and return the result.

The solution:

We must know how we will pass an array as parameter it will

be as it:

If I have int x[5]= {1 , 2 , 3 ,4 , 5}; and I want to pass it to

function so I will declare the function as int Fun(int ar[] , int

size); and I will pass the array as:

Fun(x , 5); and I will show it in thins code:

#include <iostream>

using namespace std ;

int Multipling(int ar[] , int size)

{ int Result = 1 ;

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

{

Result *= ar[i];

}

return Result ;

}

int main()

{

int x[5] = {1 ,2 , 3 , 4 ,5};

cout <<"The Result : "<<Multipling(x , 5)<<endl;

system("pause");

return 0 ;

}

As we see in the declaration of the function it take an array and its

size as parameters and attention it is very important to pass this

parameter according to its order in declaration in the function and

after it we write a for loop to loop on the array element by element

to multiply every element with the next element to find the result of

multiplying all elements as: 1* 2 * 3 * 4 * 5

It will be by using operator *= as we explain it in the last lesson and

initialize Result with one is very important because in every time I

multiply the value of result with the anther elements if it was equal

zero of any value from ram so it will result an error in its calculation

so we make it equal 1 and it will not effect on the final result.

And you can call any function within anther function such as this

code:

#include <iostream>

using namespace std ;

void InterFace()

{

cout <<"\t\t\tThe Name Of Alla"<<endl<<"\t\t\tLast Example In Lesson 2\n";

}

int Multipling(int ar[] , int size)

{

InterFace();

int Result = 1 ;

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

{

Result *= ar[i];

}

return Result ;

}

int main()

{

int x[5] = {1 ,2 , 3 , 4 ,5};

cout <<"The Result : "<<Multipling(x , 5)<<endl;

system("pause");

return 0 ;

}

The Assignment:

1:Write function Calculate the result of summation of

numbers from 1 to 1000.

2:Write Function Find The Factorial of any number that

user will Enter it

Hint: the factorial of 3 is equal 3*2*1

Factorial 6 is 6*5*4*3*2*1.

Don't Search and Send To Me Any Code From Web……I

Will Know It Easily.

3:Try To Write Function Find The Power of Any Number

that:

2 power 3 = 8 as 2*2*2.

I multiply the number with its self as the number of power.

The Dead Line Of This Ass is 14 / 7/ 2010 12 AM….I will

Not Accept Or Replay For Any One Will Not Send The

Assignment .

Send it to:

[email protected]

GOOD LUCK

Eng Khaled