Question

A.6 ... static int x = 1; int y = x * 2; void t1() {...

A.6

...

static int x = 1;

int y = x * 2;

void t1() {

                y++;

                cout << "x: " << x << " | y: " << y << endl;

                y += 1;

                x -= -1;

}

void t2() {

                int* x = &y;

                cout << "x: " << x << " | y: " << y << endl;

}

void t3() {

                int y = x;

                static int x = 2;

                cout << "x: " << x + 1 << " | y: " << y + x << endl;

                x += y;

}

void t4() {

                int y = x + 1;

                int& z = y;

                z += -1;

                cout << "x: " << x + z << " | y: " << y << endl;

}

int main() {

                vector<int> vec1{ 1, 3, 5, 7, 9 };

                vector<int> vec2{ 2, 4, 6, 8, 10 };

                vec1.swap(vec2);

                int * ptr = &vec1[1];

                y = *(ptr + 2);

                t1();

                t2();

                t3();

                t3();

                t4();

                return 0;

}

This program outputs 5 lines. What are they?

1.

2.

3.

4.

5.

A.7 - 15 Points

int x = 1, y = -1;

void swapplus1(int n1, int n2) {

                int temp = n1 + 1;

                n1 = n2 - 1;

                n2 = temp;

                x = x + n1;

}

void swapplus2(int& n1, int& n2) {

                int temp = n1 + 1;

                n1 = n2 - 1;

                n2 = temp;

}

void swapplus3(const int& n1, const int& n2) {

                int n1val, n2val, temp = n1 + 1;

                n1val = n2 - 1;

                n2val = temp;

                y -= n2;

}

void swapplus4(int* p1, int* p2) {

                int temp = *p1 + 1;

                *p1 = *p2 + 1;

                *p2 = temp;

                x = *p1 + y;

}

void swapplus5(int* &p1, int* &p2) {

                int* temp = p1 + 1;

                p1 = p2 - 1;

                p2 = temp;

}

void print(const int& x, const int& y) {

                cout << "\n x: " << x << " |y: " << y;

}

int main() {

                int arr[]{ 2, 4, 6, 8, 10, 12, 14 };

                y = arr[3] / size(arr) ;

                swapplus1(x, y);      print(x, y);

                swapplus2(x, y);      print(x, y);

                swapplus3(x, y);      print(x, y);

                swapplus4(&x, &y); print(x, y);

                int *px = &x, *py = &y;

                (*px)--;

                (*py) -= -7;

                swapplus5(px, py); print(x, y);

                return 0;

}

This program outputs 5 lines. What are they?

1.

2.

3.

4.

5.

A.7

int x = 1, y = -1;

void swapplus1(int n1, int n2) {

                int temp = n1 + 1;

                n1 = n2 - 1;

                n2 = temp;

                x = x + n1;

}

void swapplus2(int& n1, int& n2) {

                int temp = n1 + 1;

                n1 = n2 - 1;

                n2 = temp;

}

void swapplus3(const int& n1, const int& n2) {

                int n1val, n2val, temp = n1 + 1;

                n1val = n2 - 1;

                n2val = temp;

                y -= n2;

}

void swapplus4(int* p1, int* p2) {

                int temp = *p1 + 1;

                *p1 = *p2 + 1;

                *p2 = temp;

                x = *p1 + y;

}

void swapplus5(int* &p1, int* &p2) {

                int* temp = p1 + 1;

                p1 = p2 - 1;

                p2 = temp;

}

void print(const int& x, const int& y) {

                cout << "\n x: " << x << " |y: " << y;

}

int main() {

                int arr[]{ 2, 4, 6, 8, 10, 12, 14 };

                y = arr[3] / size(arr) ;

                swapplus1(x, y);      print(x, y);

                swapplus2(x, y);      print(x, y);

                swapplus3(x, y);      print(x, y);

                swapplus4(&x, &y); print(x, y);

                int *px = &x, *py = &y;

                (*px)--;

                (*py) -= -7;

                swapplus5(px, py); print(x, y);

                return 0;

}

This program outputs 5 lines. What are they?

1.

2.

3.

4.

5.

Homework Answers

Answer #1

A6)

line 1)x: 1 | y: 9
line 2)x: 0x408030 | y: 10
line 3)x: 3 | y: 4
line 4)x: 5 | y: 6
line 5)x: 4 | y: 2

A7) assuming size(arr) return sizeof arr (or is typo for sizeof(arr)) and the function is already declared

output:   x: 0 |y: 0
x: -1 |y: 1
x: -1 |y: 0
x: 1 |y: 0
x: 0 |y: 7

if size(arr) return no of elements in arr:

output:    x: 1 |y: 1
x: 0 |y: 2
x: 0 |y: 0
x: 2 |y: 1
x: 1 |y: 8

if size() function is not declared then an error will be given

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int y){ x = (x*y) mod 3; y = y – x; } main(){ i = 0; a[0] = 1; a[1] = 2; a[2] = 0; f(i, a[i]); print(“%d %d %d %d\n”, i, a[0], a[1], a[2]); f(a[i], a[i]); print(“%d %d %d\n”, a[0], a[1], a[2]); }
2. Consider the following program written in C syntax: void swap(int a, int b) {   ...
2. Consider the following program written in C syntax: void swap(int a, int b) {    int temp;    temp = a;    a = b;    b = temp; } void main() {    int value = 2, list[5] = {1, 3, 5, 7, 9};    swap(value, list[0]);    swap(list[0], list[1]);    swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
1.    Given the following segment of code: (If there is nothing output, write None.) int x;...
1.    Given the following segment of code: (If there is nothing output, write None.) int x; int y; cin >> x; cin >> y; while (x > y) {     x -= 3;     cout << x << " "; } cout << endl;        a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]               Output                                                                                                                                                                                                    x = ______________                                                                                                                                                                                                   ...