A) Fix any errors to get the following program to run in your environment. B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does.

You may also add white space or reorder the code to suit your own style as long as the intended function does not change.

#include
using namespace std;

void m(int, int []);
void p(const int list[], int arraySize)
{
list[0] = 100;
}

int main()
{
int x = 1;
int y[10];
y[0] = 1;

m(x, y);

cout << "x is " << x << endl;
cout << "y[0] is " << y[0] << endl;

return 0;
}

void m(int number, int numbers[])
{
number = 1001;
numbers[0] = 5555;
}

Respuesta :

Answer:

Answer is explained below

Explanation:

Part A -:

Error statement -:

/*

prog.cpp: In function ‘void p(const int*, int)’:

prog.cpp:7:15: error: assignment of read-only location ‘* list’

list[0] = 100;

*/

There is one error in the code in following part

void p( const int list[], int arraySize)

{

list[0] = 100;

}

you are passing list as constant but changing it inside the function that is not allowed. if you pass any argument as const then you can't change it inside the function. so remove const from function argument solve the error.

Part B -:

change made

void p( int list[], int arraySize)

{

list[0] = 100;

}

Executable fully commented code -:

#include <iostream> // importing the iostream library

using namespace std;

void m(int, int []); // Function declearation of m

void p( int list[], int arraySize) // definition of Function p

{

list[0] = 100; // making value of first element of list as 100

}

int main()

{

int x = 1; // initilizing x with 1

int y[10]; // y is a array of 10 elements

y[0] = 1; // first element of y array is 1

m(x, y); // call m function

// printing the desired result

cout << "x is " << x << endl;

cout << "y[0] is " << y[0] << endl;

return 0;

}

void m(int number, int numbers[]) // Function definition of m

{

number = 1001; // value of number is 1001 locally

numbers[0] = 5555; // making value of first element of numbers array 5555

}

Part C :-

In program we initilize x with value 1 and create an array y of 10 elements.

we initilize the y[0] with 1\

then we call function m. In function m ,first argument is value of x and second argument is the pointer to the first element of array y.

so value of x is changed locally in function m and change is not reflected in main function.

but value of y[0] is changed to 5555 because of pass by refrence.

So we are getting the following result :-

x is 1

y[0] is 5555

ACCESS MORE
EDU ACCESS
Universidad de Mexico