Exam 2 Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output of the following fragment? Assume that the fragment is embedded in a complete program. ... int x=214, y=100; cout << "&"; cout.width(5); cout << "My favorite number is " << x << endl; cout << "Which one is yours?"; cout << "Mine is " << setw(5) << y << endl; ...

(Please not the space amount between the words): &My favorite number is 214 Which one is yours? Mine is 100

What is the difference between "\n" and '\n'?

-"\n" is a string -'\n' is a single character

What is the difference between the call-by-value and the call-by-reference?

-In the call by value, the value of each actual parameter is copied in the corresponding formal parameter which acts as a copy of the corresponding actual parameter. Unless we talk about the value returned by the function, any change performed by the function in the value of a variable will remain local and it will not be reflected in the original variable. -In the call by reference, the memory address of the actual parameter is passed to the function. The memory address can be used to access the original memory location of the actual parameter. Therefore any change generated by the function to that variable will performed directly in the original variable location.

What is the precondition and the postcondition for the function: double Foo(int x, int& y) { double z=x*y; return z; }

-Precondition: Both x and y must be available -Postcondition: x is the product of x and y

Explain the difference between the following flags: a. ios:: fixed b. ios:: showpos c. ios:: showpoint d. ios:: left

-a. If this flag is set, floating point numbers are not written in e notation. -b. If this flag is set, the plus sign will be shown in front of positive numbers. -c. If this flag is set, a decimal point and trailing zeros are always shown for floating point numbers -d. If this flag is set the next item will be left justified. This flag is used in conjunction with some field width value (obtained by calling the member function width). The result is that the next item in output will be at the left end of the space specified by width.

Answer the following questions: a. Can get be used to read from a file or from the display? b. Give an example of application of get. c. What is the peculiarity of the function get?

-a. It can be used to read from both. -b. Look at the example in question 29 and 30 -c. It read one character at a time including the blank space and the carriage return character.

Consider the following code fragment char next; int count = 0; cout << "Enter a line of input: \n"; cin.get(next); while( next != '\n') { if ((count%2) = = 0) //if count is even cout << next; count++; cin.get(next); } a. If the dialog begins as follows, what will be the next line of output? Enter a line of input: abcdef gh b. If the dialog begins as follows, what will be the next line of output? Enter a line of input: 2 4 6 8 10 12 14

-a. ace h -b. 24681 21

Write the function definition of Foo that receives two parameters, the first passed by value and the second passed by reference, and that returns the product of the two numbers?

//function definition double Foo(int x, int& y) { double z=x*y; return z; }

Write the function declaration used to input data from the keyboard into the array. Call the function myinput?

Here is an example #include <iostream> using namespace std; const int SIZE = 5; void myinput(int x[], int y) { for (int i = 0; i < y; i++) { cout << "insert an integer x "; cin >> x[i]; } } int main() { int a[SIZE]; myinput(a, SIZE); for (int i = 0; i < SIZE; i++) { cout << " a[" << i<< "] = "; cout << a[i] << endl; } system("pause"); return 0; }

Give an example of an array passed to a function. Write the call to the function

Here is one of many multiple examples that can be written. void myFunction(int myIntArr[], int size); //the function declaration int myIntArr[4] = {1,2,3,4}; int size = 3; myFunction(myIntArr, size); // the call

Which of the following function prototypes are not valid? void Foo(int& x, double y); void Foo(int &x, double y); void Foo(int&x, double y);

None. All three are correct.

Write a code fragment that reads a line of text and echo the line with all the uppercase letters deleted ("deleted" in this context means "removed", not simply turned into lower case).

Note that tolower returns an integer, therefore it must be casted back to a char int main() { cout<<"Enter a line of input and I will " << "echo it.\n"; char symbol; do { cin.get(symbol); cout << static_cast<char>(tolower(symbol)); } while (symbol != '\n'); return 0; }

It is acceptable to have both call-by-value and call-by-reference parameters in the same function declaration. (TRUE/FALSE)

True

When writing a function that is expecting a pass by reference parameter, what parts of the arrays can be passed (i.e. the index variable, the size of the array or the number of indexed variables with valid data).

Typically when passing an array by reference we expect to modify the content of the array. Therefore we need to pass the name of the array, its original declared size, and finally the variable that holds the number of the elements used so far.

When is required to use the const modifier in a function declaration?

We use the const modifier in a function declaration to guarantee that the argument we are passing by reference is not modified within the function

What is the difference between cout << c; and cout.put(c); ?

No difference

What output will be produced after running this code (assume the fragment is embedded in the complete program that includes directives ect...) ... ifstream ins; ins.open("list.dat"); int count = 0, next; while (ins >> next) { count++; cout << next << endl; } ins.close( ); cout << count; ... The list.dat file contains the following 3 numbers: 1 2 3

1 2 3 3 The first 3 numbers are from reading the file, the last number is the total count of all the elements read from the file

Given the following function definitions and program fragments, what is the output? void f2(int& a, int &b) { int temp; temp = b; b = a; a = temp; } void f1( int& a, int& b) { if( a > b) f2(a,b); else a = b; } int x = 5, y = 3; f1(y,x); cout << x <<" " << y << endl; x = 3, y = 7; f1(y,x); cout << x <<" " << y << endl;

5 5 7 3

What is a driver program?

A driver program is a program that allows testing of individual functions. Typically the main is a driver program.

What is a stub?

A stub is a function that has not been yet tested. A stub is a simplified version of a function

What is a void function?

A void function is a function that doesn't return any value

What is an object?

An object is an instance of a class. For example in the declaration: ifstream instr; instr is an object of the class ifstream. Objects are special variables that have their own special-purpose functions

Given the following function declaration and local variable declarations, list which of the followings is a correct function call? int myInt; float myFloat; char myChar; void myFunction(int& first, float second, char third); a. myFunction(6, 3.2, myChar); b. myFunction(myInt, myFloat, myChar); c. myFunction(myInt, 2.0, 'c'); d. myFunction(myInt, 20, 'q'); e. myFunction(myInt, myFloat, '1');

Assuming that the variables have been instantiated somewhere in the program, all the call, except a) are correct. The call a) is incorrect since the first argument is passed by reference, which required the name of the reference). The call d) should use a float instead of an int as second argument but C++ will convert it automatically to an int.

