CS210 Lab: Operator Overloading Prelab Answers


Prelab Answers:

  1. Given a function prototype:
    void stradd(char *s1, char *s2); 
    

    Which of the following overload stradd?

    1. void stradd(char *s1, int i);
    2. void sstradd(int i, int j);
    3. void stradd(char *s1, char *s2, int i);
    4. void stradd(int i, int j);
    5. void stradd3(char *s1, char *s2, char *s3);
    a, c and d overload stradd.
    b and e have different function names.

  2. Given the following function, which calculates the average of an array of numbers of type int:
    int average(int array[], int size);
    {
    	int total=0;   			//set total to 0
    	for (int j=0; j<size; j++) 	//for every array member,
    		total+=array[j];	//add it to total
    	return total/size;		//average (as integer)
    }
    
    Overload the function so that it averages arrays of type long.
    long average(long array[], int size);
    {
    	long total=0;  			//set total to 0
    	for (int j=0; j<size; j++) 	//for every array member,
    		total+=array[j];	//add it to total
    	return total/size;		//average (as long)
    } 
    

Back to Exercise click here
Back to Operator Overloading Lab click here

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.