19.6k views
3 votes
4. Compose a C function named getSpeed that uses a reference parameter variable named speed to accept an integer argument. The function should prompt the user to enter a number in the range of 20 through 70. Set the parameter variable to zero if the user enters a number outside the range of 20 through 70.

1 Answer

6 votes

Answer:

void getSpeed(int& speed)

//the function with reference parameter speed and & denote a reference

{ //prompts user to enter a number

cout << "Enter a number in the range of 20 through 70: ";

cin >> speed; //reads the value entered by user

if(speed < 20 || speed> 70)

// if value of speed entered by user is outside the given range

{

speed = 0; // speed is set to 0

} }

Step-by-step explanation:

In order to check how this function works you can write a main function and print the value of speed as following:

int main()

{ int speed;

getSpeed(speed); //calls getSpeed function

cout<<"speed is "<<speed; } //displays value of speed

Output:

Enter a number in the range of 20 through 70: 50

speed is 50

Output for if condition:

Enter a number in the range of 20 through 70: 80

speed is 0

User Ehsan Mirsaeedi
by
4.5k points