Tuesday, June 19, 2012

Homework 8: Definitions

1. 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 << "....." << endl ; }

2. 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 a , int b ) { if ( a > b ) cout << a << endl ; else cout << b << endl ; }

3. 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 n ) { if ( n <= 0 ) return ; while ( n > 0 ) { cout << '-' ; -- n; } cout << endl; }

No comments:

Post a Comment