Change the following code so that the output goes to the screen #include <iostream> using namespace std; int main() { ifstream instream; ofstream outstream; instream.open("infile.dat"); outstream.open("outfile.dat"); int first, second, third; instream >> first >> second >> third; outstream << "The product of the first 3 numbers in \"infile.dat is " << (first*second*third) << endl; instream.close(); outstream.close(); return 0; }

Change outstream with cout: int main() { ifstream instream; //ofstream outstream; instream.open("list.txt"); //cout.open("outfile.dat"); int first, second, third; instream >> first >> second >> third; cout << "The product of the first 3 numbers in \"infile.dat is " << (first*second*third) << endl; instream.close(); return 0; }

Write a simple code fragment that asks the user to give the name of an output file. Attach the name to the ofstream my_output.

Example of a fragment .... char file_name[16];cout << "Enter the output file_name ";cin >> file_name;ifstream in_stream; ofstream my_output;in_stream.open("inputfile.dat"); my_output(filename);

Is the following code correct? If so, what does it declare? float scores[10], total;

It declares and array called scores of 10 elements of type float. Then it declares also a variable of type float called total.

What is the output of the following code? char mychar='t'; cout << toupper(mychar) << endl;

It prints 84 which is the ASCII for the upper case T

The following function definition has an error in it. What line is this error on? 0. void f1(const double array[], int size) 1. { 2. int t=0; 3. while(t< size) 4. { 5. array[t] += 4; 6. cout <<array[t]; 7. t++; 8. } 9. }

Line 5. It attempts to modify an array that is passed as a const

What is the value of myvar after the following function call? //function definition void myfunction(int& k) { k = 200; k++; } //fragment of main program int myvar=0; cout << myfunction(myvar);

Since myfunction is void, it does not return any value and therefore cannot be used in the cout command. However if used outside the cout command int myvar=0; myfunction(myvar); cout << myvar; then myvar would print 201

What does a precondition describe?

States what is assumed to be true when (immediately before) the function is called

What is the call-by-value mechanism?

The call-by-value mechanism is the substitution process in which the formal parameters are bound to the values of the corresponding actual parameters of the function call (frequently by copying the value into a new memory region)

What is the output of the following function? void calculateCost(int count, float& subTotal, float& taxCost); float tax = 0.0, subTotal = 0.0; calculateCost(15, subTotal, tax); cout << "The cost for 15 items is " << subTotal << ", and the tax for " << subTotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float& taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

The cost for 15 items is 3, and the tax for 3 is 0.3

What is the output of the following code char next[3] = { 'a','b','c'}; for (int i=0; i<=3; i++) cout << next[i];

The fragment prints a b c Ñ where Ñ is whatever is contained in the memory location next[3]. This is the case in which we try to print beyond the boundaries of an array. The system will not complain or produce error but it will print an unexpected value.

What is wrong with the following function body? void Foo(int x, float y, float& z) { if (x < 0) z = 0.0; else z = x*y; return 0; }

The function is void but it returned a value. That is not correct.

What is the fundamental rule for overloading a function?

The function must have the same name, return the same type and must have a different numbers or types of arguments.

What is the value of myvar after the following function call? //function definition int myfunction(int& k) { k = 200; return k; k++; } //fragment of main program int myvar=0; cout << myfunction(myvar);

The function returns 200. The call by reference change the original value of myvar to 200. If the function had used a call-by-value method then the value of myvar would have remained to 0.

Given the following function definition, will repeated calls to the search function for the same target find all occurrences of that target in the array? int search(const int array[], int target, int numElements) { int index=0; bool found=false; while((!found) && (index < numElements)) { if(array[index] == target) found=true; else index++; } if(found==true) return index; else return -1; }

The function returns the index position of the first element of the array that matches the target. Multiple calls of the search function will just return the same value result over and over.

Given the following function definition fragment, for which values of number should the function be tested? int doSomething(int number) { if(number < 0) { //do some stuff here } }

The function should be tested for values that are <0, for values that are >0, and for a value = 0 (we call this a "boundary condition").

What does a postcondition describe?

The postcondition describes the effect of the function call. It tells what will be true after the function is executed (when the precondition holds) If the function returns a value, that value is described. Also changes to call-by-reference parameters are described.

Is it possible to call a function from another function?

Yes it is possible (see example in question 16)

Consider the following code char c1, c2, c3, c4; cout << "Enter a line of input: \n"; cin.get(c1); cin.get(c2); cin.get(c3); cin.get(c4); cout << c1 <<c2 << c3 << c4 << "end of output"; If the dialog begins as follows, what will be printed in output? Enter a line of input: a b c d e f g

a b end of output

What are the indexed variables (members) of an array?

an example of an indexed variable is a[i] where a is the name of an array and i is the index. An indexed variable is a variable that is accessed via the use of an index.

What is the type of an input and an output stream?

an input stream is of type ifstream, an output stream is of type ofstream

Consider the following function definition: void f1(int a[], int size); { for (int i=0; i< size; i++) a[i]=2; } Which of the following are acceptable function calls? int a[25]; a. f1(a, 29); b. f1(a, 10); c. f1(a, 33); d. f1(a[3], 25);

b a and b are syntactically legal because problems since they attempt to pass a size of the array larger than the declared one. The last call is syntactically wrong since passes an int instead of an array.

Which of the following functions is a properly overloaded function of the following? int myfunct(int a, float b); a) int myfunct (int a, float b); b) float myfunct (int a, float b); c) int myfunct (int a, int b, float c); d) int myfunction(int a, float b);

