Vous êtes sur la page 1sur 12

Information Science

and Technology
St. Paul the Apostle -
Ohrid,Republic of Macedonia

Programming II

Tutorials

Functions

University for Information Science and Technology

Associate Professor Ustijana Rechkoska-Shikoska, Ph.D.


Exercise 1 - Returning an integer reversed
Write the following function to return an integer reversed:

For example reverse(3456), returns 6543.

Solution:
Exercise 2 - Summing series
Write a function to compute the following series:

()

Write a test program that displays the following table:


i m(i)
1 0.5
2 1.166
7…
19 16.4023
20 17.3546
Solution
Exercise 3 - Using the sqrt function
Write a program that prints the following table using the

Number SquareRoot

0 0.0000

2 1.4142

18 4.2426

20 4.4721

Solution:
Exercise 4 - Area of a pentagon
The area of a pentagon can be computed using the following formula:

( )

Solution

Exercise 5 - Kinetic energy


• In physics, an object that is in motion is said to have kinetic energy. The
following formula can be used to determine a moving object’s kinetic energy:

KE = mv2
• Write a function named kineticEnergy that accepts an object’s mass (in
kilograms) and velocity (in meters per second) as arguments. The function should
return the amount of kinetic energy that the object has. Demonstrate the
function by calling it in a program that ask the user to enter values for mass and
velocity.

Solution
Exercise 6
What is pass-by-value? - When you invoke a function with a parameter ,the
value of the argument is passed to the parameter. This is referred to as pass-by-
value. If the argument is a variable rather than a literal value, the value of the
variable is passed to the parameter. The variable is not affected, regardless of the
changes made to the parameter inside the function.

What is pass-by-reference? - You can use a reference variable as a parameter in a


function and pass a regular variable to invoke the function. The parameter
becomes an alias for the original variable. This is known as pass-by-reference.
When you change the value through the reference variable, the original value is
actually changed.

Show the result of the following programs.


Exercise 7
The two roots of a quadratic equation ax2+bx+c=0 can be obtained by using the
following formula:

Write a function with the following header


void solveQuadraticEquation( double a, double b, double c,
double&discriminant, double &r1, double&r2)

b2-4ac is called discriminant of the quadratic equation. If the discriminant is less


than 0, the equation has no roots. In this case we ignore the value r1 and r2.

Write a test program that prompts the user to enter values for a, b and c and
displays the result based on the discriminant. If the discriminant is If the
discriminant is greater than or equal to 0, display the two roots. If the
discriminant is equal to 0, display the one root. Otherwise, display “the equation
has no roots”.
Exercise 8 - Lowest Score Drop
Write a program that calculates the average of a group of test scores, where the
lowest score in the group is dropped. It should use the following functions:

• void getScore() should ask the user for a test score, store it in a reference
parameter variable, and validate it. This function should be called by main
once for each of the five scores to be entered.
• void calcAverage() should calculate and display the average of the four
highest scores. This function should be called just once by main, and should
be passed the five scores.
• int findLowestQ should find and return the lowest of the five scores passed to
it. It should be called by calcAverage, which uses the function to determine
one of the five scores to drop.

#include
<iostream>
using namespace std;
// Function Prototypes
void getScore(int &);
void calcAverage(int, int, int, int, int);
int findLowest(int, int, int, int, int);
int main()
{
int Score1, Score2, Score3, Score4, Score5;
// Display program introduction
cout << "\nThis program calculates the average of five Scores.\n";
// Call getScore function once for each of the five scores
getScore(Score1);
getScore(Score2);
getScore(Score3);
getScore(Score4);
getScore(Score5);
// Call calcAverage and pass the five scores
calcAverage(Score1, Score2, Score3, Score4, Score5);

return 0;
}
/*****************************************************************************
* getScore *
* This function asks the user for a test score. Stores it in a *
* reference parameter variable, and validates it. *
*****************************************************************************/
void getScore(int &Score)
{
do
{
cout << "Enter a test score: ";
cin >> Score;
if (Score < 0 || Score > 100)
{
cout << "\nInvaild test score!\n"
<< "Test score must be greater than 0 and less than
100.\n\n";
}
} while(Score < 0 || Score > 100);
}
/*******************************************************************************
* calcAverage *
* This function is passed five test scores. it calls the function findLowest *
* and calculates and displays the average of the four highest test scores. *
*******************************************************************************/
void calcAverage(int Score1, int Score2, int Score3, int Score4, int Score5)
{
int Lowest; // Lowest test score
double Avg; // Average of the four highest test scores
// Call function findLowest
Lowest = findLowest(Score1, Score2, Score3, Score4, Score5);
// Caculate average of four highest test scores
if (Lowest == Score1)
Avg = (Score2 + Score3 + Score4 + Score5)/4;
else if(Lowest == Score2)
Avg = (Score1 + Score3 + Score4 + Score5)/4;
else if(Lowest == Score3)
Avg = (Score2 + Score1 + Score4 + Score5)/4;
else if(Lowest == Score4)
Avg = (Score2 + Score3 + Score1 + Score5)/4;
else
Avg = (Score2 + Score3 + Score4 + Score1)/4;

// Display average
cout << "\nThe average of the four highest scores is "
<< Avg << ".\n\n";
}
/********************************************************************************
* findLowest *
* This function returns the lowest of five scores pass to it *

********************************************************************************/
int findLowest(int Score1, int Score2, int Score3, int Score4, int Score5)
{ // Determine and return lowest test score
if (Score1 < Score2 && Score1 < Score3 && Score1 < Score4 &&
Score1 < Score5)
return Score1;
else if (Score2 < Score1 && Score2 < Score3 && Score2 < Score4 &&
Score2 < Score5)
return Score2;
else if (Score3 < Score2 && Score3 < Score1 && Score3 < Score4 &&
Score3 < Score5)
return Score3;
else if (Score4 < Score2 && Score4 < Score3 && Score4 < Score1 &&
Score4 < Score5)
return Score4;
else
return Score5;
}

Vous aimerez peut-être aussi