Problem

Hey guys ;) Ive written a programm but it doesn't work... What's wrong? it was supposed to sort numebers.

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
  #include<iostream>
#include<fstream>
#include"conio.h"
using namespace std;

double sort (double T[], int n)
   {   
      int pom;
      for (int j=n-1; j>0; j--)
        for (int i=0; i<j; i++)
          if (T[i]>T[i+1])
          {
            pom=T[i];
            T[i]=T[i+1];
            T[i+1]=pom;
          }
   return T[n];
}
int main(){
   int n, i;
   n=10;
   double T[n];
   for ( i=0; i<n; i++) {
      cout<<endl<<"Give number "<<i+1<<": ";
      cin>>T[i];
      }
     for (i=0; i<n; i++) cout<<"   "<<T[i];
     cout <<endl;
     cout << sort(T,n);
     { 
  
 }
	 getch();
   return 0;
}
> What's wrong?
It doesn't compile.

¡Next!
Alright, let's see exactly why this doesn't work (compared to the un-helpful post above)...

Well, for one, you're displaying the array as-is in the for loop, then sorting it... then you display what sort() churns out, which in this case is just the value of T at n, which is outside of its bounds (unallocated memory. This is why at the end of running, it dumps some non-existent number at the end). Really, the issue here is what your sort() function returns. Fix that, and you'll be golden.
Thank you Ispil! I'll try ;)
Topic archived. No new replies allowed.