Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Quick question

I would like to verify the following answers:

Given the following C++ code:
int x[10], y, z;
for (int i=0; i<9; i++)
x[i] = i+1;
y = x[8];
z = x[9];
What are the respective values for:
i=??
y=80
z=90

Given the following C++ function definition:
void foo(int a, int& b)
{ a=2; b=5; }
What would be the resulting values of the following piece of client code?
int x = 0;
int y = 0;
int a;
foo(x,y);
cout << x << a << y <<;
a=2
x=0
y=0

• Originally Posted by debbie_lee104
I would like to verify the following answers:

Given the following C++ code:
int x[10], y, z;
for (int i=0; i<9; i++)
x[i] = i+1;
y = x[8];
z = x[9];
What are the respective values for:
i=??
y=80
z=90

Given the following C++ function definition:
void foo(int a, int& b)
{ a=2; b=5; }
What would be the resulting values of the following piece of client code?
int x = 0;
int y = 0;
int a;
foo(x,y);
cout << x << a << y <<;
a=2
x=0
y=0
Walk it through...
Code:
```// Instansiate (create) three variables of the integer type
//  Label them x, y and z respectively.
//  x is going to be a static array with 10 indices (nodes or columns).
int x[10], y, z;

// Next two lines go together.  Use a for-loop to populate x.  Use an integer
// labeled i as the iterator.  When i is = (equal to) or > (greater than) 9, stop
// looping.  For each instance of the loop take the value of i (0 - 8), add one
// to i and set the resulting value of i as the value of the i'th index of x.  In
// other words.  First iteration of loop x[0] = (0 + 1) = 1, second iteration of
// loop x[1] = (1 + 1) = 2, third iteration of loop x[2] = (2 + 1) = 3, etc. etc.
for (int i=0; i<9; i++)
x[i] = i+1;
// Set y = (equal to) the value of the eigth index of x.
y = x[8];
// Set z = (equal to) the value of the ninth index of x.
z = x[9];```
Second problem...
Code:
```// Use a function called foo to evaluate the following:
// An integer named a which is passed by value (whatever the value of a is
// going into the function, a remains the same when it comes out of the
// function); i.e. - foo(100, 5) returns a=100, b=5.
// An integer named b which is passed by reference (whatever the value of b
// is going into the function, b gets changed by the function); i.e. -
// foo(5, 100) returns a = 5, b = 5
void foo(int a, int& b)
{ a=2; b=5; }
// What would be the resulting values of the following piece of client code?
// Instansiate (create) a variable of the integer type, label the integer as x
// and set x's value = (equal to) 0.
int x = 0;
// Instansiate (create) a variable of the integer type, label the integer as y
// and set y's value = (equal to) 0.
int y = 0;
// Instansiate (create) a variable of the integer type, label the integer as a
// and leave a as undefined.
int a;
// Call the function foo with x and y as the parameters.
foo(x,y);
cout << x << a << y <<;```
HTH,

-saige-

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•