In: Computer Science
*/
#include
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";
}
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);
Add comments to where it is needed
♥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.
1. The function print is supposed to print each element of the array but does not work. What does it print?
Ans)its is printing the value of z ranging from 0 to 9.But not the elements of an array.
?Fix the print function so that it works properly.
void print (int arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << arr[z] << " ";
}
2) 2.Now that print has been fixed, run your program again. Why did “funny” values show up for the first call to print?
Ans) here we just declared the array as int my_arr [10]; but we didn’t initialized the array with values.
Without initializing the array we are trying to print the elements in the array.Thats we got those funny values during the first call of the print() function.
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?
Ans)we are passing the reference of an array as argument to the function.We are manipulating the array in the start() function.Different named arrays get linked up as we are passing the reference of an array while calling the functions.
4) Would the program still work if the array had the same name in the functions as in main?
Ans) we have to change all the names of the arrays inside the functions where we are performing the operations on the array.otherwise the program will not get compiled and run.
Ex:
start (my_arr); here we are calling the function by passing the array name “my_arr” as argument.
If we change the names in the function
void start (int my_arr [10])
{
int index, count;
count = 17;
for (index = 0; index < 10; index++)
{
my_arr [index] = count;
//Here in the above line also we have to change the name.other wise our program will not get compiled and run.
count--;
}
}
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 count is set to 0,then every time while executing the statement count--;
The number ‘1’ is subtracted from the count value.which results in negative numbers.
Then finally the values in the array are 0 -1 -2 -3 -4 -5 -6 -7 -8 -9
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.
Ans) the value inside the count is 17.As we removed the opening and closing braces({ }) after the for loop.Then only the immediate line after the for loop statement will get executed.So every time the count value is stored into every element of the array.
Therefore it prints 17 17 17 17 17 17 17 17 17 17
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.
Ans)
5 9 3 4 6 12 19 22 3 4
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
The value my_arr[1]= 9 which will be stored into temp variable
my_arr[3]=4 is stored into my_arr[1] now my_arr[1]=4
my_arr[3]=4 is stored into my_arr[5] now my_arr[5]=4
after that the value inside temp(9) is stored inside my_arr[3]
now my_arr[3]=9
therefore the my_arr array elements are
5 4 3 9 6 4 19 22 3 4
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
8. Explain clearly what the function add does. Include the parameters first andlast in your explanation.
Ans)
When we call the function add (my_arr, 3, 7); code inside the function will get
executed.
Here the value of the first is 3 and the vaue of the last is 7
for (m = first; m <= last; m++)
my_arr [m]++;
The for loop will get executed (starting from m=3) until the m value become 7
Inside the for loop the corresponding index element is incremented by 1 value
Means
The actual value of my_arr[3]=13 after incrementing by 1 it will becomes 14
The actual value of my_arr[4]=14 after incrementing by 1 it will becomes 15
The actual value of my_arr[5]=15 after incrementing by 1 it will becomes 16
The actual value of my_arr[6]=16 after incrementing by 1 it will becomes 17
The actual value of my_arr[7]=17 after incrementing by 1 it will becomes 18
So finally the elements in the array are
17 16 13 15 16 13 14 11 9 8
9. How would add change the array if called as follows:
add (my_arr, 7, 2);
Ans) when we call the function add(my_arr,7,2)
Here the first=7 and the last =2
The m value in the for loop is m=7 then every time the condition m<=2 will fail.So the code inside the for loop will never get executed .So the value of an array will not change
Then the values inside the array is 17 16 13 14 15 12 13 10 9 8
10)
?Add a function prototype and definition for a function called print_reverseto 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.
Ans)
#include <iostream>
using namespace std;
void start (int my_arr [10]);
void move (int my_arr [10], int x, int y, int z);
void add (int my_arr [10], int first, int last);
void print (int my_arr [10]);
//Function declaration
void print_reverse(int my_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, 7, 2);
cout << "\n\nThe array after add is:\n";
print (my_arr);
cout << "\n\nThe array in reverse order:\n";
print_reverse(my_arr);
cout << "\n\n";
}
void start (int my_arr [10])
{
int index, count;
count = 17;
for (index = 0; index < 10; index++)
{
my_arr [index] = count;
count--;
}
}
void move (int my_arr [10], int x, int y, int z)
{
int temp;
temp = my_arr [x];
my_arr [x] = my_arr [y];
my_arr [z] = my_arr [y];
my_arr [y] = temp;
}
void add (int my_arr [10], int first, int last)
{
int m;
for (m = first; m <= last; m++)
my_arr [m]++;
}
void print (int my_arr [10])
{
int z;
for (z = 0; z < 10; z++)
cout << my_arr[z] << " ";
}
//Function implementation which displays the array in reverse
order
void print_reverse(int my_arr [10])
{
for(int i=9;i>=0;i--)
{
cout<< my_arr[i]<<"
";
}
}
________________Thank You