c) int myfunct (int a, int b, float c);

Give the following declarations, check all the legal calls to this function? int myFunction(int myValue); int yourArray[200]; int myArray[100]; a. cout << myFunction(yourArray); b. cout << myFunction(yourArray[]); c. myArray = myFunction(yourArray); d. myArray[2] = myFunction(yourArray[3]);

d myArray[2] = myFunction(yourArray[3]);

Write a legal call to the displayOutput function defined as void displayOutput(int total);

displayOutput(total); or displayOutput(x); where x is an integer

Declare an array "pay" that contains the values 12000, 13000 and 14000.5.

float pay[3] = {1200, 1300, 1400.5}; or double pay[3] = {1200, 1300, 1400.5};

Write the code that reads values from the keyboard into the array? (Assume the size of the array is the constant SIZE).

int arr[3]; for(int index=0;index<3;index++) cin >> arr[index];

Write a small program that attaches an input stream to the input file "myFile.dat" and attaches an output stream to the output file "yourFile.dat".

int main() { ifstream instream; ofstream outstream; instream.open("myFile.dat"); outstream.open("yourFile.dat"); ... instream.close(); outstrean.close(); return 0; }

What is the purpose of the dot operator?

the purpose of the . operator is to access the member of a class. For example instream.close() uses the . operator to access the member function close() of the class ifstream for the object instream.


Conjuntos de estudio relacionados

Business Law for Managers Mindtap Questions

View Set

Linux Filesystem Administration (review questions) - [LINUX System Administration]

View Set

Respiratory NCLEX Questions Asthma and More

View Set

Testbank Questions: The Impact of Artificial Intelligence

View Set

Chapter 5 : Autonomic nervous system

View Set