Question

In: Computer Science

/* CIS 251 In Class Assignment    This program performs various operations on a ten element...

/*

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?

  • 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. 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.

  • 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.

Solutions

Expert Solution

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?

  • Fix the print function so that it works properly.

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;

}


Related Solutions

CompSci 251 Assignment 1 Due Sep. 23rd, 2019 Create a program that will format long pieces...
CompSci 251 Assignment 1 Due Sep. 23rd, 2019 Create a program that will format long pieces of text based on user input. User input will determine the number of indents allowed for the first line in each paragraph, the number of characters allowed per line, and the number of sentences allowed per paragraph. There are three pieces of text, where each is a famous speech that is represented by a single String variable at the top of the base file....
For this week's assignment analyze a family's spending habits by creating a program that performs arithmetic...
For this week's assignment analyze a family's spending habits by creating a program that performs arithmetic calculations. The program should ask the user to enter information for the following questions. How much does the family spend on groceries per month? How much does the family spend on dining out per month? How much does the family spend on entertainment per month? How much does the family spend on rent/mortgage per month? How much does the family spend on utilities per...
Write a program in Java which performs the sort operation. The main method accepts ten numbers...
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
Write a Python program that performs the following list operations. Part A Define a list called...
Write a Python program that performs the following list operations. Part A Define a list called numList with elements. 84, 94, 27, 74, 19, 90, 16, 21, 56, 50, 77, 59, 41, 63, 18, 26, 80, 74, 57, 30, 40, 93, 70, 28, 14, 11, 43,65, 91, 83, 22, 53, 74, 44, 73, 55, 47, 74, 81 Display the followings: All the numbers in numList The number of elements in numList The smallest number in numList The largest number in...
Write a Program(code) in Python that performs a large number of floating-point operations and an equal...
Write a Program(code) in Python that performs a large number of floating-point operations and an equal number of integer operations and compares the time required. Please attach an output.
I need a program(code) in any programming language that performs the following operations: union, concatenation and...
I need a program(code) in any programming language that performs the following operations: union, concatenation and conversion DFA-NDFA.
The sixth assignment involves writing a Python program to read in the temperatures for ten consecutive...
The sixth assignment involves writing a Python program to read in the temperatures for ten consecutive days in Celsius and store them into an array. The entire array should then be displayed. Next each temperature in the array should be converted to Fahrenheit and the entire array should be again be displayed. The formula for converting Celsius to Fahrenheit is °F = (°C × 1.8) + 32. Finally, the number of cool, warm and hot days should be counted and...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT