Random number array sorting

Jul 25, 2013 at 4:45am
I am having trouble getting these values to return correctly, most times everything but descending order works but if i input a 3 everything messes up, any help would be greatly appreciated.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

void myOrder(int numb, int random);
void myOrder(int numb, int random)
{
     int asc, des;
     int list[numb];
     
     qsort(list, numb, sizeof(int), compare);
     cout<<"\n\n\nThe "<<numb<<" numbers in Ascending Order ";
     for(int i = 0; i < numb; i++)
     {
             cout<<list[i]<<" ";
     }
     
     cout<<"\nThe "<<numb<<" numbers in Descending Order "; 
     for(int i = numb; i >= 0; i--)
     {
             cout<<list[i-1]<<" ";
     }
         
}

void mySort(int numb, int random);
void mySort(int numb, int random)
{
    int list[numb];
        
    qsort(list, numb, sizeof(int), compare);
    
    cout<<"\n\n\nThe highest number is "<<list[(numb-1)];
    cout<<"\nThe lowest number is "<<list[0];
  
}

int getNumb();
int getNumb()
{
    bool restart = false;
    int numb;
    do
    {
    cout<<"How many numbers would you like to be generated(2-20)? ";
    cin>>numb;
    if ((numb >= 2 )&&(numb <= 20)) 
       {
    cout<<"\n\nThe "<<numb <<" numbers generated are:\n";
    return numb;   
       }
    else 
         {
         cout<<"Sorry, that is invalid try again.\n"; 
         restart = true;
         }
    }
    while (restart == true);
    
}

int myRandom(int numb);
int myRandom(int numb)
{
         int random[numb];
         for (int i = 0; i < numb ; i++)
         {
             random[i] = rand() % 20 + 2;
             cout<<random[i]<<" ";
         }
         return random[0];
}

int main()
{
    int numb, random;
    srand (time(NULL));
    cout<<"Data Searching and Sorting!!\n\n";
    
    numb = getNumb();
    random = myRandom(numb);
    mySort(numb, random);
    myOrder(numb, random);
    
    
cin.get();
cin.get();
return 0;   
}
Jul 25, 2013 at 5:08am
In myRandom, random is a local array that ceases existing when the function ends, so all of the assignments you've made to individual elements are lost. (Besides that, C++ requires array sizes to be compile time constants so line 71 is illegal.)

In mySort, list is a local array with uninitialized elements that could be any value at all. (Besides that, C++ requires array sizes to be compile time constants so line 35 is illegal.)

In myOrder, list is a local array with uninitialized elements that could be any value at all. (Besides that, C++ requires array sizes to be compile time constants so line 15 is illegal.)

You may want to disable the extension that allows those illegal lines.
Jul 25, 2013 at 5:27am
Thanks for the advice, I hate this bloodshed compiler. Any chance you know where the option would be to block those lines?
Jul 25, 2013 at 5:36am
The bloodshed IDE is no longer developed (and if you downloaded the package from their website it comes with an outdated version of the GCC compiler.) I would suggest getting rid of it at the earliest opportunity. If you want to stick with a similar IDE, the Orwell branch is actively developed. Code::Blocks is another popular choice to use with GCC.

http://sourceforge.net/projects/orwelldevcpp/
http://www.codeblocks.org/

I believe the option you need to disable extensions with GCC is -pedantic.
Topic archived. No new replies allowed.