In: Computer Science
/*
CIS 251 In Class Assignment
This program performs various operations on a ten
element int array.
*/
#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:\n";
print (my_arr);
start (my_arr);
cout << "\n\nThe array after start is:\n";
print (my_arr);
move (my_arr, 2, 4, 6);
cout << "\n\nThe array after move is:\n";
print (my_arr);
add (my_arr, 3, 7);
cout << "\n\nThe array after add is:\n";
print (my_arr);
cout << "\n\n";
return 0;
}
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?
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. Explain why this is not a problem.
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.
Solution for the problems/ Errors of program that performs various operations on a ten element int array.
1. The function print is supposed to print each element of the array but does not work. What does it print?
Ans. Instead of printing the original array, it prints the values of z from 0 to 9.
Code given for print function:
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << z << " ";
}
Code after fixing the issue in Print function:
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << arr[z] << " "; //Need to write cout << arr[z] << " "; instead of cout << z << " ";
}
To print the values of array from the index 0 to 9, we need to
write 'cout << arr[z] << " "; '
instead of 'cout << z << " ";' .
2. Now that print has been fixed, run your program again. Why did “funny” values show up for the first call to print?
Ans. If we runs the program again after fixing the problem, we will get some 'funny' values show up for the first call to print because, we have not initialized/ added the array with some valid values. So it will Print some default values which are not predictable, for each run.
Screenshot after run again :
3. The array has different names in the functions than it does in main. Explain why this is not a problem.
Ans. In the given code, the same array is passed to different functions as a parameter. The name of parameters used in function definition and function call doesn't have to be same. Instead of name, the type of parameters and their order of passing are important and need to be same.
4. Would the program still work if the array had the same name in the functions as in main?
Ans. Yes. 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.
Ans. If we set count=0 instead of 17, the values 0,-1,-2,-3,-4,-5,-6,-7,-8,-9 are to be added in the array.
Screenshot of start 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.
Ans. If we remove the { } associated with the for statement in the function start, the values displayed will be the same for each index of the array. Eg:- if count=17, all the elements of the array will be contain 17. This is because, if we remove the {} of for statement, it will consider only the next first line as inside the for loop. Others will be considered as outside of the loop. So the line 'count--' will not be executed inside the loop.
7. Say that we have an array, my_arr, with the following values:
5 9 3 4 6 12 19 22 3 4
and the function move is called as follows: move (my_arr, 1, 3, 5); Show above how this call would change the array.
Ans: Screeshot of move (my_arr, 1, 3, 5);
8. Explain clearly what the function add does. Include the parameters first and last in your explanation.
Ans. Add function, void add (int arr [10], int first, int last), has 3 parameters, arr[10] as an array and int first and int last as two integer values.
In Add function it increments the values of array elements from first(can be any number passed from main function Eg:-3) index to last index(can be any number passed from main function Eg:-5) by 1.
That is if array: 17 14 15 16 13 14 11 10 9 8 and first =3 and last=5, Then
After Add function array will be: 17 14 15 17 14 15 11 10 9 8
9. How would add change the array if called as follows:
add (my_arr, 7, 2);
Ans. add (my_arr, 7, 2); statement does not make any change on the array, because, as first=7 and last=2, the second condition of for loop m <= last; fails in the first iteration itself(m=7; m<=2). So the array remains the same after this function call.
To add 'print_reverse' functionality to the program, we need to do the following things in the current program.
1. Add function prototype at the top of the program.
#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]);
2. Add function Definition to the program.
void print_reverse(int my_arr [10])
{
int z;
for (z = 9; z >=0; z--)
cout << my_arr[z] << " ";
}
3. print a label indicating that the array is being printed in reverse before the call in main function.
int main ()
{
------
-------
cout << "\n\nThe Reversed array is:\n";
---------
cout << "\n\n";
return 0;
}
4. Call the function print_reverse in main function
int main ()
{
------
-------
print_reverse(my_arr);
cout << "\n\n";
return 0;
}