Question

In: Computer Science

*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...

*/

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

Solutions

Expert Solution

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


Related Solutions

#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT