Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
2.20/5 (7 votes)
See more:
How to write a program to take 100 random variables as an input in an integer array and then sort this array in ascending order?

Please tell me. I need to know.
Posted
Updated 25-Dec-10 5:36am
v2
Comments
Sandeep Mewara 25-Dec-10 11:36am    
Any effort made?
Dr.Walt Fair, PE 25-Dec-10 13:35pm    
Try to do a Google for "sort algorithms" and that should provide everything you need.
fjdiewornncalwe 25-Dec-10 19:09pm    
You are doing yourself a great disservice if you do your homework this way. You may get decent marks when people give you the answers on a platter, but you won't survive an interview or a real job situation in the development world if you don't learn how to do these things now. Cheers and happy holidays.

Because this sounds seriously like homework, it is not likely that you will just get the answer, ready to cut and paste - we don't do that. Homework is given for a reason: to make you think about what you have been taught, and to get you actually using it for yourself.

Instead, here are the processes to go through. Since you are just starting out, you can do them separately if you want - i.e. finish one process and be sure it works before moving to the next.

1) Fill an array with 100 random integers.
1a) Declare an array of 100 integers so you have somewhere to put them.
1b) Set up a loop to go through each integer in the array in turn
1c) Put a random integer in each element of the array.
To do these, you will need the words int, for and rand - use Google.

2) Write code to prove you have 100 random integers in the array.
2a) Set up a loop to go through each integer in the array in turn
2b) Print each element of the array to the console.
To do these, you will need for, <<


3) Sort the array.
3a) Decide how you will sort it. There are loads of ways, form the trivial (use the sort function) to the complex (implement quick sort yourself). Only you can decide how this fits into your homework question!

Good luck - it's not difficult, all you have to do is try it yourself!
 
Share this answer
 
Comments
pasztorpisti 25-Dec-10 12:16pm    
Programming is not for anyone, but anyone can get an IT degree. Its sad but true. The good point of it is that you have less competition on the job market. But your solution is also a 5, depends on the intention of the OP.
fjdiewornncalwe 25-Dec-10 19:06pm    
A great answer to a homework based question. You've given me new hope and a new way to approach these types of questions. Cheers.
If it's that small of an array you can #include <algorithm> and use std::sort or use C's qsort. Note that std::sort is neither very algorithmically nor memory efficient and should not be utilized when you need the highest performance.
 
Share this answer
 
XML
#include <iostream.h>
#include <stdlib.h>
int sort(my_array, my_array+ARRAY_SIZE)
int main()
{
     const int NUMBER_MAX_PLUS1 = 100;
     const int ARRAY_SIZE = 100;
     int my_array[ARRAY_SIZE];
     for (int i=0; i<ARRAY_SIZE; ++i)
         {
            { 
                my_array[i] = rand() % NUMBER_MAX_PLUS1;
                int sort(my_array, my_array+ARRAY_SIZE);
            }
            for (int i=0; i<ARRAY_SIZE; ++i)
             {
                    cout<< i<<               my_array[i]<<"\n";
             }
         }
     return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900