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;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
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?
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y = new Ex1().change(x); 5. System.out.print(x+y); 6. } 7. int change(int x){ 8. x=12; 9. return x; 10. } 11. } Can you please explain this entire code and what is happening?
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;...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
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]); }
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
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...