Tuesday, June 19, 2012

Welcome!

If you need help with TuringsCraft codelab for CS 1/C++ you've come to the right place! Go to the right hand side for easy navigation and search button! Make sure you book mark our page or search google for "c plus plus codelab answers" next time you come!

Homework 10: testing

1. Assume you need to test a function named max . The function max receives two int arguments and returns the larger. Write the definition of driver function  testmax whose job it is to determine whether max is correct. So testmax returns true if  max is correct and returns false otherwise. Instructor's notes: testmax does not take any arguments, only max does. So, the header of testmax will look like this:


bool testmax () { return max ( 5 , 4 ) == 5 && max ( 4 , 5 ) == 5 && max ( 4 , 4 ) == 4; }

2. Assume you need to test a function named inOrder . The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function  testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if  inOrder is correct and returns false otherwise.
. For the purposes of this exercise, assume inOrder is an expensive function call, so call it as few times as possible!

bool testInOrder () { return inOrder ( 1 , 2 , 3 ) && inOrder ( 1 , 1 , 3 ) && inOrder ( 1 , 2 , 2 ) && inOrder ( 2 , 2 , 2 ) && !inOrder ( 5 , 2 , 3 ) && !inOrder ( 3 , 4 , 1 ) && !inOrder ( 2 , 1 , 3 ) && !inOrder ( 3 , 2 , 1 ) && !inOrder ( 3 , 1 , 1 ) && !inOrder ( 1 , 3 , 1 ) && !inOrder ( 3 , 3 , 1 ) && !inOrder ( 3 , 1 , 3 ); }

Homework 10: Vectors-2 Vectors (Part 2)

1. Assume that an array of integers named  a that contains exactly five elements has been declared and initialized. Write a single statement that assigns a new value to the first  element of the array. This new value should be equal to twice the value stored in the last  element of the array. Do not modify any values in the array other than the first element. Instructor's notes: An array is used just like a vector, only it does not have any member functions. That means you cannot use the size member function for this exercise, but you do know the size is 5

a [ 0 ] = 2 * a [ 4 ] ;

2. Assume v is a vector that has been declared and initialized. Write an expression whose value is the number of values that have been stored in v.

v . size ( )

3. Assume that an vector of  int named  a has been declared with 12 elements and that the integer variable  k holds a value between 0 and 6. Assign 15 to the vector element whose index is  k .

a [ k ] = 15 ;

4. Given that an vector of  int named  a has been declared, and that the integer variable  n contains the number of elements of the vector  a , assign -1 to the last  element in  a . Instructor's notes: Do not use a member function in your solution to this problem.

a [ n - 1 ] = - 1 ;

5. Given that an vector of  int named  a has been declared with 12 elements and that the integer variable  k holds a value between 0 and 6. Assign 9 to the element just after  a[k] .

a [ k + 1 ] = 9 ;

6. Given that an array of  int named  a has been declared with 12 elements and that the integer variable  k holds a value between 2 and 8. Assign 22 to the element just before  a[k] . Instructor's notes: This question works exactly the same for vectors, so just switch the name array with vector in the exercise description. In other words, think of a as a vector, not an array.

a [ k - 1 ] = 22 ;

7.  An vector of  int named  a that contains exactly five elements has already been declared and initialized. In addition, an  int variable  j has also been declared and initialized to a value somewhere between 0 and 3. 
Write a single statement that assigns a new value to the element of the vector indexed by  j . This new value should be equal to twice the value stored in the next element of the vector (i.e. the element after the element indexed by  j ). Do not modify any other elements of the vector! 

a [ j ] = 2 * a [ j + 1 ] ;

Homework 9: VECTORS

1. Declare a vector named  scores of twenty-five elements of type  int .

vector < int > scores ( 25 ) ;

2. Given the vector  a , write an expression that refers to the first   element of the vector. 

a [ 0 ]

3. Given an vector  a , declared to contain 34 elements, write an expression that refers to the last   element of the vector. Instructor's notes: Do not use a member function in your solution to this problem.

a [ 33 ]

4. Assume that the vector  arr has been declared. In addition, assume that  VECTOR_SIZE has been defined to be an integer that equals the number of elements in  arr . Write a statement that assigns the next to last  element of the vector to the variable  x ( x has already been declared). 

x = arr [ VECTOR_SIZE - 2 ] ;

5. Assume that the vector  monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.). Write a statement that writes to standard output the element corresponding to October. 

cout << monthSales [ 9 ] ;

6. Given that an vector of  int named  a has been declared, assign 3 to its first  element. 

a [ 0 ] = 3 ;

7. Assume that an vector of  int variables named  salarySteps that contains exactly five elements has been declared. Write a single statement to assign the value  30000 to the first  element of this vector. 

salarySteps [ 0 ] = 30000 ;

8. Assume that an vector of integers named  salarySteps that contains exactly five elements has been declared. Write a statement that assigns the value  160000 to the last  element of the vector  salarySteps . 

salarySteps [ 4 ] = 160000 ;

9.  Assume that an vector named  a , containing exactly five integers has been declared and initialized. Write a single statement that adds ten to the value stored in the first  element of the vector. 

a [ 0 ] += 10 ;

10. Given that an vector of  int named  a with 30 elements has been declared, assign 5 to its last  element. Instructor's notes: Do not use a member function to answer this question.

a [ 29 ] = 5 ;

11. Assume v is a vector that has been declared and initialized. Write an expression whose true if there are any values stored in v.

! v. empty ( )

12. Assume v is a vector that has been declared and initialized. Write a statement that removes ALL the values stored in v.

v . clear ( ) ;

13. Assume v is a vector of integers that has been declared and initialized. Write a statement that adds the value 42 to the vector.

v . push_back ( 42 ) ;

Homework 8: Reference Parameters

1. 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 )

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

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

Homework 8: Exchanges

1. Given two  int variables,  i and  j , which have been declared and initialized, and two other  int variables,  itemp and  jtemp , which have been declared, write some code that swaps the values in  i and  j by copying their values to  itemp and  jtemp , respectively, and then copying  itemp and  jtemp to  j and  i , respectively.


itemp = i ; jtemp = j ; i = jtemp ; j = itemp ;

2. Given three already declared  int variables,  i ,  j , and  temp , write some code that swaps the values in  i and  j . Use  temp to hold the value of  i and then assign  j 's value to  i . The original value of  i , which was saved in  temp , can now be assigned to  j . 

temp = i ; i = j ; j = temp ;

3. Given two  int variables,  firstPlaceWinner and  secondPlaceWinner , write some code that swaps their values. Declare any additional variables as necessary, but do not redeclare  firstPlaceWinner and  secondPlaceWinner .

int temp ; temp = firstPlaceWinner ; firstPlaceWinner = secondPlaceWinner ; secondPlaceWinner = temp ;

4. Given two  double variables,  bestValue and  secondBestValue , write some code that swaps their values. Declare any additional variables as necessary. 

double temp ; temp = bestValue ; bestValue = secondBestValue ; secondBestValue = temp ;
 

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

Homework 8: Invocations invocations

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

printTodaysDate ( ) ;

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

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

Homework 8: Declarations

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

void printErrorDescription ( int ) ;

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

void printLarger ( int , int ) ;

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

int twice ( int ) ;

Homework 7: Definitions

1. 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 x ) { return x + 1 ; }

2. 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 x ) { return x - 1 ; }

3. 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 x ) { return x / 2 ; }

4. 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 x ) { return x * x ; }

5. Write the definition of a function  absoluteValue , that 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 x ) { return abs ( x ) ; }

6. 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 x ) { if ( x < 0 ) { return - 1; } else if ( x == 0 ) { return 0; } else { return 1; } }

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

8. Write the definition of a function  isSenior , that 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.

bool isSenior ( int x ) { if ( x >= 65 ) { return true; } else { return false; } }

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

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

int twice ( int x ) { return 2 * x ; }

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

int add ( int x , int y ) { return x + y ; }

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

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

13. 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 x , int n ) { double prod = 1.0; if ( n < 0 ) return 0.0; while ( n > 0 ) { prod *= x; --n; } return prod; }

Homework 7: Function Invocations

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

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

3. 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 )

4. 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 ) )

Homework 7: Intro

1. Assume that the  int variables  i ,  j and  n  have been declared, and  n has been initialized. Write code that causes a "triangle" of asterisks to be output to the screen, i.e.,  n lines should be printed out, the first consisting of a single asterisk, the second consisting of two asterisks, the third consisting of three, etc. The last line should consist of  n asterisks. Thus, for example, if  n has value 3, the output of your code should be (scroll down if necessary):
 *
 **
 ***


for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= i ; j ++ ) { cout << "*" ; } cout << endl ; }