MockExam _more pointer questions_

MockExam _more pointer questions_ - MOCK EXAM ...

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: 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; } 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: 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[i-1] + fibonacci[i-2]; } fibonacci = fibonacci + 1; cout << *(fibonacci + 4) - *(fibonacci + 6); return 0; } Answer: 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: 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. QUESTION #6 Create a function that accepts the two arrays that you read before and computes the average speed of the runner. 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.

Ask a homework question - tutors are online