int main int x 0 y 1 z 2 double b3 19 23 30 int d22 1234 cout twoDy b0 endl a

Int main int x 0 y 1 z 2 double b3 19 23 30 int d22

  • Queens College, CUNY
  • CS 111
  • Test Prep
  • samiul77
  • 233
  • 100% (2) 2 out of 2 people found this document helpful

This preview shows page 101 - 105 out of 233 pages.

the blocks for the functions. int main() { int x = 0, y = 1, z = 2; double b[3] = {1.9, 2.3, 3.0}; int d[2][2] = {{1,2},{3,4}}; cout << twoD(y, b[0]) << endl; // (a) prints difference: 0.9 y = addUp(d[1][1], y); // (b) sets y to sum 4 + 1 cout << firstElt(d, 2, 2); // (c) prints last element: 1 b[2] = av(b, 3); // (d) sets as average setOne(b[2], d[0][0]); // (e) sets both to 1 return 0; } (a) Title line for twoD . Answer:
Image of page 101
double twoD(int a, double b) (b) Title line for addUp . Answer: int addUp(int x, int y) (c) Title line for firstElt . Answer: int firstElt(int d[][2], int r, int c) (d) Title line for av . Answer: double av(double b[], int cap) (e) Title line for setOne . Answer: void setOne(double &x, int &y) Problem 154 Consider the following C++ program. #include <iostream> using namespace std; string fun(int x) { string ans = "0123456789"; if (x <= 10) return "0"; if ((x <= 30) || (x > 10000)) return ans.substr(x % 10); if ((x >= 0) && (x < 100)) return "x+1"; return ans.substr(x%4, x%4); } int nuf(int &x) { cout << x << endl; x = x * x; return x; } int main() { int x = 2; cout << fun(2) << endl; // line (a) cout << fun(22) << endl; // line (b) cout << fun(222) << endl; // line (c) nuf(x); // line (d) cout << nuf(x) << endl; // line (e) } (a) What is the output at line (a)? Answer: 0 (b) What is the output at line (b)? Answer:
Image of page 102
23456789 (c) What is the output at line (c)? Answer: 23 (d) What is the output at line (d)? Answer: 2 (e) What is the output at line (e)? Answer: 4 16 Problem 155 Write a function called bigRow that calculates and returns the biigest possible sum of entries of any row in a 2-dimensional array. For example, a program that uses the function bigRow follows. int main() { int x[2][3] = {{3, 1, 4}, {1, 5, 9}}; cout << bigRow (x, 2, 3) << endl; // from the 2-d array x that has size 2 x 3, find the biggest row sum // output will be 15 since row #1 contains 1, 5 and 9 is biggest. return 0; } Answer: int bigRow(int x[][3], int r, int c) { int ans; for (int row = 0; row < r; row++) { int sum = 0; for (int col = 0; col < c; col++) sum += x[row][col]; if (row == 0 || sum > ans) ans = sum; } return ans; } Problem 156 Write a function called bond that the insert the digit 7 after any pair of zero digits in a positive integer parameter. For example, a program that uses the function bond follows. int main() { cout << bond(400) << endl; // prints 4007 cout << bond(401) << endl; // prints 41 cout << bond(4007) << endl; // prints 40077 cout << bond(400) + 1 << endl; // prints 4008 return 0; } Answer:
Image of page 103
int bond(int x) { if (x <= 0) return 0; if (x % 100 == 0) return 1000 * bond(x / 100) + 7; return 10 * bond(x / 10) + x % 10; } Problem 157 Write a complete C++ program that does the following. (Programs that correctly carry out some of the tasks will receive partial credit.) 1. It asks the user to enter an integer n that is between 1 and 22. 2. It terminates if the user supplies an illegal value for n . 3. It prints out a triangular picture with n rows like the one shown in the example (below). The triangle has a vertical left edge and a horizontal top edge. The left edge is formed from the letter A, next to it is a vertical line formed from the letter B, then one formed from the letter C and so on. The top edge is also formed from the letter A, just below it is a line formed from the letter B and so on as in the example.
Image of page 104
Image of page 105

You've reached the end of your free preview.

Want to read all 233 pages?

  • Spring '15
  • Alexander Ryba
  • Void type, Carriage return, cout

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture