CPP Sol Week04

download CPP Sol Week04

of 9

Transcript of CPP Sol Week04

  • 8/21/2019 CPP Sol Week04

    1/20

    A ril 2015 

    ()

     

    FunctionsValue-Returning

    ئا را حا

       ن اا ا  رد أ   م

     

     

     

     رن

     

    خآ ص يأ  س.

     ا ت++

    ن ات  ة .اء ل ء  أأ

     ا ت++

     ضاا

     ح

     

     ي

      ئا ر أ تا تر.

     ا ت++

    ا  حا ت  ت

     ئارا

    ضإ أ ر.

     C++ Programming

    Week 4

    ذا   اث 

         تا ار   eng-hs.netأع

    .  ائ اار اث

  • 8/21/2019 CPP Sol Week04

    2/20

    A ril 2015 

    (2)

     

    Functions (Introduction ) 

    Write a program that reads 2 integers and prints the sum of integers in

    between inclusively (without using functions).

    # include

    using namespace std;

    int main ( )

    {int x, y, i, min, max, sum = 0;

    cout > x >> y;

    if (x < y)

    { min = x; max = y; }

    else

    { min = y; max = x; }

    for (i = min ; i

  • 8/21/2019 CPP Sol Week04

    3/20

    A ril 2015 

    (3)

     

    اا (ا)

    اا 

    اا

     اء

    اا

     ت

    ا اخري

      لا ءاmain

    اا  ةئا ا

    أ (x, y)اء  س اء (s)(sum)(main)

     

    Functions (Introduction )

    Write a program that reads 2 integers and calls a function that gets these

    two integers and returns the sum of integers in between.

    The main function should print the result.

    # include using namespace std;

    int fun (int, int);

    int main ( )

    {

    int x, y, sum;

    cout > x >> y;

    sum = fun (x, y);

    cout

  • 8/21/2019 CPP Sol Week04

    4/20

    A ril 2015 

    (4)

     

    Functions (Power ) 

    Write a program that reads two positive integers, and call a function that

    returns the power of the first integer raised to the second integer.

    Note: you are not allowed to use  library functions.

    # include

    using namespace std;

    int power (int, int);

    int main ( )

    {

    int x, y, p;

    cout > x >> y;

     p = power (x , y);

    cout

  • 8/21/2019 CPP Sol Week04

    5/20

    A ril 2015 

    (5)

     

     ج  اط

     

    Functions (Middle)

    Write a program that reads three integers and calls a function to find their

    middle. The main function will print the result.

    # include

    using namespace std;

    int middle (int, int, int);

    int main ( )

    {

    int x, y, z, mid;

    cout > x >> y >> z;

    mid = middle (x, y, z);

    cout

  • 8/21/2019 CPP Sol Week04

    6/20

    A ril 2015 

    (6)

     

    Functions (Even  

    or odd? ) 

    Write a program that reads an integer and calls a function that indicates if

    the integer is even or odd, the main function will print the result.

    # include

    using namespace std; 

    bool Is_Odd (int);

    int main ( )

    {

    int x; bool y;

    cout > x;

    y = Is_Odd (x);

    if (y == true)

    cout

  • 8/21/2019 CPP Sol Week04

    7/20

    A ril 2015 

    (7)

     

     factorial 

     

    Functions (Factorial ) 

    Write a program that reads a positive integer and calls a function that

    calculates its factorial. The main function will print the result.

    # include using namespace std;

    int fact (int x)

    { int i, f = 1;

    for (i = 1; i x;

    if (x < 0) 

    cout

  • 8/21/2019 CPP Sol Week04

    8/20

    A ril 2015 

    (8)

     

    )ign S - Reverse unctions (F 

    Write a program that reads a number and calls a function that reverses the

    sign of the number received according to the following sample outputs. The

    main function will print the result.

    # include

    using namespace std;

    double reverse (double x)

    {

    return x * -1;

    int main ( )

    {double x;

    cout > x;

    if (x == 0)

    cout

  • 8/21/2019 CPP Sol Week04

    9/20

    A ril 2015 

    (9)

     

     ة

     

    Functions (Perfect ) 

    Write a program that reads a positive integer and calls a function that

    checks if it is perfect or not. The main function will print the result.

    # include  

    using namespace std;

    bool perfect (int);

    int main ( )

    {

    int x;

     bool y;

    cout > x;

    y = perfect (x);

    if (y == true)

    cout

  • 8/21/2019 CPP Sol Week04

    10/20

    A ril 2015 

    (1)

     

       أ  ا  ر 

     

    Functions (Prime) 

    Write a program that reads a positive integer and calls a function that

    checks if it is prime or not. The main function will print the result.

    # include using namespace std;

    bool prime (int); 

    int main ( )

    {

    int x;

    cout > x;

    if (x

  • 8/21/2019 CPP Sol Week04

    11/20

    A ril 2015 

    ()

     

    Functions (Sum x-to-y ) 

    Write a function that gets three integers and returns the sum of all integers

    between the first two integers that are divisible by the third one inclusively.

    If the integers are: 14 3 5 , the function should return 15  If the integers are: 15 21 7 , the function should return 21  

    If the integers are: 12 21 11 , the function should return 0  

    int divisible (int x, int y, int z)

    {

    int i, min, max, sum = 0;

    if (x < y)

    { min = x; max = y; } 

    else

    { min = y; max = x; } 

    for (i = min; i

  • 8/21/2019 CPP Sol Week04

    12/20

    A ril 2015 

    (2)

     

    Functions (Contains  

    odd? )

    Write a function that gets an integer and returns true if any of its digits is

    odd.

    If the integer is 1236, the function will return true .If the integer is 284 , the function will return false . 

    bool digits (int x) 

    {

    int d;

    while (x != 0)

    {d = x % 10;

    if (d % 2 != 0)

    return true;

    x /= 10;}

    return false;

    }

    أ إز  أن ن اص

     اي

     ل

     

     

     ه

    .اص اي  ه

     ن

     اأإذا

     خت

    oddاا

     ن

      را  true.

    : اخر  اخ او

    if (x % 2 != 0)

    return true;

    x /= 10;} 

  • 8/21/2019 CPP Sol Week04

    13/20

    A ril 2015 

    (3)

     

    Functions (Average of even digits )

    Write a function that gets a positive integer and returns the average of the

    even digits it contains.

    If the integer is 284, the function will return 4.66667  If the integer is 1236, the function will return 4

    double digits (int x)

    {

    int i, d, count = 0, sum = 0;

    while (x != 0)

    {d = x % 10;

    if (d % 2 == 0)

    {

    sum += d;

    count ++;

    }

    x /= 10;

    }

    if (count == 0)

    return 0;

    return sum * 1.0 / count;

    }

     أ

     ن

     

     غ

     أ

     أ

     خأ

     ص

     نأي

     

     نأ  ء أ ن.

       اأي إاء   ي   ما نأ.

     اع

     

     

     أ

     إذا

    double. 

    (x/10) ر ي   

    ا

     إ

    .  

  • 8/21/2019 CPP Sol Week04

    14/20

    A ril 2015 

    (4)

     

    Functions (M ir ror of on integer )

    Write a function that gets a positive integer and returns its mirror.

    If the integer is 1236, the function will return 6321  If the integer is 284, the function will return  482  

    int mirror (int x)

    {

    int d, m = 0;

    while (x != 0)

    {

    d = x % 10;

    m = m * 10 + d;

    x /= 10;

    }

    return m;

    }

    ش

     

     ا

     ن

     ا ةا.

      ر ض اذإ(10)زأ .خ ار خ إ ار

     ر  أر أ نأر ان   ة

     ا ف ل. 

  • 8/21/2019 CPP Sol Week04

    15/20

    A ril 2015 

    (5)

     

    Functions (gcd ) 

    Write a function that gets two positive integers and returns their greatest

    common divisor.

    If the integers are: 12   36 , the function will return 12  If the integers are: 24   18, the function will return 6  

    If the integers are: 15   7, the function will return  1  

    int gcd (int x, int y)

    {

    int i, min;

    if (x < y)

    min = x;

    else

    min = y;

    for (i = min; i >= 1; i--)

    if (x % i == 0 && y % i == 0)

    return i;

    }

    آ نخا   ر ز     

     

     

     آ

     أ

    .

    ا كا ا  ان

     

     

     أ

     

     اق ن.

    ا  ك   اذإ اا د(1.)

     

  • 8/21/2019 CPP Sol Week04

    16/20

    A ril 2015 

    (6)

     

     اء whileأ مد د غ مرأ ة.

     ة أloopخاد ةـاloop.}(2){

     

    Exercise: An application of function floor is rounding a value to the nearest

    integer, The statement

    y = floor ( x + 0.5 );

    rounds the number x to the nearest integer and assigns the result to y. 

    Write a program that reads several numbers and uses the preceding

    statement to round each of these numbers to the nearest integer.(Hint: use functions).

    # include

    # include  

    using namespace std;

    int main ( ){

    double x, y;

    cout > x;

    while ( x != -1 )

    { y = floor ( x + 0.5 );

    cout

  • 8/21/2019 CPP Sol Week04

    17/20

    A ril 2015 

    (7)

     

    Exercise:  Define a function hypotenuse   that calculates the length of the

    hypotenuse of a right triangle when the other two sides are given.

    Use this function in a program to determine the length of the hypotenuse for

    each of the triangles below:

    side1 = 3.0 side2 = 4.0

    side1 = 5.0 side2 = 12.0side1 = 8.0 side2 = 15.0

    # include

    # include

    # include

    using namespace std;

    double hyp ( double, double );

    int main ( )

    cout

  • 8/21/2019 CPP Sol Week04

    18/20

    A ril 2015 

    (8)

     

    Exercise: Write a function that takes a number and a character, and prints

    a solid square of that character with side equal to the number. For example,

    if 4 and # are read, the following square is printed.

    ####

    ####

    ########

    # include  

    using namespace std;

    void square ( int, char );

    int main ( )

    int size;

    char c;

    cout > size;

    cout > c;

    square ( size, c );

    return 0;

    void square ( int size, char c )

    {for ( int i = 1; i

  • 8/21/2019 CPP Sol Week04

    19/20

    A ril 2015 

    (9)

     

    Exercise: Write a program that inputs three double numbers and passes

    them to a function that returns the smallest number.

    # include  

    using namespace std;

    double smallest ( double, double, double );

    int main ( )

    double x, y, z, min;

    cout > x >> y >> z;

    min = smallest ( x, y, z );

    cout

  • 8/21/2019 CPP Sol Week04

    20/20

    A ril 2015 

    (21)

    Exercise: Write a function distance  that calculates the distance between two

    points (x1, y1) and (x2, y2). All numbers and return values are of type

    double.

    # include # include

    # include  

    using namespace std;

    double distance ( double, double, double, double );

    int main ( )

    double x1, x2, y1, y2, d;

    cout > x1 >> y1;

    cout > x2 >> y2;

    d = distance (x1, y1, x2, y2 );

    cout