Code you should be able to write pretty quickly if asked on a test

Disclaimer:

This is only a very small sample of the topics and kinds of questions you might encounter on the final. Just being able to do these questions does –not– mean you will know everything that will be asked on the final. This is a sampling of questions, not a thorough review.

Write a function that will "reverse" an array (of ints).
Q: Are you being asked to display an array in reverse order or to actually change the data in the array? A: change the array. Q: can this function be passed an array of doubles or does it have to be overloaded to work with an array of doubles? A: must be over loaded (try that one too!)

Write a function to write the contents of an array of ints either on the screen or into an already opened-for-output file. Do this so that if the caller calls this function without passing in stream variable, the output will go to the screen.

Write a function that will print the last N elements of an array of ints in reverse order on the screen.

Write the function that will open a file for input. The filename will be provided by the user and will be given only one try. The function must allow the caller to determine whether or not the file was opened successfully.

Write a function that will determine if two arrays of doubles contain the same number of values and that they appear in the same order. Q: will your function need to be passed how many items there are in each array? A: Yes, for all arrays unless you are working with the full capacity of the array, you must assume the passed in array is partially filled and thus two things are needed to work with arrays in functions: where the array starts and the size or quantity of meaningful elements. Both of these must be passed into any function that will work on an array.

Write a function that will return the average difference of two arrays (assume that they have the same number of elements).
3 4 51 and
2 7 10
have an average difference of ( |3-2| + |4-7| + |51-10| ) / 3
Q: Since there are three mathematical calculations on integers involved, what else will you have to do?
A: write function for the calculation of absolute difference between two ints.
Q: If you are guaranteed that the two arrays contain the same number of elements to be processed, do you need to be passed the number elements in both arrays? A: No.

