CS作业代写 CSCI 111 Final Exam Spring 2018 05.21.18

QUEENS COLLEGE Department of Computer Science
CSCI 111 Final Exam Spring 2018 05.21.18
08.30am – 10.30am, Monday, May 21, 2018
Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

Copyright By PowCoder代写 加微信 powcoder

int main() {
int z[3] = {0, 1, 2};
double r[3] = {1.9, 2.3, 3.0};
bool bl = true;
bl = a(bl, bl);
r[0] = b(z[0], r[1]);
z[2] = c(a(bl,bl));
d(d(z[0], z[1]), 5);
a(e(z[0] + z[1], r, z), bl);
(a) Title line for a. Answer:
bool a(bool x, bool y)
(b) Title line for b. Answer:
double b(int x, double u)
(c) Title line for c. Answer:
int c(bool x)
(d) Title line for d. Answer:
int d(int x, int y)
(e) Title line for e. Answer:
bool e(int x, double y[], int z[])

Problem 2 Consider the following C++ program. It is compiled to a.out and executed with the command ./a.out abcabc abc123.
#include
using namespace std;
int main(int argc, char *argv[]) {
string words[4] = {“A “, “very “, “easy”, “question “};
cout << words[1].substr(2) << endl; for (int i = 0; i <= 2; i++) cout << words[i][i]; cout << endl; cout << argv[1] << endl; cout << words[3].find("u") << endl; cout << argc << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the (b) What is the (c) What is the (d) What is the (e) What is the output at line (a)? output at line (b)? output at line (c)? output at line (d)? output at line (e)? Problem 3 Write blocks of code to perform the functions used in the following main program. Your blocks must match the given title lines. Each block should be a short function that is written only in the designated answer space. int main() { int x[5] = {3, 1, 4, 1, 5}; // (a) Return the average. Here 2.5 is printed. cout << average(2, 3) << endl; // (b) Return the middle of 3 numbers, here 5 is printed. cout << middle(5, 6, 4) << endl; // (c) Return the middle entry of an array with odd capacity. Here 4. cout << middleEntry(x, 5) << endl; // (d) Return the first index of 7 in the array or -1 if not present. Here -1 is printed. cout << findIndex7(x, 5) << endl; // (e) Return the upper case version of a lower case char. Here print H cout << toUpper(’h’) << endl; double average(int x, int y) { return (x + y) / 2.0; int middle(int x, int y, int z) { if ((x - y) * (x - z) <= 0) return x; if ((y - x) * (y - z) <= 0) return y; int middleEntry(int x[], int c) { return x[(c - 1)/2]; int findIndex7(int array[], int cap) { for (int i = 0; i < cap; i++) if (array[i] == 7) return i; return -1; char toUpper(char x) { return x + ’A’ - ’a’; Problem 4 Write a complete C++ program that does the following: It generates 250 random numbers between 1 and 1000. For each of the 250 numbers that has not been seen before it prints the number. Your program should not repeat any output value, random values should be computed with the C++ random number function rand() . This function should be called exactly 250 times. It is likely that fewer than 250 numbers will be printed. Excessively long or complicated code may lose points. #include
#include
using namespace std;
int main() {
bool seen[1001];
for (int i = 0; i < 1001; i++) seen[i] = false; for (int i = 0; i < 250; i++) { int n = rand() % 1000 + 1; if (!seen[n]) { cout << n << endl; seen[n] = true; return 0; } Problem 5 Write a function called rowSums. The function has two array parameters first and second the first is two dimensional with 5 columns and the second is one dimensional. The entries of both arrays have type int. Additional parameters specify the row and column counts for first . The function sets each entry second[r] to be the sum of the entries in row r of first . For example, a program that uses the function follows. int main() { int first[3][5] = {{9,9,8,1,0},{2,9,8,1,0},{1,1,8,1,0}}; int second[3]; rowSums(first, second, 3, 5); for(inti=0;i<3;i++)cout<
using namespace std;
int main(int argc, char *argv[]) {
string words[4] = {“Max”, “Freddy”, “Jack”, “Kelly”};
cout << words[1].substr(2) << endl; for (int i = 0; i <= 2; i++) cout << words[i][i]; cout << endl; cout << argv[2] << endl; cout << words[3].rfind("l") << endl; cout << argc << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the (b) What is the (c) What is the (d) What is the (e) What is the output at line (a)? output at line (b)? output at line (c)? output at line (d)? output at line (e)? Problem 3 Write blocks of code to perform the functions used in the following main program. Your blocks must match the given title lines. Each block should be a short function that is written only in the designated answer space. int main() { int x[5] = {7, 1, 4, 7, 5}; // (a) Return the average. Here 3.33333 is printed. cout << average(2, 3, 5) << endl; // (b) Return the smaller of 2 numbers, here 5 is printed. cout << smaller(5, 6) << endl; // (c) Return the next to last entry of an array. Here 7. cout << secondLastEntry(x, 5) << endl; // (d) Return the last index of 7 in the array or -1 if not present. Here 3 is printed. cout << findIndex7(x, 5) << endl; // (e) Return the lower case version of an upper case char. Here print h cout << toLower(’H’) << endl; double average(int x, int y, int z) { return (x + y + z) / 3.0; int smaller(int x, int y) { if (x <= y) return x; int secondLastEntry(int x[], int c) { return x[c - 2]; int findIndex7(int array[], int cap) { for (int i = cap - 1; i >= 0; i–)
if (array[i] == 7) return i;
return -1;
char toLower(char x) {
return x + ’a’ – ’A’;

Problem 4 Write a complete C++ program that does the following:
It generates 250 random numbers between 1 and 1000. For each of the 250 numbers that has been seen before it
prints the number.
Random values should be computed with the C++ random number function rand() . This function should be called exactly 250 times. If a random number is seen more than twice, it will be printed more than once.
Excessively long or complicated code may lose points.
#include
#include
using namespace std;
int main() {
bool seen[1001];
for (int i = 0; i < 1001; i++) seen[i] = false; for (int i = 0; i < 250; i++) { int n = rand() % 1000 + 1; if (seen[n]) cout << n << endl; seen[n] = true; return 0; } Problem 5 Write a function called colSums. The function has two array parameters first and second the first is two dimensional with 5 columns and the second is one dimensional with the same number of columns. The entries of both arrays have type int. Additional parameters specify the row and column counts for first . The function sets each entry second[c] to be the sum of the entries in column c of For example, a program that uses the function follows. int main() { int first[3][5] = {{9,9,8,1,0},{2,9,8,1,0},{1,1,8,1,0}}; int second[5]; colSums(first, second, 3, 5); for(inti=0;i<5;i++)cout<
using namespace std;
int main(int argc, char *argv[]) {
string words[4] = {“123”, “987654”, “9999”, “77777”};
cout << words[2].substr(2) << endl; for (int i = 1; i <= 3; i++) cout << words[i][i]; cout << endl; words[3] = argv[2]; cout << words[3] << endl; cout << words[3].find("9") << endl; cout << argc << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the (b) What is the (c) What is the (d) What is the (e) What is the output at line (a)? output at line (b)? output at line (c)? output at line (d)? output at line (e)? Problem 3 Write blocks of code to perform the functions used in the following main program. Your blocks must match the given title lines. Each block should be a short function that is written only in the designated answer space. int main() { int x[5] = {7, 1, 4, 7, 5}; // (a) Return the average. Here 3.33333 is printed. cout << average(2, 3, 5) << endl; // (b) Return the smaller of 2 numbers, here 5 is printed. cout << smaller(5, 6) << endl; // (c) Return the next to last entry of an array. Here 7. cout << secondLastEntry(x, 5) << endl; // (d) Return the last index of 7 in the array or -1 if not present. Here 3 is printed. cout << findIndex7(x, 5) << endl; // (e) Return the lower case version of an upper case char. Here print h cout << toLower(’H’) << endl; double average(int x, int y, int z) { return (x + y + z) / 3.0; int smaller(int x, int y) { if (x <= y) return x; int secondLastEntry(int x[], int c) { return x[c - 2]; int findIndex7(int array[], int cap) { for (int i = cap - 1; i >= 0; i–)
if (array[i] == 7) return i;
return -1;
char toLower(char x) {
return x + ’a’ – ’A’;

Problem 4 Write a complete C++ program that does the following:
It generates random numbers between 1 and 1000. As soon as it generates a number that is the sum of the two numbers right before it, it should print out this sum, report the total number of random numbers that have been generated and end.
Your program should produce output in this form:
626 = 154 + 472
Generated 307 Numbers
Random values should be computed with the C++ random number function Excessively long or complicated code may lose points.
#include
#include
using namespace std;
int main() {
int lastButOne = rand() % 1000 + 1;
int last = rand() % 1000 + 1;
int count = 2;
while (true) {
int n = rand() % 1000 + 1;
if (n == last + lastButOne) {
cout << n << " = " << last << " + " << lastButOne << endl; cout << "Generated " << count << " Numbers " << endl; lastButOne = last; return 0; } Problem 5 Write a function called rowPositive. The function has two array parameters first and second the first is two dimensional with 5 columns and the second is one dimensional. The entries of first have type int. Additional parameters specify the row and column counts for first . The function sets each entry second[r] to be true exactly when the entries in row r of first have a positive sum. For example, a program that uses the function follows. int main() { int first[3][5] = {{9,-9,8,1,0},{2,-9,-8,1,0},{1,-1,-8,1,0}}; bool second[3]; rowPositive(first, second, 3, 5); for (int i = 0; i < 3; i++) if (second[i]) cout << "Positive "; else cout << "Negative "; // prints Positive Negative Negative cout << endl; Excessively long or complicated code may lose points. void rowPositive(int first[][5], bool second[], int r, int c) { for (int i = 0; i < r; i++) { int sum = 0; for (int j = 0; j < c; j++) sum += first[i][j]; second[i] = sum > 0;

Problem 6 Write a function called numberMatch. The function has two integer parameters first and second that are positive and have the same number of digits. It returns the number of positions where their digits are equal. For instance numberMatch(1234,1894) would return 2, since the numbers match in their first and last digits. If parameters have illegal values your function can operate however you choose. Excessively long solutions
that use more than 5 lines of code may lose points. For example, a program that uses the function follows.
int main() {
cout << numberMatch(1628, 1328) << endl; cout << numberMatch(862, 862) << endl; cout << numberMatch(862, 628) << endl; // prints 3 // prints 3 // prints 0 int numberMatch(int first, int second) { if (second == 0) return 0; int ans = numberMatch(first / 10, second / 10); if (first % 10 == second % 10) return ans + 1; return ans; QUEENS COLLEGE Department of Computer Science CSCI 111 Final Exam Spring 2018 05.21.18 01.45pm – 03.45pm, Monday, May 21, 2018 Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions. int main() { double rr[3] = {0, 1.1, 2.2}; int zz[3] = {19, 23, 30}; char ch = ’a’; ch = a(ch, ch); zz[0] = b(rr[0], zz[1]); cout << 3 * c(a(ch,ch)); d(d(rr[0], rr[1]), 5); a(e(rr, rr[0] + rr[1], zz), ch); // (e) (a) Title line for a. Answer: char a(char x, char y) (b) Title line for b. Answer: int b(double x, int u) (c) Title line for c. Answer: int c(char x) (d) Title line for d. Answer: double d(double x, double y) (e) Title line for e. Answer: char e(double z[], double x, int y[]) Problem 2 Consider the following C++ program. It is compiled to a.out and executed with the command ./a.out PQRPQR PQR789. #include
using namespace std;
int main(int argc, char *argv[]) {
string words[4] = {“NOP”, “NOPQ”, “OPQR”, “MNOPQRS”};
cout << words[3].substr(2) << endl; for (int i = 0; i <= 3; i++) cout << words[i][i]; cout << endl; words[3] = argv[2]; cout << words[3] << endl; cout << words[3].rfind("R") << endl; cout << argc % argc << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the (b) What is the (c) What is the (d) What is the (e) What is the output at line (a)? output at line (b)? output at line (c)? output at line (d)? output at line (e)? Problem 3 Write blocks of code to perform the functions used in the following main program. Your blocks must match the given title lines. Each block should be a short function that is written only in the designated answer space. int main() { int x[5] = {3, 1, 4, 1, 5}; // (a) Return the average. Here 2.5 is printed. cout << average(2, 3) << endl; // (b) Return the middle of 3 numbers, here 5 is printed. cout << middle(5, 6, 4) << endl; // (c) Return the middle entry of an array with odd capacity. Here 4. cout << middleEntry(x, 5) << endl; // (d) Return the first index of 7 in the array or -1 if not present. Here -1 is printed. cout << findIndex7(x, 5) << endl; // (e) Return the upper case version of a lower case char. Here print H cout << toUpper(’h’) << endl; double average(int x, int y) { return (x + y) / 2.0; int middle(int x, int y, int z) { if ((x - y) * (x - z) <= 0) return x; if ((y - x) * (y - z) <= 0) return y; int middleEntry(int x[], int c) { return x[(c - 1)/2]; int findIndex7(int array[], int cap) { for (int i = 0; i < cap; i++) if (array[i] == 7) return i; return -1; char toUpper(char x) { return x + ’A’ - ’a’; Problem 4 Write a complete C++ program that does the following: It generates random numbers between 1 and 1000. As soon as it generates a number that is the square root of the number right before it, it should print out these two numbers, report the total number of random numbers that have been generated and end. Your program should produce output in this form: 19 = sqrt 361 Generated 20703 Numbers Random values should be computed with the C++ random number function Excessively long or complicated code may lose points. #include
#include
using namespace std;
int main() {
int last = rand() % 1000 + 1;

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com