This problem is supposed to reverse an array without changing main. I've gotten to here, but for some reason it only reverses the first three elements of the array. Could someone help me figure out why that is happening?
#include <iostream>
usingnamespace std;
void backwards(int [], int);
int main()
{
int arr[] = {2,8,17,3,5,16}; int i;
backwards(arr,6);
for (i = 0; i< 6; i++)
cout<<arr[i]<<endl;
//system("pause");
return 0;
}
void backwards(int array[], int number)
{
/*Pre: array[] - array of integers
number - number of elements in the array that have values
Post: Nothing
Purpose: Reverse the order of the elements of an array */
int i; int temp;int j;
for(i = 0; i < number;i++)
{
temp = array[number - 1 - i];
array [i] = array [number - 1 - i];
array [i] = temp;
}
return;
}
void backwards(int array [], int number)
{
int first = 0;
int last = number - 1; // last legal index
while (first < last)
{
int temp = array[first];
array[first] = array[last];
array[last] = temp;
++first;
--last;
}
}
#include <iostream>
#include <algorithm> // not changing main() ;)
usingnamespace std;//leaving this in so that main() is untouched but ...
//http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practicevoid backwards(int [], int);
int main()
{
int arr[] = {2,8,17,3,5,16}; int i;
backwards(arr,6);
for (i = 0; i< 6; i++)
cout<<arr[i]<<endl;
//system("pause");
return 0;
}
void backwards(int array[], int number)
{
std::reverse(array, array + number);
}
how this helps the OP understand how an array can be reversed.
to be fair, that is exactly how an array is reversed. It probably doesn't help the OP with their homework, but it helps someone stumbling on this question in a web search a year later.
to be fair, that is exactly how an array is reversed. It probably doesn't help the OP with their homework, but it helps someone stumbling on this question in a web search a year later.
Not at all. Anyone googling how to reverse an array is likely looking for the actual code that does the work, not the interface.
Replying to an obvious beginner with a built-in function does no help to anyone, not even a person stumbling across the question a year later.
Arslan - explain to me how difficult it is to google std::reverse()
Explain to me how difficult it is to actually answer the OP's question? You clicked on this thread and wasted your own time and energy to give an unhelpful reply, when you could have used that same time and energy to actually answer the question.
If you wanted him to google it, you would've been better off saying "google it" instead of giving that ridiculous reply.
Next time someone asks you how quicksort works, I suppose you can simply answer with std::sort and leave it at that.
@OP Here's another way as a result of slight embellishment. Your difficulty was the halfway mark not being the loop termination.
I decided to generalize the size of the array as part of the debugging and thought I may as well leave it in, subject as always to approval from the C++ police.
#include <iostream>
usingnamespace std;
void backwards(int [], int);
int main()
{
int arr[] = {2,8,17,3,5,16};
int size = sizeof(arr)/sizeof(int);
backwards(arr, size);
for (int i = 0; i< size; i++)
cout<<arr[i]<<endl;
//system("pause");
return 0;
}
void backwards(int array[], int number)
{
/*Pre: array[] - array of integers
number - number of elements in the array that have values
Post: Nothing
Purpose: Reverse the order of the elements of an array */
int temp = 0;
for(int i = 0; i < number/2; i++)
{
temp = array[i];
array[i] = array[number - 1 - i];
array[number - 1 - i] = temp;
}
return;
}
int arr[] = {2,8,17,3,5,16};
16
5
3
17
8
2
Program ended with exit code: 0
int arr[] = {2,8,17,99,3,5,16};
16
5
3
99
17
8
2
Program ended with exit code: 0