Hello, dear all, here is my algorithm. i'm trying to reverse the array but its not function and not loop.

#include <iostream>
using namespace std;


void initial(int *v, int N)
{   int i;
	for(i=1; i<=N; i++)
		v[i] = i;
}
void write (int *v, int N)
{
	for (int i = 1 ; i <= N; i++)
		cout << v[i] << "";
	cout <<"\n";
}
void reverse (int *v, int N)
{
	int tmp = v[1];
     for (int i = 1; i<N-2; ++i)
	 {
     v[i] = v[N-i-1];
     v[N-i-1] = tmp;
	 }
}


int main(int *v)
{    int  N;
	cout << "enter size of element:";
	cin >> N;
if (N >0 && N<=100){
int *v = new int [N];
	initial (v,N);
	write (v,N);
	reverse (v,N);
	delete v;
}

Your reverse function looks suspicious.

You are overwriting the initial element before you store it to replace the last element, so the data is unrecoverable and you will end up with a mirrored half-way reversed array instead of the entire original array reversed.

The tmp implementation isn't helping because its only done once for only one location (which is the 2nd indice) of the array.

You probably meant to do something like this--

for (int i = 1; i<N-2; ++i)
{
	int tmp = v[i];
	v[i] = v[N-i-1];
	v[N-i-1] = tmp;
}

--also if efficiency is an issue, keep in mind that you're reversing the order of the elements, so your array has the same elements, just mirrored across a pivot-point, or center element.

This means that you only need to do int-division based on the size of the array, and reverse values across that pivot point.

1) on line 18 you are setting tmp = second element of the array. The value of tmp is not changed inside the loop, so change it to temp = v[i]; and move it inside the loop.

2) In the loop, line 19, you are running through all elements and swapping nearly all of them twice - which will revert them to the unswapped order. Obviously this needs fixing.

As an aside, you can swap 2 variables using swap(v1, v2) which is neater (and less error prone as in the above case), instead of using a temporary variable.

after i fixed it

void recycle (int *v, int N)
{
	int tmp ;
     for (int i = 1; i<N-2; ++i)
	 {
     tmp = v[i];
     v[i] = v[N-i-1];
     v[N-i-1] = tmp;
	 }
}

it still doesn't work

I made a few fixes--

// reverse_stuff.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void initial(int *v, size_t N)
{   
	size_t i;
	for(i=1; i < (N + 1); i++)
		v[i-1] = i;
}
void write (int *v, size_t N)
{
	for (size_t i = 1 ; i < (N + 1); i++)
		cout << v[i-1];
	cout << "\n";
}

void reverse (int *v, size_t N)
{
	for (size_t i = 1; i < static_cast<size_t>(N/2) + (size_t)1; i++)
	{
		int tmp = v[i-1];
		v[i-1] = v[N-i];
		v[N-i] = tmp;
	}
}


int main()
{   
	size_t  N;
	cout << "enter size of element:";
	cin >> N;
	if (N<=100){
		int *v = new int [N];
		initial (v,N);
		write (v,N);
		reverse (v,N);
		write(v, N);
		delete v;
	}
	cin.ignore();
	cin.get();
	return 0;
}

--I don't like providing solutions to non-existent problems, but I believe this is correct unless otherwise noted.

#include "stdafx.h"

there is error as follows:
c:\documents and settings\user\my documents\visual studio 2008\projects\exercises harian\exercises harian\exercises harian.cpp(7) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory.

Just remove the line. Alex uses pre-compiled headers in Visual studio, this requires for stdafx.h to be included. Removing the line should solve your prob :)

Comments
Totally missed that XD. Thank you =)
This article has been dead for over six months. Start a new discussion instead.