mock_exam2 - MOCK EXAM QUESTION #1 Suppose you...

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 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: 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: 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 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 = 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: ...
View Full Document

Ask a homework question - tutors are online