MIDTERM_EXAM2solns[1]

MIDTERM_EXAM2solns[1] - MIDTERM EXAM #2 NAME:

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MIDTERM EXAM #2 NAME: _____________________________________ UFID: ______________________________________ QUESTION #1 (10 credits) What will the following code output? #include <iostream> #include <fstream> using namespace std; float fun (int &x, int &y); int main(){ int x=1; int y=1; cout << x << y << fun(x,y); return 0; } float fun (int &x, int &y){ float z=0; while(y<5){ z+=x*y; y++; x--; } return z; } Answer: The final output depends on the compiler. In GNU ­GCC, the compiler we are using in class, the result will be  ­35 ­10, since the function is executed first altering the values of x and y. However, it was considered also correct when a student would write 11 ­ 10, which would be the output in case the cout of the variables was to happen first. QUESTION #2 (15 credits) The following segment of code is supposed to accept from a user a file, read from it and store the information in an array. What is wrong with the segment? ofstream file1; float array[100]; char filename[100]; cin.getline(filename, sizeof(filename)); file1.open(filename); bool done=false; do{ file1 >> array[i]; }while(!done); Answer: First of all it is an ofstream object which means that the user is declaring a file for output reasons (in order to write to). So this should be ifstream. Secondly and most importantly the do.. while loop is an infinite loop, because the user has not thought to check for the end of file (using the file1.eof() method) so that the loop is exited at some point. Last, there is no variable i declared and it should be used inside a for loop or by increasing it, so that the other array elements are filled from the file. QUESTION #3 (10 credits) Given the following function prototypes: int fun1(int, int); float fun2(float a, float); void fun3(float); and the following variables: int a, b; float c; float x[100]; , which of the function calls are invalid and why? cout << fun3(c); float d=fun2(x, c); float d=fun2(x, c); cout << fun1(a, b); fun3(x[10]); fun3(x[100]); Answer: fun3(float) is void which means that there is nothing to be printed on screen. It is not an invalid call, but it is considered bad programming. fun2 accepts an array and a float, however the first call (fun2(x, c)) is invalid since array arguments are called by name. The next one is completely valid. fun1 returns an integer so printing that result on screen is perfectly valid. The same thing applies for fun3(x[10]). x[10] is a float number, an element of the array x. The last one though is trying to access an element outside the bounds of the array and thus is considered invalid. QUESTION #4 (15 credits) #include<iostream> #include<fstream> using namespace std; int main(){ ofstream out; out.open(“temp.dat”); for(int i=1;i<4;i++) out <<i<<" "<<i/2; out.close(); ifstream in("temp.txt"); // should be the same file as above.. double sum=0.0; for(int i=1;i<=3;i++) { double x; in >> x; sum+=x; } in.close(); cout << sum; return 0; } What does the above code do? What will be the output on screen? The result on the screen will be 16. Let us see why: the program opens a file to write to called temp.dat. In there it writes consecutively 1 and then leaves an empty space followed by ½=0, since i is an integer variable. Then, without leaving an empty space it prints on the file 2 and after the empty space 1. Finally, the file will have 1 02 13 1. The second part is trickier. It opens the file again though now for reading purposes. The thing is that now it sums up the three first elements of the file which are 1, 2 and 13. Their addition (16) is printed on screen. QUESTION #5 (10 credits) #include<iostream> using namespace std; int main(){ float x=0; float y=0; int array[10]; int name[10]={1, 3, 2, 4, 7, 8, 3, 4, 0, 9}; int *pntr; pntr=array; float *pntr2=&x; for(int i=1;i<4;i++) pntr[i]=name[10-i]; y=(*pntr2)++; pntr[5]=y; pntr[0]=x; for(int i=0;i<5;i++) cout << array[i]; } return 0; What will the output of the code be? What are the rest of the elements of the array by the name array? Answer: The output is the five first elements of the array named array. That is 19040. Let us see why. First of all we have two arrays, one named array and one named name with 10 integer elements each. Then, we declare a pointer called pntr and assign to it the address where array is being stored. Then a second pointer (pntr2) is assigned to point to the address of x. In the for loop that follows we assign to elements 1, 2 and 3 (that is the second, third and fourth elements) of the pointer pntr (that is the array named array) the 10th, 9th and 8th elements of the array name. So, array[1]=9, array[2]=0 and array[3]=4. Then we assign to y the value of the address where pointer pntr2 is pointing to (that is x) and then post increase x. Thus, pntr[5]=array[5]=0, and pntr[0]=array[0]=x=1. Finally we will have: array[0]=1, array[1]=9, array[2]=0, array[3]=4, array[4] is undefined. The rest of the elements as also array[4] are undefined. NOTE: Clearly the code is full of logical errors and fallacies. That is the reason why it was so difficult to read and understand. CODING PART (50 credits) Coding Question #1 (25 credits) Write a function that reads a polynomial from a file that the user gives and prints it out on the screen, i.e. if inside the file there are the values 2.2, 1.3, 4, 0, 1 it would print on screen: “The polynomial entered is 2.2x^4+1.3x^3+4x^2+0x+1.”. Take care of the possibility that the filename the user inputted does not exist. Answer: void function1(void){ ifstream file; char filename[100]; float poly[100]; float x; int i=0; cout << "Please give file name: "; cin.getline(filename, sizeof(filename)); do{ file.open(filename); }while(!file.fail()); bool done=false; do{ file >> x; if(file.eof()) done=true; else{ poly[i]=x; i++; } }while(!done); cout << "The polynomial entered is: "; for(int k=0;k<i;k++){ cout << poly[k] << "x^" << i-1-k; if(k<i-1) cout << "+"; } } Coding Question #2 (25 credits) Write a function that accepts from the user the number of inputs he wants to give and the corresponding values and writes the evaluation of the previous polynomial (use the same name array from before as an argument) in a file (poly_out.txt). i.e. The user gives 3 and then 1, 2, 3. The answer on the file will be: 8.5 62.6 250.3 Answer: void function(float poly, int length){ ofstream file2; file2.open("poly_out.txt"); int num; cin >> num; float values[100], eval[100]; for(int l=0;l<num;l++) cin >> values[l]; for(int k=0;k<num;k++){ eval[k]=0; for(int j=0;j<length;j++) eval[k]+=poly[j]*pow(values[k], length-j-1); file2 << eval[k] << endl; } } ...
View Full Document

Ask a homework question - tutors are online