QUESTION 1 a) 50 7 6 b) 8 7 3 c) 1,1:2,1:2,2:3,1:3,2:3,3 d) ###5####2.44 ----------------------------------------------------------------------------------------------------------------- QUESTION 2 a) cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << setfill ('0') << setw(10) << x << endl << y << endl; b) double data[20], sum; int counter, counter1; for (counter = 0; counter <= 19; counter++) { sum+=data[counter]; } c) z%5 == 0 //expression is true if evenly divisible by 5 ----------------------------------------------------------------------------------------------------------------- QUESTION 3 a) bool checkForSquares (int values[], int maxvalues){ int count1, count2; for (count1 = 0; count1 < maxvalues; count1++){ for (count2 = 0; count2 < maxvalues; count2++){ if (values[count1]*values[count1] == values[count2] && values[count2] != values[count1]){ return 1; } } } return 0; } ----------------------------------------------------------------------------------------------------------------- QUESTION 4 int main (void) { ifstream xin; int test[100], counter, counter1, counter2; xin.open("C:\\Users\\Marco\\Documents\\ECOR1606 Assignments\\data.txt"); if (xin.fail()){ cout << "File cannot be opened" << endl; system ("PAUSE"); return 0; } for (counter = 0; counter < 101 ; counter++) { xin >> test[counter]; if (xin.fail()){ if (!(xin.eof())){ cout << "File contains bad data" << endl; system ("PAUSE"); return 0; } else { break; } } } if (counter > 99) { cout << "File contains too many values" << endl; system("PAUSE"); return 0; } else if (counter < 9) { cout << "File contains too few values" << endl; system("PAUSE"); return 0; } else { for (counter1 = 0; counter1 <= counter; counter1++) { for (counter2 = 0; counter2 < counter; counter2++) { if (test[counter1] == test[counter2] && counter2 != counter1) { cout << "File contains at least one duplicate value" << endl; system("PAUSE"); return 0; } } } } cout << "File is OK" << endl; system("PAUSE"); return 0; } ----------------------------------------------------------------------------------------------------------------- QUESTION 5 a) void getMonthAndDay (int year, int dayNumber, int &month, int &dayOfMonth) { int monthcounter = 1, daysofMonth; for (monthcounter = 1; monthcounter <= 12; monthcounter++) { daysOfMonth = daysInMonth (counter, year); if (dayNumber - daysInMonth > 0) { dayNumber-=daysInMonth; } else { month = monthcounter; dayOfMonth = dayNumber; break; } } } ----------------------------------------------------------------------------------------------------------------- QUESTION 6 a) int main (void) { int week, days, hours; //variables for calculation bool invalid; //tests for whether input is valid cout << "Please enter number of weeks, days, hours: "; //prompt user for input do { cin >> week >> days >> hours; //intake input if (cin.fail() || week < 0 || days < 0 || hours < 0) { //if at any moment, there is an invalid input cout << "Invalid entries were detected. Please try again: "; //notify user to re enter all values cin.clear(); //reset flag cin.ignore (INT_MAX, '\n'); //clear the input line invalid = true; //set invalid input boolean to true, so that program will not continue } else { // no problems invalid = false; //allow loop to exit } } while (invalid == true); //loops while there is invalid input hours = totalHours(week, days, hours); //calculate total hours cout << "Total hours: " << hours << endl; // output total hours system ("PAUSE"); return 0; } ** USE BELOW FUNCTION TO TEST CODING int totalHours (int week, int days, int hours) { return (week*7*24)+(days*24) + hours; } b) int main (void) { int week, days, hours; //variables for calculation bool invalid; //tests for whether input is valid cout << "Please enter number of hours: "; //prompt user for input do { cin >> hours; //intake input if (cin.fail() || hours < 0) { //if at any moment, there is an invalid input cout << "Invalid entries were detected. Please try again: "; //notify user to re enter all values cin.clear(); //reset flag cin.ignore (INT_MAX, '\n'); //clear the input line invalid = true; //set invalid input boolean to true, so that program will not continue } else { // no problems invalid = false; //allow loop to exit } } while (invalid == true); //loops while there is invalid input sortHours(week, days, hours); //calculate total hours cout << "Weeks: " << week << " Days: " << days << " Hours: " << hours << endl; // output total hours system ("PAUSE"); return 0; } ** USE BELOW FUNCTION TO TEST CODING void sortHours (int &week, int &days, int &hours) { week = hours/(24*7); hours-= week*(24*7); days = hours/24; hours-=days*24; } Reasoning: If any of the integers is less than 0, no way of sorting. Takes care of invalid input such as letters or real numbers Since for part b, there needs to be more than one return from the function, disregarding useless variables and assuming function uses call by reference. ----------------------------------------------------------------------------------------------------------------- QUESTION 7 a) int numOccurences (int array[], int size, int value) { //accepts an array, size of array, and value for searching int check, occurences = 0; for (check = 0; check < size; check++) { //loop checks every value of array against that of value if (array[check] == value) { occurences++; //every time there is a match, increase count } } return occurences; //return number of occurences } b) void mostCommon (int array[], int size, int &element, int &mostoccur) { //call by reference to most occuring element, and number of occurences for that element int occurences = 0, count; //variables to compare occurences for (count = 0; count < size; count++) { //test every value of the array to see number of occurences for each value occurences = numOccurences(array, size, array[count]); //find number of occurences for array to a value of the array if (count == 0 || occurences > mostoccur) { //if occurence for a value is greater than the max, or first run element = array[count]; //initialize most occuring element back in the main function mostoccur = occurences; //do the same for the number of occurences } } } ----------------------------------------------------------------------------------------------------------------- QUESTION 8 PEAR 27 APPLE 2 5 3 30 ORANGE 12 4 0 MELON 12 1 MELON 12 2 MELON 11 1 MELON 11 2 ----------------------------------------------------------------------------------------------------------------- QUESTION 9 cout << "Enter n: "; cin >> n; cout << "Enter x: "; cin >> x; if (n >= 0) { k = 0; res = 0; tmp = x; while (k <= n) { res += tmp; tmp *= (-x)/(2*(k+1)); tmp *= x/(2*(k+1)+1); k++; } cout << "The result is" << res; } else { cout << "n must be greater than 0" << n; } system("PAUSE"); ----------------------------------------------------------------------------------------------------------------- QUESTION 10 double zeta (int n){ int counter, total = 1; for (counter = 2; counter <= n; counter++) { if (isPrime(counter)){ total*=1/(1-pow((1/counter),2)); } } return total; } ----------------------------------------------------------------------------------------------------------------- QUESTION 11 Part I) double findMinimumTemperature (double temperatures[], int actualDays) { int counter, mintemp = 0; for (counter = 0; counter < actualDays; counter++) { if (counter == 0 || temperatures[counter] < mintemp) { mintemp = temperatures[counter]; } } return mintemp; } Part II) void findLongestColdSnap (double temperatures[], int actualDays, int &longestColdSnapDuration, int &longestColdSnapStart) { int day, duration = 0, start = 0, counter = 0; longestColdSnapDuration = 0; for (day = 0; day < actualDays; day++) { if (temperatures[day] < -10) { duration++; if (duration == 1) { start = day + 1; } } else { if (duration > longestColdSnapDuration) { longestColdSnapDuration = duration; longestColdSnapStart = start; } duration = 0; } } }