what is the problem in this code? solve!

#include<iostream.h>
void sort(int A[20],int N)
{
int i,j, temp;
for (i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(A[i]>A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
cout<<A[i-1];
}
}
void main()
int A[20], i, j, N;
cout<<"enter no. of elements";
cin>>N;
cout<<"enter array";
for(i=0;i<N;i++)
{
cin>>A[i];
sort(A[20], N);
}
Last edited on
Well, I don't think anyone would want to rewrite the code for you...

But here's a couple of pointers:

1-It isn't standard anymore for main to return void. Return an int instead.

1
2
3
4
int main()
{
//code goes here
}


2-iostream.h is a deprecated header. use <iostream> instead (you should probably change your IDE)

3-Your code is all wrong. Right after your void main() (remember to use int main) there should a { immediately following it.

4-Use the code tags here in the forum to post code, just like so:

1
2
3
4
int main()
{
//code goes here
}
It would also be helpful if you posted compiler errors.
Maybe it is because of my incompetence but I have never seen a function that has an array as a parameter be declared the way you did. I mostly see something like this
 
void sort(int Array[], int N)

And when call the function only the name of the array is pass in
 
sort(A, N)
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

main()
{
	int A[20];
	int i, j, N;
	std::cout<<"enter no. of elements";
	std::cin>>N;
	std::cout<<"enter array";
	for(i=0;i<N;i++)
	{
		std::cin>>A[i];
		
		int i,j, temp;
		for (i=0;i<N;i++)
		{
			for(j=i+1;j<N;j++)
			{
				if(A[i]>A[j])
				{
					temp=A[i];
					A[i]=A[j];
					A[j]=temp;
				}
			std::cout<<A[i-1];
			}
		}
	}
}



Okay, I made your code compilable, but that doesn't mean it works as it should.

1. Line 5: You call an array with 20 values in it, but then you ask for the N value. If I put in any value above 20, your program will throw away the last N-20 values. I suggest you wait until the user enters a N value, then call an array[N];.

2. Line 25: You are showing users the values already, so the output gets all messed up.

3. Everywhere: Your program is not very user friendly. Format your questions and comments so your program looks elegant. Example: instead of std::cout<<"enter array";, you should put std::cout<<"Enter array item "<<i<<": ";. In this case, the user knows how many items he/she has entered and it looks clean and correct.

Now, take a look at my code, which I made just a few days ago:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * File: bubblesort.c
 * ------------------------
 * Bubble sorts an integer array.
 */

#include <iostream>

main()
{
      int n,tmp,i,j,k,reckon;
      reckon=0;
      
      std::cout<<"This program uses the bubble sort method to sort an integer array.\n";
      std::cout<<"How long is your list?  ";
      std::cin>>k;
      std::cin.ignore();
      
      int array[k];
      
      std::cout<<"\n\n";
      
      for(i=0;i<k;i++)
      {
            std::cout<<"Item number "<<i+1<<":  ";
            std::cin>>array[i];
            std::cin.ignore();
  	  }
      
      while(reckon<=k)
      {
            reckon++;
            for(n=1;n<k;n++)
            {
                  if(array[n]>array[n-1])
                  {
					    ;
				  }
                  else
                  {
                        tmp=array[n];
                        array[n]=array[n-1];
                        array[n-1]=tmp;
                  }
            }
      }
      
      std::cout<<"\n\n";
      
      for(j=1;j<=k;j++)
      {
            std::cout<<"Sorted item number "<<j<<" = "<<array[j-1]<<"\n";
      }
}


Use this only as an example (1. It is extensive. 2. It uses up quite some memory). Although my code is longer, a sample run shows its user-friendliness:

This program uses the bubble sort method to sort an integer array.
How long is your list?  5

Item number 1:  13
Item number 2:  12
Item number 3:  -5
Item number 4:  17
Item number 5:  12

Sorted Item number 1 = -5
Sorted Item number 2 = 12
Sorted Item number 3 = 12
Sorted Item number 4 = 13
Sorted Item number 5 = 17


It is good to tell your user what the program does. Then, prompt the user in a friendly way. Give them information so they can understand it.

Also, the other are right about forum posting: include code tags and other formatting options to make your threads more readable and understandable. The more information you give us, the more we are equipped to help you.

Well, that's all!
Good luck and happy coding!
~Usandfriends
Last edited on
Topic archived. No new replies allowed.