find the exact output - موقع المهندس حماده شعبانhs engineers( مكتانق...

18
( شرح فيديو مجانا( د على قناتكمعديد من الموال ل) HS Engineers ) . حمادة شعبان م260 4444 9 hs.com - g en info@ متوفرةات النوت مجانا بالموقعينnet . hs - eng , com . hs - eng (random function) find the exact output: x = rand ( ); x = rand ( ) % 11; x = 1 + rand ( ) % 100; x = rand ( ) % 50 - 50; x = rand ( ) % 30 - 40; x = rand ( ) % 6 + 1; (1 KD) write down a random statement that simulates sum of rolling of 2 dices: choose one answer: 1) sum = 2 * ( 1 + rand ( ) % 6 ); 2) sum = 2 + 2 * rand ( ) % 6; 3) sum = 2 + rand ( ) % 11; 4) sum = 2 + rand ( ) % 6 + rand ( ) % 6; 5) All are correct. 0 . . . 32767

Upload: vuongliem

Post on 17-May-2018

387 views

Category:

Documents


6 download

TRANSCRIPT

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

(random function) find the exact output:

x = rand ( );

x = rand ( ) % 11;

x = 1 + rand ( ) % 100;

x = rand ( ) % 50 - 50;

x = rand ( ) % 30 - 40;

x = rand ( ) % 6 + 1;

(1 KD)

write down a random statement that

simulates sum of rolling of 2 dices:

choose one answer:

1) sum = 2 * ( 1 + rand ( ) % 6 );

2) sum = 2 + 2 * rand ( ) % 6;

3) sum = 2 + rand ( ) % 11;

4) sum = 2 + rand ( ) % 6 + rand ( ) % 6;

5) All are correct.

0 . . . 32767

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

(random function)

write the equivalent random equation: (1 KD)

x = 10 . . 100

x = -20 . . 20

x = -10 . . 0

x = 100 . . 300

study the following program: seed = 1 seed = 70 seed = time

int main ( )

{

int x;

// srand ( 70 );

// srand ( time (NULL) );

for (int i = 1; i <= 10; i++)

{

x = 1 + rand ( ) % 6;

cout << x << endl;

}

return 0;

}

6

6

5

5

6

5

1

1

5

3

4

1

3

6

2

3

2

3

2

6

chan

ges

acc

ord

ing

to

curr

ent

tim

e

x = 10 + rand ( ) % 91;

إذا لمممض أمممخ ة ممما ك،ممم ل ممم

فستصبح جزءا من ك،ط غ رك.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

(random function)

write a program that rolls two dies (n times) and print

how many sevens encountered as the sum of the two

dies (2 KD)

# include <iostream>

# include <cstdlib >

# include <ctime >

using namespace std;

int main ( )

