6.13: Using Reference Variables as Parameters

अब 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 header for a function addOne that accepts a single integer reference parameter and returns nothing. Name the parameter x.

void addOne(int &x)

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

void addOne(int& num){ num = num + 1; }

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 an integer variable i, declare a reference variable r that is a reference to i.

int&r = i;

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; }

Assume that a function named swapdoubles has been defined and is available for use in this exercise: that function receives two variables of type double and exchanges their values . Write the definition of a function named sort3 that is passed three double variables . The function returns nothing but modifies the values of these variables so they are in sorted order. So, if a, b and c have (respectively) the values 3.14, 2.71, and 3.04, and the invocation sort3(a,b,c) is made, then upon return, the values of a, b and c will be 2.71, 3.04, and 3.14 respectively.

void sort3 (double &a, double &b, double &c){ if (a>b) swapdoubles (a,b); if (b>c) swapdoubles (b,c); if (a>b) swapdoubles (a,b); }

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; }

zeroIt is a function that takes one argument and returns no value. The function stores the value 0 back into the parameter. x is an int variable that has been declared. Write a statement that sets the value stored in x to zero by invoking the function zeroIt .

zeroIt(x);


संबंधित स्टडी सेट्स

Section 3.4 Part 1: Multiplying and Dividing Integers and Variable Expressions with Integers

View Set

Chapter 15 - Aggregate Demand and Aggregate Supply

View Set

Environmental Design 100: Final Exam

View Set

Multilingual, Hybrid, Planken, Multiple, Codes, Bilingualism, Multilingualism, Mixing, Practices, Contact, Variation, World_Englishes_English, Genre_Variety, Global, Code-switching, Function, Symbol, Norm_Approach, Advertising, Contact, Identity: 18-42

View Set

Oklahoma State Physiology BIOL 3204 Exam 1 Questions

View Set

Module 31: Exemplar C - Obsessive-Compulsive Disorder

View Set