Question

In: Computer Science

How can I make sure (test) a reverse program that returns the same array with the...

How can I make sure (test) a reverse program that returns the same array with the same values as you entered before, but just in reverse order?

Example of the program im talking about:

void reverseArray(int arr[], int start, int end)

{

    int temp;

    while (start < end)

    {

        temp = arr[start];   

        arr[start] = arr[end];

        arr[end] = temp;

        start++;

        end--;

    }   

}  

Solutions

Expert Solution

Explanation:

How the function reverseArray works :

Note: Here start and end are not elements they are indexes

Code for testing function :

#include<iostream>
using namespace std;
// function for reverxing the array
// taking argumrnts array,startindex, end index
int revArray(int arr[] , int start,int end){
   int temp ;
   // loop for swaping first element and last element until
   // start < end
   // it results the sorted array
   while (start < end){
       temp = arr[start];
       arr[start] = arr[end];
       arr[end] = temp;
       // after swaping increment start value and decrement end value
       end--;
       start++;
    }
}
int main(){
   // initialising array
   int arr[20],size,i;
   // asking size of the array
   cout << "Enter size of the array: " ;
   // storing length in size
   cin >> size;
   // asking user to enter elements in the array
   cout << "\nEnter elements of the array: ";
   for(i=0 ; i<size ; i++){
       cin >> arr[i];
   }
   // printing array
   cout << "Original Array is : " ;
   for (i=0 ; i<size ; i++ ){
       cout << arr[i] << " " ;
   }
  
   // calling the function revArray
   // by passing array and initial index 0 and the end index
   // which is equal to the size-1
   revArray(arr,0,size-1);
   // printing the same array but this time it prints in reversed order
   // beacuse we called the functioin revArray() so elements are reversed and prints here
   cout << "\nReversed Array is : " ;
   for (i=0 ; i<size ; i++ ){
       cout << arr[i] << " " ;
   }
}

Attachments:

Output:

Any queries comment, please

Thank you :)


Related Solutions

can you detail explain how solved: "is just to practice" and make sure I have same...
can you detail explain how solved: "is just to practice" and make sure I have same answer You are deputy chief of the Space Federation Force (SFF), which means you do whatever the chief of the SFF says. She says you evaluate at 10% per year unless told otherwise. Space Fuel Inc. is considering establishing a new propellant depot to provide space vehicles a refueling point in their trek to Mars. If placed in a LaGrange point, the depot could...
**I need to make this program to make sure that the numbers are unique. In another...
**I need to make this program to make sure that the numbers are unique. In another word, if the number has been generated and appear early, it should continue to find the next number until the number is unique. Hint: use the array to check whether the number has been generated. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int n, low, high; cout << "Enter how many random numbers you would like to see:...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
Create a program that calculates the average of 3 test scores. Make use of an array...
Create a program that calculates the average of 3 test scores. Make use of an array to store the integer scores. const int size = 3; int testScores[size]; Send this array to a function that actually calculates and returns the average. 1. Tell the user what the program does. 2. Prompt the user to enter the integer scores. ( Use a for loop to do this. ) 3. Create and implement a function with prototype: double average( int a[], int...
This program is in C++ Please kindly make sure that the output exactly matches the test...
This program is in C++ Please kindly make sure that the output exactly matches the test case so I can vote a thumbs up. If it doesn't the program is wrong. Test Cases and the given code are given after the prompt Prompt: Modify the given code to: 1.. Store the data in Binary file and access it   in Random Access mode.   2.Replace Class with Structure for Employee and Department. 3. Inside each structure, replace all string variables with array...
can you detail explain how solved: "is just to practice" and make sure I have the...
can you detail explain how solved: "is just to practice" and make sure I have the same answer is from the economy for engineer and scientists You are deputy chief of the Space Federation Force (SFF), which means you do whatever the chief of the SFF says. She says you evaluate at 10% per year unless told otherwise. Space Fuel Inc. is considering establishing a new propellant depot to provide space vehicles a refueling point in their trek to Mars....
What test can be done after the preparation of aspirin to make sure that the desired...
What test can be done after the preparation of aspirin to make sure that the desired product was obtained? Can the test tell you if it is pure?
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT