Unformatted text preview: MOCK EXAM QUESTION #1 What will be the output of the following code? #include<iostream> #include<fstream> using namespace std; int main() { ofstream out("temp.txt"); for(int i=1;i<=3;i++) out <<i<<" "<<i/2; out.close(); ifstream in("temp.txt"); double sum=0.0; for(int i=1;i<=3;i++) { double x; in >> x; sum+=x; } in.close(); cout << sum; return 0; } 16. In the file we write 1 02 13 1 and then we add the three first numbers of the file together. Hence: 1+2+13=16 QUESTION #2 A programmer wants to create a function that accepts an array with 10 elements and returns the average. However, the code is not correct. Why will this not work? Either rewrite the code or write which parts will make the algorithm fail. #include <iostream>
using namespace std;
int average(float array);
int n=10;
int main(){
float sum=0;
float array[10]={7.5, 10, 9.5, 8, 6.5, 5, 7, 3, 5, 10};
cout << "The average is " << average(array[10]);
} return 0; int average(float array){
for(int i=1;i<=10;i++)
sum+=array[i];
return sum/n;
}
Answer: The errors are: the function should be float average(…) since the average is usually a real number. The call of the function is wrong (it should be average(array)) remember that arrays when they are arguments are always called by name. Inside the function we should start the for loop at 0 (1st element of the array) and the condition should be strictly less than 10 (<) instead of less than or equal (<=). Last, sum is not declared inside the function. It should either be declared in there instead of in main or it should become a global variable. QUESTION #3 What will the output be? int main()
{
int *fibonacci;
fibonacci = new int [8];
fibonacci[0] = 1;
fibonacci[1] = 1;
for (int i = 2; i < 8; i++) {
fibonacci[i] = fibonacci[i1] + fibonacci[i2];
}
fibonacci = fibonacci + 1;
cout << *(fibonacci + 4)  *(fibonacci + 6);
return 0;
} Answer: 
13. QUESTION #4 What is the output of this code segment? int f[10] = {1,2,3,4,5,6,7,8,9,10}; double sum=0; for(int i=1;i<10;i++) { sum+=f[i]; cout << sum/10; Answer: 5.4. QUESTION #5 Assume that you are reading from a file called “runs.txt”. In there you find: # of runs time_of_run1 distance_of_run1 time_of_run2 distance_of_run2 time_of_run3 distance_of_run3 …… …… Create a function that reads from the file into two arrays. These two arrays should have exactly (# of runs) elements. void fun(float *time, float *dist){ ifstream file(“runs.txt”); bool done=false; int nRuns; file>> nRuns; time=new float[nRuns]; dist=new float[nRuns]; for(int i=0;i<nRuns;i++) file >> time[i] >> dist[i]; } QUESTION #6 Create a function that accepts the two arrays that you read before and computes the average speed of the runner. float speed(float *time, float *dist, int nRuns){ float sumtime=0; float sumdistance=0; for(int i=0;i<nRuns;i++){ sumtime+=time[i]; sumdistance+=dist[i]; } return sumdistance/sumtime; } QUESTION #7 Write a function that accepts a date of birth (string) and computes exactly how old the person is. For example, assuming the date is November 1st, 2011, the age of a person born on August 3rd, 1989 would be: 22 years, 2 months and 59 days ...
View
Full
Document
This note was uploaded on 11/16/2011 for the course COP 2271 taught by Professor Staff during the Fall '11 term at University of Florida.
 Fall '11
 Staff
 Computer Programming

Click to edit the document details