This preview shows page 1. Sign up to view the full content.
Unformatted text preview: MOCK EXAM QUESTION #1 Suppose you have a file DOB.txt which has the following information:
First_Name Last_Name DateofBirth (MM/DD/YYYY)
Write a program than access the file, stores the data available in arrays and then
creates a new file (called yrsold.txt) which has the last name, the initial letter of
the first name and the age. Also the program should output how many are over
21 years old.
Sample: DOB.txt:
Chrysafis Vogiatzis 06/21/1984
yrsold.txt:
Vogiatzis C. 27 QUESTION #2 What does the following code do? (either explain what it does or
provide a detailed example of how it would work for n<=5)
#include <iostream>
#include <cmath>
using namespace std;
int main(){
float x;
int n;
float array[5];
cin >> n;
for(int i=0;i<n;i++)
cin >> array[i];
float sum=0;
cin >> x;
for(int i=0;i<n;i++)
sum+=array[i]*pow(x,i);
cout << sum;
return 0;
} Answer: Polynomial. QUESTION #3 What will the output be (that is the value of x) after the execution of this code? Suppose that in the file we have the following values (separated by spaces and having more spaces at the end): 3 21 7 0 8 7 2 0 0 1 #include <iostream>
#include <fstream>
int main(){
ifstream file1;
ofstream file2;
int x;
int array[10];
file1.open("test.txt");
for(int i=0;i<10;i++)
file1 >> array[i];
file1.close();
file2.open("test1.txt");
for(int j=0;j<10;j++)
file2 << array[j];
file2 << " ";
file2.close();
file1.open("test1.txt");
file1 >> x;
cout << x;
return 0;
}
Answer: 32170872001 (or overflow, but still one number only) QUESTION #4 Write a function that has as an argument a boolean array with 8 elements. The function should convert the binary number contained in the array and return the integer result in the decimal system. Ex: if the Boolean array contains: False, False, True, False, True, True, False, True then the result would be 0*2^7+0*2^6+1*2^5+0*2^4+1*2^3+1*2^2+0*2^1+1*2^0 = 45 int convert (bool a[8]){ int sum=0; for(int i=0;i<8;i++) return sum; } if(a[i]) sum+=pow(2.0, 7 i); QUESTION #5 In the binary system, multiplying a number by 2 can be represented as shifting the contains of a boolean array to the left. For example, False, False, True, False, True, True, False, True = 45 after shifting to the left becomes False, True, False, True, True, False, True, False = 90 Write a function that accepts as argument the boolean array in the binary system and an integer number of shifts and shifts the first argument (the array) to the left as many times as the second argument tells us to. Ex: shifting two times to the left the array False, False, True, False, True, True, False, True = 45 gives us True, False, True, True, False, True, False, False void shift(bool a[8], int shifts){ for(int i=0;i<8;i++) if(i+shifts<8) a[i+shifts]=a[i]; for(int i=0;i<shifts;i++) a[i]=0;
} = 180 QUESTION #6 Suppose that you can create a function that accepts two boolean arrays and returns their addition. That is, False, False, True, False, True, True, False, True = 45 + False, True, False, False, False, True, False, False = 68 False, True, True, True, False, False, False, True = 113 Assuming that you have this function (call it add) along with the shifting function that you wrote before, sketch your ideas on how you would implement an algorithm that can multiply a binary number by 3, 5, 6, 7 and 9. (NO NEED FOR CODE HERE, LIST YOUR IDEAS AND YOUR THINKING PROCESS) Answer: for 3: shift the array once and then add the original one for 5: shift the array twice and then add the original one for 6: shift the array twice and then shift it once and add the two arrays together. and so on… ...
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