Rewrite the previous function using some other mechanism to communicate the value back to the caller. [What's the difference between returning a value, using a parameter to communicate a value back to the caller and writing output on the screen or to a file?]
A: writing to the screen does NOT communicate to the caller of the function. Telling the USER something is not communicating with the caller - the user didn’t call the function!
There are two ways to communicate back to the caller: the return statement and reference parameters.
Q: which of these two ways can only communicate back one value? A: the return statement.

Q: When is the return statement preferred instead of reference partakers?
A: When only one value is passed back (and we’re not passing back a class like vector or istream or ostream).

Q When are reference parameters required?
A: If there is more than one piece of information to communicate back - since the return statement can pass back only one piece of information. (Of course might consider writing a struct to contain the separate pieces of data and return one struct.)

Write a function that will get two names from the user – first name and last name – and will return a string with last name, first name only if the two names are not the same. The function should return the empty string if they are the same. Ex: if the user enters John Smith, the function should return the string: Smith, John. If the user for some dumb reason enters Jon Jon, the function should return the null string.

A: Make sure there are 3 local std:strings. Two are for the input of the names. The third is a local std::string var that is not initialized (thus holding the null string) and if the test for = = is true, return that uninitialized var. If the test fails, you can build the string return by: var += second + “, “ + first.
Then return that string. [You can also just return second + “, “ + first] Return type should be string. NO CSTRINGS ALLOWED!

Write a function that will be passed an inclusive range (the two endpoints) and will return an integer in that range gotten from the user. The function should not allow processing to continue until a number in the range is entered. The function must make sure that after it is called the next input will not be messed up. For example if the program should get a valid int and then the name of a city, this function that gets only the int should not mess up getting the name of a city.

Write a function that takes a file that has been opened for input and a file that has been opened for output and an array that contains ints. The input file contains only integers and whitespace. Only certain numbers are to be written into the output file. The array contains these valid numbers. After the function has completed, the output file should contain only the numbers from the input file that match a number in the array. The output format should be: one per line The order of the output file should be the same as the order in the input file (except of course that it may be that not all the integers from the input file are written to the output file).
Q: should you write more than one function to do this problem? That is, should this function call other functions that you write in order to do its job? A: Yes, if there are subproblems, you should write functions for each (like the search function to see if an input value is found in the array).

Write a function that will read a file that contains four ints per line and returns the line number that has numbers with the largest average. (Assume that there are no two lines with the same average.) [Don't read the next problem until you have solved this one.]

Explain why these two loops work exactly the same. (Which would be preferred?)
The problem is from the previous problem.
STR_VAR is the parameter name for an ifstream object that will catch an already successfully opened stream variable.
int first, second ,third, fourth; // tmps for input
unsigned currentLineNumber( 0 ); // counter for # of lines
double maxLineAvg( -999999999999 ); // a good guess
unsigned lineWithMaxAverage; // none yet
double currentLineAvg;
while ( STRM_VAR > first > second > third > fourth ) {
// just read a line, count it
// we add before processing so the "first" line number
// will be 1, not 0. If we don't process any, we'll
// say that line number 0 has the max – the caller can
// test for this
++currentLineNumber;
// take the average for this line
currentLineAvg = (first+second+third+fourth)/4.0;
// if we have a new greatest average…
if ( currentLineAvg > maxLineAvg )
// …then update the current maximum…
maxLineAvg = currentLineAvg;
// … and the line number it occurred on
lineWithMaxAverage = currentLineNumber;
}
return lineWithMaxAverage;
Here’s the other version of the loop
while ( STRM_VAR > first ) {

STRM_VAR > second > third > fourth ); // WHY???

Notes:
For the initial guess, we might have used the average of the first numbers – but what it we were processing an empty file? If you are guaranteed that the file will contain at least four numbers, this initialization is fine.

Answer to the question:

The first version is preferred because all the input is used as –one– test. Using the second version makes us (the graders) think that you do not believe that the first version even works. If the first input fails, the STRM_VAR variable enters fail state and none of the other three attempts at input will succeed. Even if there were some reason to process the first integer on the line differently taking the other three makes sense. The problem states clearly that there are always going to be four inputs on a line. Why only take one?

Another NOTE: Q: why isn’t there a function called findAverageOf that takes the four values and returns the average? A: there probably should have been!

Write a function that returns the average of a rectangular part of a 2D array of ints.
The caller must specify the upper left and lower right corners. Assume that these will always be valid values for the 2D array passed in. Q: Do you have to count the number of elements in order to do the average = sum/quantity calculation? A: No Q: Do you have to loop over the rows or columns as the outer loop? A: Either will do. Typically we do the rows as the outer loop and the columns as the inner loops so that the array is more intuitively accessed as: arr[row][col].

Write a function that fills an array of structs of this shape from user input.

struct threeThings {

int integer;

string thing;

double size;

};

The function should be passed the number of data records (the contents of one struct is one data record) that must be taken from the user. Assume the user will not make any mistakes when entering data and will always enter the amount specified by the caller. Q: is this a for or while situation? A: for . Note that normally this would be a while statement since we don’t normally know how many inputs a user will do (or how many things are in an input file).

Rewrite the previous to allow the user to hit ^Z to indicate that they are done even if they have not entered as many as the caller wanted. The number of complete inputs should be returned to the caller.
(this would be the normal way)

NOTE:

There might also be much simpler questions like: write the word hello on the screen twelve times.

[GO ON TO THE NEXT PAGE]


Given these prototypes (declarations):

void getFilenameFromUser( string& );

// Preconditions

// none

// Postconditions

// parameter will contain a string typed

// from the user

bool openFileForInput( ifstream &, const string& );

// Preconditions

// stream variable is not opened and is not in fail state

// string contains name do data file to be opened

// Postconditions

// returns true if file was successfully opened,

// false otherwise

void showArrayElements( const double doubleArray[],

const size_t numberToProcess,

ostream & streamVar = cout );

// Preconditions

// doubleArray contains numberToProcess doubles

/ streamVar has been successfully opened and is not in fail state

// Postconditions

// Each double in the array is written to streamVar using

// default formatting

size_t fillArrayFromStream( double doubleArray[],

const size_t capacity,

istream & streamVar = cin );

// Preconditions

// streamVar has already been successfully

// opened for input

// Assumption for input file: each line contains

// exactly one double value

// Postconditions

// The array is filled with data from file

// The quantity of doubles placed into the array is returned

// If more than capacity doubles are found in the file,

// they are read from the file but are not placed into the array

// streamVar is in fail state due to reading all of file contents


Write the main function to solve this problem:

·  The user will specify the name of a data file that contains only double values and whitespace.

·  If the file can't be opened,

o  Display an error message stating this

·  otherwise

o  Display the doubles from the file in reverse order on the screen.

o  If there are more than 500 doubles in the file, only show the first 500 of them in reverse order and ignore the rest

Do NOT write the definitions of the functions we have given you.

Just write the definition for main – the variables and statements (including calls to these given functions) needed to solve the problem.

IT’S LIKELY THAT YOU COULD GET A PROBLEM (or part of larger problem) WHERE WE GIVE YOU PROTOTYPES WITH PRE- and POSTCONDITIONS AND YOU WILL HAVE TO USE (CORRECTLY!!!) THE FUNCTIONS WE GIVE YOU.

MAKE SURE THAT IF WE GIVE YOU A FUNCTION TO USE - YOU DO NOT DEFINE IT.
THAT WOULD BE A WASTE OF YOUR TIME.

WE WOULD TRY TO MAKE THIS CLEAR ON THE TEST.

IF WE GIVE YOU A PROTOTYPE AND SAY YOU MUST USE THAT FUNCITON IT MEANS YOU MUST CALL THAT FUNCTION, NOT DEFINE IT.

(Sort of like when you use the sqrt( ) function, we have never ever asked you do define it!)