Ch 5

Ace your homework & exams now with Quizwiz!

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

int add(int, 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);

Assume that printStars is a function that takes one argument and returns no value. It prints a line of N stars (followed by a newline character) where N is the value of the argument received. Write a statement that invokes printStars to make a line of 35 stars.

printStars(35);

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

printTodaysDate();

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 isSenior, which receives an integer parameter and returns true if the parameter's value is greater or equal to 65, and false otherwise. So if the parameter's value is 7 or 64 or 12 the function returns false. But if the parameter's value is 69 or 83 or 65 the function returns true.

int isSenior(int a){ if(a<65){ a=0; return a; } else{ a=1; return a; } }

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 attitude) { if (attitude==1){ printf("disagree\n"); } if(attitude==2){ printf("no opinion\n"); } if(attitude==3){ printf("agree\n"); } }

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

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

Assume the availability of a function called printStars. The function receives an int argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if the printStars(8) is called, ******** (8 asterisks) will be printed. Assume further that the variable starCount has been declared and initialized to a positive integer. Write some code that prints starCount asterisks to standard output by: first printing a single asterisk (and no other characters) then calls printStars to print the remaining asterisks.

if(starCount>0){ printf("*"); printStars(starCount-1); }

Write the definition of a function twice, which receives an integer parameters and returns an integer that is twice the value of the parameter.

int twice(int a){ a=a*2; return a; }

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

int twice(int);

Write an expression to compute the quartic root of x. The quartic root of a number is the square root of its square root. EXAMPLES: For example, the quartic root of 16.0 is 2.0 because: the square root of 16.0 is 4.0 and the square root of 4.0 is 2.0. Another example: the quartic root of 81.0 is 3.0 because the square root of 81.0 is 9.0 and the square root of 9.0 is 3.0. Thus, to find the quartic root of a number you take the square root of the number and then take the square root of that. In this exercise you must find the quartic root of x in a single expression-- you must not write any statements. Also, you may only use the sqrt() function-- no other functions. (HINT: you will need to call the sqrt() function twice-- and you will need to pass the return value of one of those calls as argument to the other call. AND REMEMBER: write an expression, not a statement.)

sqrt(sqrt(x))

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

void printLarger();

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.

void printLarger(int a, int b) { if(a>b){ printf("%d\n",a); } else{ printf("%d\n",b); } }

Write a function called printStars. The function receives an int parameter. If the parameter is positive, the function prints (to standard output) the given number of asterisks; otherwise the function does nothing. The function does not return a value. Thus, if the printStars(8) is called, ******** (8 asterisks) will be printed. The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it gets the job done by examining its parameter, returning if the parameter's value is not positive. If the parameter is positive, it prints a single asterisk (and no other characters) then (recursively) calls itself to print the remaining asterisks.

void printStars(int star) { if(star>0){ printf("*"); printStars(star-1); } }

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

void printTodaysDate();

Assume the availability of a function called fact. The function receives an int argument and returns an int value. If the argument is one or smaller, it returns the integer value one. Otherwise it returns the product of all the integers from one to its argument. So the value of fact(4) is 1*2*3*4 and the value of fact(10) is 1*2*3*4*5*6*7*8*9*10. Assume further that the variable k has been declared and initialized to a positive integer. Assume further that the variable x has been declared as an integer type. Write a statement that assigns x the value k*(k-1)*(k-2)*...*3*2*1 by calling the fact function and multiplying its return value by k. Note: your solution must include multiplying fact's return value by k here.

x=fact(k-1)*k;

Write the definition of a function powerTo, which receives two parameters. The first is a double and the second is an int. The function returns a double. If the second parameter is negative, the function returns 0. Otherwise it returns the value of the first parameter raised to the power of the second.

double powerTo(double a, int b){ if(b>=0){ a=pow(a,b); return a; } else return 0; }

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) { printf("Grade:%c\n",grade); }

