#include<iostream.h>
void ascending(int,int);
int main()
{
	int array_size=10;
	int numbers[]={3,54,23,0,677,45,77,342,4,10};
	cout<<"The numbers are: "<<endl;
	for(int i=0;i<10;i++)
	{
		cout<<numbers[i];
	}
	cout<<"the ascending form is: "<<endl;
	ascending( numbers[], array_size);
	return 0;
}



void ascending(int numbers[], int array_size)
{
  int i, j, index;

  for (i=1; i < array_size; i++)
  {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index))
    {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}

Recommended Answers

All 4 Replies

What exactly is the problem with it. Do you also take your car to the garage and tell them "my car is broke -- please fix it". Of course you don't -- you have to tell them what you think is the problem or the symptoms.

i am intented to make an array of size n arrange in ascending order. i am using insertion sort for this purpose. The problem with my code is, its giving me syntax error. where as i cannot find any problem in my syntax. Also, the logic is correct according to me..

>>its giving me syntax error.
Where?? What line number


>>Also, the logic is correct according to me..
That's the root of all bugs :)

As for syntax problems, lets start from the top. #include<iostream.h> You shouldn't use the .h version anymore, as it is no longer in the standard. There are compilers that don't even have it anymore (VS 2008, for example).

because of that, you then need to add using namespace std; , or put std:: in front of all of your cout's and endl's.

Your function prototype is incorrect for what you want, it should be void ascending(int[],int); Later, when you call the function ascending( numbers[], array_size); :
when passing an array to a method, you only need the variable name, not the []s afterwords.

so, your code should now look like this

#include <iostream>
using namespace std;
void ascending(int[], int);
int main()
{
	int array_size=10;
	int numbers[]={3,54,23,0,677,45,77,342,4,10};
	cout<<"The numbers are: "<<endl;
	for(int i=0;i<10;i++)
	{
		cout<<numbers[i];
	}
	cout<<"the ascending form is: "<<endl;
	ascending(numbers, array_size);
	return 0;
}



void ascending(int numbers[], int array_size)
{
  int i, j, index;

  for (i=1; i < array_size; i++)
  {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index))
    {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}

However, your sorting logic is incorrect. I'll give you a chance to look at that now that your syntax errors have been cleared up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.