Reverse Contents in Array

0 votes

I'm attempting to reverse an array of integers. 

I think my code's function is accurate, but I can't seem to achieve the right result.

The result is written as 10 9 8 7 6. 

Why am I unable to obtain the second half of the numbers? 

The output is as follows when I delete the "/2" from count: 10 9 8 7 6 6 7 8 9 10

void reverse(int [], int);

int main ()
{
   const int SIZE = 10;
   int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   reverse(arr, SIZE);
   return 0;
}
void reverse(int arr[], int count)
{
   int temp;
   for (int i = 0; i < count/2; ++i)
   {
      arr[i] = temp;
      temp = arr[count-i-1];
      arr[count-i-1] = arr[i];
      arr[i] = temp;

      cout << temp << " ";
   }
}
Aug 3, 2022 in C++ by Nicholas
• 7,760 points
627 views

1 answer to this question.

0 votes

My strategy would be as follows:

#include <algorithm>
#include <iterator>

int main()
{
  const int SIZE = 10;
  int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  std::reverse(std::begin(arr), std::end(arr));
  ...
}
answered Aug 5, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Reverse a number without converting it to string in C++

I'm attempting to write a programme in C++ to reverse a number. It works well with numbers like 1234, but when I try to enter a number like 5430, it displays 345, and the same with numbers that begin with zero, such as 0234, which displays 432. Could someone please explain how to deal with zeros at the beginning and end of a sentence? I simply need to save the number without converting it to a string. #include<iostream> using namespace std; int main(){ ...READ MORE

Jun 30, 2022 in C++ by Nicholas
• 7,760 points
601 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
489 views
0 votes
0 answers

How to declare an array of strings in C++?

I am trying to iterate over all ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
543 views
0 votes
1 answer

Create a reverse LinkedList in C++ from a given LinkedList

A simpler solution is to just let the current node point to the previous node while going through your linked list, saving the previous and next nodes as you go. void LinkedList::reversedLinkedList() { if(head == ...READ MORE

answered Aug 5, 2022 in C++ by Damon
• 4,960 points
768 views
0 votes
0 answers

How do I determine the size of my array in C?

How do I determine the size of ...READ MORE

May 1, 2022 in Others by Kichu
• 19,040 points
457 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
539 views
0 votes
0 answers

How to reverse an array in php WITHOUT using the array reverse method

I have an array called reverse array ...READ MORE

Jun 12, 2022 in PHP by narikkadan
• 63,600 points
2,209 views
0 votes
0 answers

Php function returning associate array

How can I make a function return ...READ MORE

Jun 17, 2022 in PHP by narikkadan
• 63,600 points
308 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,137 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
5,603 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP