COMP 150 Chapter 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume a function addOne that accepts a single integer reference parameter and returns nothing. Call the function, passing it the variable i which has already been declared .

addOne (i) ;

Write the definition of a function named alternator that receives no parameters and returns true the first time it is invoked, returns false the next time it is invoked, then true , false and so on, alternating between true /false on successive invocations.

bool alternator () { static int i; i++; if((i % 2) != 0) return true; else return false; }

Write the definition of a function isEven, that receives an integer parameter and returns true if the parameter 's value is even, and false otherwise. So, if the parameter 's value is 7 or 93 or 11 the function returns false . But if the parameter 's value is 44 or 126 or 7778 the function returns true .

bool isEven(int x) { if (x % 2 == 0) { return true; } else { return false; } }

Write the definitions for three functions named max. Each receives two parameters , of the same type , and returns the larger of the two values . Define one of these functions to apply to type double , another to type int and a third to type char .

double max(double a, double b) { if (a>=b) return a; else return b; } double max(int a, int b) { if (a>=b) return a; else return b; } double max(char a, char b) { if (a>=b) return a; else return b; }

Write a statement that declares a prototype for a function powerTo, which has two parameters . The first is a double and the second is an int . The function returns a double .

double powerTo(double,int);

Write the definition of a function named maxmin that is passed four int arguments . The function returns nothing but stores the larger of the first two arguments in the third argument it receives and the the smaller of the first two arguments in its fourth argument . So, if you invoke maxmin(3,7,x,y), upon return x will have the value 7 and y will have the value 3.

void maxmin(int iVal1, int iVal2, int& x, int& y) { if (iVal1 > iVal2) { x = iVal1; y = iVal2; } else { x = iVal2; y = iVal1; } }

Given that a function receives three parameters a, b, c, of type double , write some code, to be included as part of the function, that checks to see if the value of a is 0; if it is, the code prints the message "no solution for a=0" and returns from the function.

if (a == 0){ cout << "no solution for a=0"; }

Given that a function receives three parameters a, b, c, of type double , write some code, to be included as part of the function, that determines whether the value of "b squared" - 4ac is negative. If negative, the code prints out the message "no real solutions" and returns from the function.

if (b*b < 4*a*c) { cout << "no real solutions"; }

Write the header for a function addOne that accepts a single integer reference parameter and returns nothing. Name the parameter x.

void addOne(int &x)

Write the definition of a function dashedLine, with one parameter , an int . If the parameter is negative or zero, the function does nothing. Otherwise it prints a complete line terminated by a new line character to standard output consisting of dashes (hyphens) with the parameter 's value determining the number of dashes. The function returns nothing.

void dashedLine (int dashes) { if (dashes>0) { for (int i=0; i<dashes; i++) cout << "-"; cout << "\n"; } }

Write the definition of a function named panic. The function prints the message "unrecoverable error" and then terminates execution of the program , indicating failure..

void panic() { cout << "unrecoverable error" << endl; exit(EXIT_FAILURE); }

Write the definition of a function isPositive, that receives an integer parameter and returns true if the parameter is positive, and false otherwise. So, if the parameter 's value is 7 or 803 or 141 the function returns true . But if the parameter 's value is -22 or -57, or 0, the function returns false .

bool isPositive(int x) { if (x > 0) {return true;} else {return false;} }

toThePowerOf is a function that accepts two int parameters and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized . Another int variable , cubeVolume, has already been declared . Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3, and store this value in cubeVolume.

cubeVolume = toThePowerOf(cubeSide, 3);

Write the definition of a function named quadratic that receives three double parameters a, b, c. If the value of a is 0 then the function prints the message "no solution for a=0" and returns. If the value of "b squared" - 4ac is negative, then the code prints out the message "no real solutions" and returns. Otherwise the function prints out the largest solution to the quadratic equation. The formula for the solutions to this equation can be found here: Quadratic Equation on Wikipedia

double quadratic(double a, double b, double c) { if(a==0) { cout<<"no solution for a=0"; return a; }else if ((b*b-4*a*c)<0) { cout<<"no real solutions"; }else { double x= (-b+sqrt(b*b-4*a*c))/(2*a); double y= (-b-sqrt(b*b-4*a*c))/(2*a); if (x>y) { cout<<x; return x; }else { cout<<y; return y; } } }

Write the definition of a function named timeOnHighway that receives three parameters , all of type double : mileEndingPoint, mileStartingPoint, and speed. The first two parameters indicate the mile markers on an interstate at which a vehicle goes to and starts at; the third parameter indicates the speed of the vehicle in miles per hour. The function returns the number of hours it takes a vehicle to go from the starting mile marker to the ending one. The function has a default value for the speed: 55 miles per hour, and a default value for mileStartingPoint: 0.0.

double timeOnHighway(double mileEndingPoint, double mileStartingPoint=0.0, double speed=55.0) { return (mileEndingPoint-mileStartingPoint)/speed; }

add is a function that accepts two int parameters and returns their sum . Two int variables , euroSales and asiaSales, have already been declared and initialized . Another int variable , eurasiaSales, has already been declared . Write a statement that calls add to compute the sum of euroSales and asiaSales and store this value in eurasiaSales.

eurasiaSales = add(euroSales, asiaSales);

Write the definition of a function add, which receives two integer parameters and returns their sum

int add (int a, int b) { return a + b; }

Write a statement that declares a prototype for a function add, which has two int parameters and returns an int .

int add (int, int);