{

ةريد ةن ةكون كل م

يمكنني ةن ةكو ه.

enter number of rolls: 1000

number of sevens = 269

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

void functions (call by value & call by reference)

study the difference between the following two programs:

int main ( )

{

int x = 5;

cout << x << endl; x

change (x);

x += 2;

cout << x << endl;

change (x);

cout << x << endl;

return 0;

}

void change (int x) x

{

x += 3;

cout << x << endl;

}

int main ( )

{

int x = 5; x

cout << x << endl;

change (x);

x += 2;

cout << x << endl;

change (x);

cout << x << endl;

return 0;

}

void change (int &x) x

{

x += 3;

cout << x << endl;

}

ةفأل إ ج ز هو ةن كون الشخص

الذي ريده في ع لض ي ول جعل

الشخص الذي ال ريده.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

void functions (call by value & call by reference)

find the exact output of the following:

int main ( ) (1 KD)

{

int a = 5, b = 7;

fun (a, b);

cout << "total = " << b + a << endl;

return 0; a b

5 7

}

void fun (int a, int &b)

{

int c = a;

a = b;

cout << "total = " << a + b << endl;

b = c;

}

ة مما غ ممر م، لممن لتكممون ةفأممل مممن

ةي شممخص ةكممر ة مما م، لممن مم ن

كون ةفأل شيء يمكن ةن كو ه.

a b c

5 7

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

void functions - call by reference (power)

write a program that reads two integers and call a void

function power that computes the power of first integer

raised to the second one.

The main function will print the result.

# include <iostream>

using namespace std;

int main ( )

{

enter two integers: 3 4

power is: 81

ح ن الوقا لكي ع ش

ال ة التي خ لن ه .

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

void functions - call by reference (total-average)

write a program that reads three integers and call a

function calc that computes their total and average.

The main function will print the results (2 KD)

# include <iostream>

using namespace std;

int main ( )

{

enter three integers: 3 7 9

total: 19

average: 6.33333

ر مممم لمممض يعلمممآ الكمممرون مممم الا

عريأممم علمممي ولكمممن مممم زلممما

عريأ على فسي. ةعلآ م الا

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

functions (local-global-static-scope)

find the exact output of the following:

int x = 10; x

int main ( )

{

int x = 4;

cout << x << endl; x

{

int x = 7;

cout << x << endl;

x *= x;

} x

cout << x << endl;

fun1 ( );

fun2 ( );

fun3 ( );

fun1 ( );

fun2 ( );

fun3 ( );

cout << x << endl;

return 0;

}

void fun1 ( )

{

static int x = 30; x

cout << x-- << endl;

}

void fun2 ( )

{

int x = 23;

cout << x++ << endl; x

}

void fun3 ( )

{

cout << ++x << endl;

}

قممدرا ن م ممل ةراجمم تمتممخ

عشر سرع ت لكن ةغلبنم

يركن إلى السرع األقل.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

functions (local-global-static-scope output)

find the exact output of the following:

int x = 3; (3 KD) x

3

int main ( )

{

int y = 1; y

1

while (x < 7)

{

fun (x, y++);

cout << "total = " << x + y << endl;

}

return 0;

}

void fun (int &a, int b)

{ a b i k

static int i = 0 ;

i += 1;

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

a += fun_fun (b);

}

int fun_fun (int y) y

{

return x - y;

} ال يعدم الف شل اكتالق ةلف

عذر ل برر ه فشله.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

functions (local-global-static-scope output)

find the exact output of the following: (2 KD)

int num, sum = 10; num sum

sum 10

int main ( ) 0

{

cout << "enter 10 values:\n";

{

int sum = 0;

fun (sum);

cout << "sum is: " << sum << endl;

}

fun (sum);

cout << "sum is: " << sum << endl;

return 0;

}

a count

void fun (int &a)

{

static int count = 0 ;

while (count < 5)

{

++ count;

cin >> num;

a += num;

}

count --;

}

enter 10 values:

5 10 3 12 3 3 4 5 8 3

قسض ةي مهم صعب إلى مه م ةصغر

ةسهل فإن الجب ل تكون من ال ص .

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

recursion (factorial)

write a program that reads a positive integer and calls

a recursive function that calculates its factorial. the

main function will print the result. (2 KD)

# include <iostream>

using namespace std;

int rfact (int);

int main ( )

{

int x;

cout << "enter a positive integer: ";

cin >> x;

if (x < 0)

cout << "invalid number\n";

else

cout << "factorial is: " << rfact (x) << endl;

return 0;

}

enter a positive integer: -3

invalid number

enter a positive integer: 4

factorial is: 24

لتكن ة ا ةهض ند في

ق ئم اهتم م .

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

recursion (power)

write a program that reads two positive integers

and calls a recursive function that returns the

power of the first integer raised to the second.

note: you are not allowed to use cmath library

(2 KD)

# include <iostream>

using namespace std;

int main ( )

{

int x, y;

cout << "enter two positive integers: ";

cin >> x >> y;

cout << "power is: " << rpower (x, y) << endl;

return 0;

}

enter two positive integers: 3 4

power is: 81

enter two positive integers: 4 -2

invalid power

ل شيء في هذا الع لض

فه لت صل عل ه.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

recursion (fibonacci)

write a program that reads a positive integer and calls a

recursive function that calculates its fibonacci.

the main function will print the result. (1 KD)

# include <iostream>

using namespace std;

int fibon (int);

int main ( )

{

int x;

cout << "enter an integer: ";

cin >> x;

cout << "fibonacci is: " << fibon (x) << endl;

return 0;

}

enter a positive integer: 7

fibonacci is: 13

ةم كن هن ك ةائم ا

ش غرة على القم .

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

recursion (print-digits)

write a recursive function that gets a positive integer

and prints each of its digits on a separate line. (2 KD)

if the integer is: 1236 the function will print:

6

3

2

1

يسهل عل ن إةراك الفرص

عندم فلا من ن ةيدين .

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

recursion (maximum-digit)

write a program that reads a positive integer and calls

a recursive function that returns the maximum digit

that integer contains.

the main function will print the result. (3 KD)

# include <iostream>

using namespace std;

int maxdigit (int);

int main ( )

{

int n;

cout << "enter a positive integer: ";

cin >> n;

cout << "maximum is: " << maxdigit (n) << endl;

return 0;

}

enter a positive integer: 5837

maximum is: 8

إذا عرفا ةطلن ةين ر

ال ةحد يخ،ط للفشل ي ي

ندم ال خ،ط.الفشل ع

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

Exercise: Write a program that simulates coin tossing.

Let the program toss the coin 100 times and count the

number of times each side of the coin appears. Print

the results. The program should call a separate

function flip that takes no arguments and returns false

for tails and true for heads.

# include <iostream>

# include <cstdlib>

# include <ctime>

using namespace std;

bool flip ( );

int main ( ) {

الك ممر ممممن فشمملوا لممض يممدركوا مممد

قر هض من النج ح عندم استسلموا.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

Exercise: Write a program that helps an elementary school

student learn multiplication. Use rand to produce two

positive one-digit integers. It should then type a question

such as

How much is 6 times 7?

The student then types the answer. Your program checks the

student’s answer. If it is correct, print ''very good!". If the

answer is wrong, print ''No. Please try again", until the

student finally gets it right.

# include <iostream>

# include <cstdlib>

# include <ctime>

using namespace std;

bool check ( int, int, int );

int main ( ) {

النج ح مل لمن يدفخ ال من.

(HS Engineers( للعديد من المواد على قناتكم )مجاناشرح فيديو )

net.hs-eng ,com.hs-eng بالموقعين مجانا النوتات متوفرة @hs.com-geninfo 26044449 م. حمادة شعبان

Exercise: Write a recursive function gcd that returns

the greatest common divisor of x and y.

# include <iostream>

using namespace std;

int gcd ( int, int );

int main ( )

{

إن ةي شكل من ةشك ل التخ، ط ةفأل

من عدم التخ، ط على اإلطالق.