#include <iostream> using namespace std; void start (int boxes [10]); void move
ID: 3784698 • Letter: #
Question
#include <iostream>
using namespace std;
void start (int boxes [10]);
void move (int squares [10], int x, int y, int z);
void add (int arr [10], int first, int last);
void print (int arr [10]);
int main ()
{
int my_arr [10];
cout << "The original array is: ";
print (my_arr);
start (my_arr);
cout << " The array after start is: ";
print (my_arr);
move (my_arr, 2, 4, 6);
cout << " The array after move is: ";
print (my_arr);
add (my_arr, 3, 7);
cout << " The array after add is: ";
print (my_arr);
cout << " ";
}
void start (int boxes [10])
{
int index, count;
count = 17;
for (index = 0; index < 10; index++)
{
boxes [index] = count;
count--;
}
}
void move (int squares [10], int x, int y, int z)
{
int temp;
temp = squares [x];
squares [x] = squares [y];
squares [z] = squares [y];
squares [y] = temp;
}
void add (int arr [10], int first, int last)
{
int m;
for (m = first; m <= last; m++)
arr [m]++;
}
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << z << " ";
}
Questions and Experiments:
1. The function print is supposed to print each element of the array but does not work. What does it print?
Fix the print function so that it works properly.
2. Now that print has been fixed, run your program again. Why did “funny” values show up for the first call to print?
3. The array has different names in the functions than it does in main. This does not appear to be a problem. How do these different named arrays get linked up?
4. Would the program still work if the array had the same name in the functions as in main?
5. What values would be placed in the array by the function start if count was initially set equal to 0 instead of 17? Show the values.
6. Remove the { } associated with the for statement in the function start. Run your program and explain clearly why the output changes as shown.
Add the {} back to the program.
7. Say that we have an array, my_arr, with the following values:
5 9 3 4 6 12 19 22 3 4
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
and the function move is called as follows:
move (my_arr, 1, 3, 5);
Show above how this call would change the array.
8. Explain clearly what the function add does. Include the parameters first and last in your explanation.
9. How would add change the array if called as follows:
add (my_arr, 7, 2);
Explain your answer.
Add a function prototype and definition for a function called print_reverse to your program. print_reverse takes an array as a parameter and prints the array values in reverse order. Have your program call this function after the last print. Have main print a label indicating that the array is being printed in reverse before the call.
Explanation / Answer
1. Here we are printing only index value. We need to print the array value at the index. The corrected print method is below
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << arr[z] << " ";
}
2. when the array is created then its default values are indeterminate. that is the reason why we have funny values
3. arrays are passed as a pointer to the first element of the array. so there won't be any problem with the array parameters and arguments.
4. yes, the program would work
5. The array after start is:
0 -1 -2 -3 -4 -5 -6 -7 -8 -9
6. The array after start is:
17 17 17 17 17 17 17 17 17 17
when we remove {} associated with the for() statement then for() block behaves like it only contains the first followed line. so here, for() contains the below line only
for (index = 0; index < 10; index++)
boxes [index] = count;
so for all array index values, only 17 value is assigned.
later count is decreased by 1 and hence become 16 but that doesn't have any effect
7.
move (my_arr, 1, 3, 5);
temp = squares [x]; ------------------------ temp = my_arr [1]
squares [x] = squares [y]; ------------------ my_arr [1] = my_arr [3]
squares [z] = squares [y]; ------------------ my_arr [5] = my_arr [3]
squares [y] = temp; -------------------------- my_arr [3] = temp
5 4 3 9 6 4 19 22 3 4
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
8. the add function add 1 to the array values starting from the first index to last index
9. add (my_arr, 7, 2);
there is no change in the array as the first parameter 7 is not less than or equal to last paramter 2
#include <iostream>
using namespace std;
void start (int boxes [10]);
void move (int squares [10], int x, int y, int z);
void add (int arr [10], int first, int last);
void print (int arr [10]);
void print_reverse (int arr [10]);
int main ()
{
int my_arr [10];
cout << "The original array is: ";
print (my_arr);
start (my_arr);
cout << " The array after start is: ";
print (my_arr);
move (my_arr, 2, 4, 6);
cout << " The array after move is: ";
print (my_arr);
add (my_arr, 3, 7);
cout << " The array after add is: ";
print (my_arr);
cout << " The array being printed in reverse is: ";
print_reverse (my_arr);
cout << " ";
}
void start (int boxes [10])
{
int index, count;
count = 17;
for (index = 0; index < 10; index++)
{
boxes [index] = count;
count--;
}
}
void move (int squares [10], int x, int y, int z)
{
int temp;
temp = squares [x];
squares [x] = squares [y];
squares [z] = squares [y];
squares [y] = temp;
}
void add (int arr [10], int first, int last)
{
int m;
for (m = first; m <= last; m++)
arr [m]++;
}
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << arr[z] << " ";
}
void print_reverse (int arr [10])
{
int z;
for (z = 9; z >= 0; z--)
cout << arr[z] << " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.