Reversing An Integer Array

Posts 1–12 of 12 · Page 1 of 1
Reversing An Integer Array
So I'm I need help on arrays. My code is supposed to display nums array backwards. Here's my code:
#include<iostream>

using namespace std;

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

int copyReverse[10];


for(int i=9;i<0;i--)
{
nums[9];
cout<<nums<<endl;

}

for(int c=0;c>0;c+=)
{
copyReverse[0];
cout<<copyReverse<<endl;

}

system ("pause");

return 0;
}
Code:
#include <iostream>
using namespace std;
int* Reverse(int a[],int Len)
{
	int* temp= new int[Len];
	for(int i=Len-1;i>=0;i--)
	{
		temp[Len-i-1]=a[i];
	}
	return temp;
}

int main()
	{

int nums[]={1,2,3,4,5,6,7,8,9,10};
int *z = Reverse(nums,10);
for(int i=0;i<10;i++)
cout << z[i] << ",";
system("pause");
return 0;
	}
Done. Use the reverse function defined on top. To call it, just pass the array to be reversed and the length of the array.
Using Modern C++:
Code:
// C++11 Initializer list
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> reverse_numbers;

std::reverse_copy(numbers.begin(), numbers.end(), reverse_numbers.begin());

// Anonymous functions
std::for_each(numbers.begin(), numbers.end(), [](int x) { std::cout << x << std::endl; });
std::for_each(reverse_numbers.begin(), reverse_numbers.end(), [](int x) { std::cout << x << std::endl; });
Quote Originally Posted by Fovea View Post
Using Modern C++:
Code:
// C++11 Initializer list
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> reverse_numbers;

std::reverse_copy(numbers.begin(), numbers.end(), reverse_numbers.begin());

// Anonymous functions
std::for_each(numbers.begin(), numbers.end(), [](int x) { std::cout << x << std::endl; });
std::for_each(reverse_numbers.begin(), reverse_numbers.end(), [](int x) { std::cout << x << std::endl; });
Sheesh. I would hate to make a full on win32 gui app with that style of writing c++ code.
Quote Originally Posted by 258456 View Post
Sheesh. I would hate to make a full on win32 gui app with that style of writing c++ code.
You can use it in console applications as well ! It's actually nice. And I don't see why you'd hate to do it in a GUI App !!
Quote Originally Posted by Hassan View Post


You can use it in console applications as well ! It's actually nice. And I don't see why you'd hate to do it in a GUI App !!
Lol, it hurts my eyes. That's why. If i would write a simple 2,000 line app with that style, i would go crazy and there's no way in hell i would be able to manage it neatly and update.
This is actually standard. It becomes readable if you just namespace std; it.
And when using it for a while, it will be just like anything else in c++, since it will be familiar for you.

Things like for_each should be already familiar from Java.

Big advantage is, it's 100% pure OP, causing it to be fast as hell!
Meh, using a generic function:
Code:
template<class T>
T* reverse(T *arr, int n)
{
	for(int i = 0; i < n / 2; i++)
	{
		T temp = arr[i];
		arr[i] = arr[n - i - 1];
		arr[n - i - 1] = temp;
	}
	return arr;
}
Think this has been comprehensively solved though.
std algorithms are already generic. Also, many would argue it is much more easier to read. See Herb Sutter's video on modern C++.
Quote Originally Posted by Fovea View Post
std algorithms are already generic. Also, many would argue it is much more easier to read. See Herb Sutter's video on modern C++.
I'm aware of that, but as I've said in quite a few posts, "Yes, this is remnants of C, get off me.". Simple fact of the matter is that I'm currently learning C, not C++ but if I see a question where I can provide a solution that works, I will.
There is no such thing as templates in C.
Quote Originally Posted by Fovea View Post
There is no such thing as templates in C.
I never said there was, I knew about templates from my dabblings in C++, but as you were complaining about my lack of "modernized C++" that was my disclaimer on the code I posted.
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us