Assume the availability of a function called printStars. The function receives an int argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if the printStars(8) is called, ******** (8 asterisks) will be printed. Assume further that the variable starCount has been declared and initialized to a some integer, possibly negative or zero. Write some code that does nothing if starCount is not positive but that otherwise prints starCount asterisks to standard output by: first printing a single asterisk (and no other characters) then calls printStars to print the remaining asterisks.

if(starCount>0){ printf("*"); printStars(starCount-1); } else return 0;

Write the definition of a function absoluteValue, which receives an integer parameter and returns the absolute value of the parameter's value. So if the parameter's value is 7 or 803 or 141 the function returns 7, 803 or 141 respectively. But if the parameter's value is -22 or -57, the function returns 22 or 57 (same magnitude but a positive instead of a negative). And if the parameter's value is 0, the function returns 0.

int absoluteValue(int a){ if(a<0){ a=-a; return a; }else return a; }

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

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

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 a){ a=a/2; return a; }

Write the definition of a function isEven, which 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.

int isEven(int a){ int b; b=a%2; if(b==0){ a=1; return a; }else{ a=0; return a; } }

Write the definition of a function isPositive, which 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.

int isPositive(int a){ if(a>0){ a=1; return a; }else{ a=0; return a; } }

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

int min(int a, int b){ if(a<b){ return a; } else return 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 a){ a=a-1; return a; }

Write the definition of a function oneMore, which receives an integer parameter and returns an integer that is one more than the value of the parameter. So if the parameter's value is 7, the function returns the value 8. If the parameter's value happens to be 44, the functions returns the value 45.

int oneMore(int a){ a=a+1; return a; }

Write the definition of a function called product. The function receives two int parameters. You may assume that neither parameter is negative. The function returns the product of the parameters. So, product(3,5) returns 15 and product(30,4) returns 120. The function must not use a loop of any kind (for, while, do-while) to accomplish its job.

int product(int x,int y) { return(x*y); }

Write the definition of a function signOf, which 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 a){ if(a==0){ return a; }else{ if(a>0){ a=1; return a; } else{ a=-1; return a; } } }

Write the definition of a function square, which receives an integer parameter and returns the square 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 a ){ a=a*a; return a; }

max is a function that accepts two int parameters and returns the value of the larger one. Four int variables, population1, population2, population3, and population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max. (HINT: you will need to call max three times and you will need to pass the return values of two of those calls as arguments to max. REMEMBER: write an expression, not a statement.)

max(max(population1,population2),max(population3,population4))

max is a function that accepts two int parameters and returns the value of the larger one. Two int variables, population1 and population2, have already been declared and initialized. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.

max(population1,population2)

Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min. Assume that all the variables have already been declared and that x, y, and z have been assigned values).

min = x>y?y:x; min=min>z?z:min;

Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a function that receives a single integer parameter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14. Write an expression whose value is eight times that of x without using the standard C arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this. In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

twice(twice(twice(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 a){ if(a>0){ int i; for(i=0; i<a; i++){ printf("-"); } printf("\n"); } }

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() { printf(".....\n"); }

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

void printErrorDescription();


Related study sets

Corporal's Course (Leadership II)

View Set

Кримінальний кодекс

View Set

ME-253 Intro to Manufacturing Lab

View Set

Cybersecurity Essentials chapter 2, part 1

View Set

English File Upper-intermediate 3A - Air travel (Hay), Adverbs and adverbial phrases - English File Upper Int - SB p155 other vocab (Hay)

View Set

Public Speaking - Group Presentations

View Set

BUS 424 MC, Accounting Ethics Midterm #1 Questions, Accounting Ethics Midterm #2 Questions, Ch. 1 Ethics, CH. 6 QUIZ ETHICS, Ch. 2 Cognitive Processes and Ethical Decision Making in Accounting, Ch. 7 Accounting Ethics, Accounting Ethics Final Exam Re...

View Set

Mac OS Support Essentials v10.12 User Accounts

View Set

Business Law Chapter 39 Limited Liability Companies & Related Forms 6th ed

View Set