Write the definition of a function half, which receives an integer parameter and returns an integer that is half the value of the parameter . (Use integer division!) So if the parameter 's value is 7, the function returns the value 3. If the parameter 's value happens to be 44, the functions returns the value 22.

int half(int input) { return input/2; }

Write the definition of a function max that has three int parameters and returns the largest.

int max(int a, int b, int c) { int max = a; if(b>a) max = b; if(c>max) max = c; return max; }

Write the definition of a function min that has two int parameters and returns the smaller.

int min(int a, int b) { return ((a<b)?a:b); }

Write the definition of a function oneLess, which receives an integer parameter and returns an integer that is one less than the value of the parameter . So if the parameter 's value is 7, the function returns the value 6. If the parameter 's value happens to be 44, the functions returns the value 43.

int oneLess(int amount) { return amount - 1; }

Write the definition of a function signOf, that receives an integer parameter and returns a -1 if the parameter is negative, returns 0 if the parameter is 0 and returns 1 if the parameter is positive. So, if the parameter 's value is 7 or 803 or 141 the function returns 1. But if the parameter 's value is -22 or -57, the function returns -1. And if the parameter 's value is 0, the function returns 0.

int signOf(int input) { return ((input > 0) ? 1 : (input==0) ? 0 : -1); }

Write the definition of a function square, which receives an integer parameter and returns the square of the value of the parameter . So if the parameter 's value is 7, the function returns the value 49. If the parameter 's value happens to be 25, the functions returns the value 625. If the value of the parameter is 0, the function returns 0.

int square (int input) { return input*input; }

Write a statement that declares a prototype for a function twice, which has an int parameter and returns an int .

int twice(int);

printErrorDescription is a function that accepts one int parameter and returns no value . Write a statement that calls the function printErrorDescription, passing it the value 14.

printErrorDescription(14);

printLarger is a function that accepts two int parameters and returns no value . Two int variables , sales1 and sales2, have already been declared and initialized . Write a statement that calls printLarger, passing it sales1 and sales2.

printLarger(sales1,sales2);

printTodaysDate is a function that accepts no parameters and returns no value . Write a statement that calls printTodaysDate.

printTodaysDate();

Write the definition of a function named counter that receives no parameters and returns 0 the first time it is invoked, returns 1 the next time it is invoked, then 2, 3 and so on.

static int counter() { static int i; return i++; }

Write a function addOne that adds 1 to its integer reference parameter . The function returns nothing.

void addOne (int&x){ x++;}

Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message to standard output depending on the value of its parameter . -If the parameter equals 1, the function prints disagree -If the parameter equals 2, the function prints no opinion -If the parameter equals 3, the function prints agree In the case of other values , the function does nothing. Each message is printed on a line by itself.

void printAttitude(int input) { switch(input) { case 1: cout<<"disagree\n"; break; case 2: cout<<"no opinion\n"; break; case 3: cout<<"agree\n"; break; } }

Write the definition of a function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line (terminated by a new line character ) consisting of 5 periods.

void printDottedLine() { cout << ".....\n"; }

Write a statement that declares a prototype for a function printErrorDescription, which has an int parameter and returns nothing.

void printErrorDescription (int);

Write the definition of a function printGrade, which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character ) to standard output . Don't forget to put a new line character at the end of your line. Thus, if the value of the parameter is 'A', the function prints out Grade: A

void printGrade(char grade) { cout << "Grade: " << grade << "\n"; }

Write the definition of a function printLarger, which has two int parameters and returns nothing. The function prints the larger value of the two parameters to standard output on a single line by itself. (For purposes of this exercise, the "larger " means "not the smaller".)

void printLarger(int first, int second){ if(first>second) cout<<first<<"\n"; else cout<<second<<"\n"; }

Write a statement that declares a prototype for a function printLarger, which has two int parameters and returns no value .

void printLarger(int,int);

Write a statement that declares a prototype for a function printTodaysDate, which has no parameters and doesn't return anything.

void printTodaysDate ();

Write the definition of a function named rotate4ints that is passed four int variables . The function returns nothing but rotates the values of the four variables : the first variable , gets the value of the last of the four, the second gets the value of the first, the third the value of the second, and the last (fourth) variable gets the value of the third. So, if i, j, k, and m have (respectively) the values 15, 47, 6 and 23, and the invocation rotate4ints(i,j,k,m) is made, then upon return, the values of i, j, k, and m will be 23, 15, 47 and 6 respectively.

void rotate4ints(int &i, int &j, int &k, int &m) { int tmp = m; m = k; k = j; j = i; i = tmp; }

Write the definition of a function named swapints that is passed two int variables . The function returns nothing but exchanges the values of the two variables . So, if j and k have (respectively) the values 15 and 23, and the invocation swapints(j,k) is made, then upon return, the values of j and k will be 23 and 15 respectively.

void swapints (int &j, int &k){ int temp; temp=j; j=k; k=temp; }


Kaugnay na mga set ng pag-aaral

hinkle Ch 30: Management of Patients with Hematologic Neoplasms ML7

View Set

ECON 401, Chapter 6: The Org of the Firm

View Set

Read and Interact - CH 7 Geography

View Set

Chapter 14- Water Pollution: Study Guide

View Set

Intro to Psych Chapter 9 - Motivation and Emotion

View Set

Penny CH27 fetal heart and chest

View Set

Comprehensive Exam - Public Speaking - Chapter 15

View Set

Lab 10 Activity 5: Fatigue in Isolated Skeletal Muscle

View Set