Question

Write an algorithm and a C++ program that reverse the order of the elements of an...

Write an algorithm and a C++ program that reverse the order of the elements of an array that is assumed to have been previously entered ("the first will be the last ..."). The reversion must be performed without the usage of any intermediary array.

1 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main() {
    int size, temp;
    cout << "Enter size of array: ";
    cin >> size;
    int *arr = new int[size];

    cout << "Enter " << size << " elements: ";
    for (int i = 0; i < size; ++i) {
        cin >> arr[i];
    }

    for (int i = 0; i < size/2; ++i) {
        temp = arr[i];
        arr[i] = arr[size-i-1];
        arr[size-i-1] = temp;
    }

    cout << "Array in reverse is: ";
    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;

    delete[] arr;
    return 0;
}

Enter size of array: Enter 5 elements: Array in reverse is:971 2 4

Enter size of array: Enter 5 elements: Array in reverse is:971 2 4

Algorithm:

input size
input arr

for i from 1 to size/2
    swap arr(i) with arr(size-i-1)
end for

output arr
Add a comment
Know the answer?
Add Answer to:
Write an algorithm and a C++ program that reverse the order of the elements of an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • Write a C++ program that reads in 6 elements from the keyboard and stores them in...

    Write a C++ program that reads in 6 elements from the keyboard and stores them in an array x[6]. It should then create a second array y[6] that contains the elements from x in reverse order and with the s Tsecond - Tsmallest shown below. A sample run should look like: rocted ITrom it (e.g., Jlast first Tsmallest Vsecond last , etc). The program should finally print out both arrays to screen, in the format Enter 6 array elements: 4...

  • [C++] Write a program that reads a list of integers, and outputs those integers in reverse....

    [C++] Write a program that reads a list of integers, and outputs those integers in reverse. You must use an array for this lab. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 5 To achieve the above, first read the integers into...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Java Program: Write an algorithm (steps) using pseudocode (no java code required) explaining steps for a...

    Java Program: Write an algorithm (steps) using pseudocode (no java code required) explaining steps for a method which will reverse an integer array (Integer[]) without using any other data structure. The method should return the reversed integer array. E.g. If input array Integer[] integers = [1,2,3,4,5,6] should return [6,5,4,3,2,1]

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

  • 1. Write a C++ Program to display address of elements of an array using both array...

    1. Write a C++ Program to display address of elements of an array using both array and pointers 2. Write a C++ Program to insert and display data entered by using pointer notation. 3. Write a C++ Program to access Array Elements Using Pointer

  • Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

    Need help with my Java Hw: Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT