Question:
Sorting random numbers with arrays c++?
Gx
2014-04-23 21:53:56 UTC
Have to print out 10 random numbers from 10-100. Can't use #include

Have it printing out random numbers but cant figure out how to sort them.

#include
#include
#include

using namespace std;

int main()
{
int TenRand[10], i, TenInOrder[10], j, a;
srand(time(0));

for (i=0; i<10; i++)
TenRand[i]=rand()%100+1;

for (i=0; i<10; i++)
{
for (j=0; j<100; j++)
{
if (TenRand[i]==j)
TenInOrder[i]=TenRand[i];
}
}
for (i=0; i<10; i++)
{
cout << TenInOrder[i] << endl;

}
return 0;
}


Any ideas?
Four answers:
cja
2014-04-24 07:31:02 UTC
If you haven't learned bubble sort yet, then I assume you haven't learned any sorting algorithms. It seems unlikely that your instructor is expecting you to invent a sorting algorithm. There are sort functions in the C and C++ library, but I doubt you're intended to use one of those. So, you aren't left with many choices. One option is to add the random numbers to an array so that after each new number is added, the array is always sorted. In my example below I'm using a vector so I don't have to worry about moving array elements around. You can use the same logic with an array, you'd just have to write your own 'insert' function, and move array elements yourself.



#include

#include

#include

#include

#include

#include



using namespace std;



const size_t N = 10;

const int minVal = 10;

const int maxVal = 99;

unsigned random(double rangeMin, double rangeMax);



int main(int argc, char *argv[]) {

  time_t t;

  vector intVec;

  vector::iterator i;

  int n;



  srand((unsigned)time(&t));



  //

  // Fill vector of random numbers, inserting to maintain ascending order.

  //

  while (intVec.size() < N) {

      n = random(minVal, maxVal);

      i = intVec.begin();

      while ((i != intVec.end()) && (n > *i)) i++;

      intVec.insert(i, n);

  }

  copy(intVec.begin(), intVec.end(), ostream_iterator(cout, " "));

  cout << endl;

  return 0;

}



unsigned random(double rangeMin, double rangeMax) {

    assert(rangeMin <= rangeMax);

    double maxN = rangeMax - rangeMin;

    return static_cast(((rand() / (double)RAND_MAX) * maxN) + rangeMin);

}



#if 0



Sample output:



16 21 23 47 51 69 80 82 96 97



#endif
?
2014-04-24 02:25:48 UTC
Here is an article about C++ sorting.

http://www.cplusplus.com/reference/algorithm/sort/



This would be your code, in case you implemented it:

You have to additionally include

#include

#include

#include

#include

using namespace std;



int main()

{

int TenRand[10], i, TenInOrder[10], j, a;

srand(time(0));



for (i=0; i<10; i++)

TenRand[i]=rand()%100+1;

copy(TenRand, TenRand + 10, TenInOrder);

sort(TenInOrder, TenInOrder + 10);



/* This here looks like you wanted to bubble sort your array as Rahul and EddieJ suggested */

/* I commented this code out */

/*

for (i=0; i<10; i++)

{

for (j=0; j<100; j++)

{

if (TenRand[i]==j)

TenInOrder[i]=TenRand[i];

}

}

*/

for (i=0; i<10; i++)

{

cout << TenInOrder[i] << endl;



}

return 0;

}
tumbleweed_biff
2014-04-23 23:29:33 UTC
Assign the first number to a variable. test against each element of the array, keeping the lower of the two as the test variable. Assign the result to the first element of the 2nd array. Repeat as necessary until done.
anonymous
2014-04-24 00:26:31 UTC
i used bubble sort as EddieJ mentioned



here is the code

#include

#include

#include



using namespace std;



int main()

{

int TenRand[10], i, TenInOrder[10], j, a,f=1,temp;

srand(time(0));



for (i=0; i<10; i++)

TenRand[i]=rand()%100+1;



for (i=0; i<10; i++)

{

for (j=0; j<100; j++)

{

if (TenRand[i]==j)

TenInOrder[i]=TenRand[i];

}

}



for(i=0;i<10;i++){

cout << TenInOrder[i] << endl;

}



j=10;

while(f==1){

f=0;

for (i=0; i
{

if(TenInOrder[i]>TenInOrder[i+1])

{

temp=TenInOrder[i];

TenInOrder[i]=TenInOrder[i+1] ;

TenInOrder[i+1] =temp;

f=1;

}



}

}



cout<<"sorted"<
for(i=0;i<10;i++){

cout << TenInOrder[i] << endl;

}





return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...