Thread: Arranging Random Numbers in Ascending Order

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Arranging Random Numbers in Ascending Order

    Hello, I'm working on a code to arrange 100 randomly generated integers on the interval [1,20] in ascending order. I've gotten to a point where I'm stuck on how to put them in ascending order. Along with putting them in ascending order, I have to ensure no repeating integers.

    Could use some help, stressing with midterms and coding, haha.

    Thank you!

    Here's what I got so far:
    Code:
    int main(void)
    {
    	
    srand(time(NULL)); // seed generator for random numbers.
    int myInts[100]; // array for 100 integers
    int i; // used in loop
    int size = sizeof(myInts) / sizeof(myInts[0]); // Getting length of array
    		
    		
    		// Will associate the array with random integers on interval [1,20]
    		for (i = 0; i < size; i++)
    		{
    			myInts[i] = rand()%20 + 1;
    		}
    		
    		for (i = 0; i < size; i++)
    		{
    			printf("Element #%d: %d\n", i, myInts[i]);
    		}
    		return 0;
    		
    		
    }

  2. #2
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Output: Arranging Random Numbers in Ascending Order-updated-hwk-jpg

    I'm able to display each of the elements, but I don't know how to display them in ascending order without repeating values.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    90
    Hey,

    Your output seems fine.

    How would you go about displaying them in ascending order?
    What would you try to order the array of numbers?

    Quote Originally Posted by csmith03 View Post
    but I don't know how to display them in ascending order without repeating values.
    This seem to imply that you know how to display them in order but you are missing the repeating value part, is this right?
    If it is, could you show us the result?

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post

    I'm able to display each of the elements, but I don't know how to display them in ascending order without repeating values.
    it is kind of hard not to repeat numbers in assenting or descending order if you have duplicates without deleting the duplicates.
    bubble sort comes to mind to order them though

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    I'm going to be honest, a majority of our CPRE hasn't a clue how to do these problems, the TA's have been getting emailed all week. Here's the instructions: Arranging Random Numbers in Ascending Order-hwk6problem1-png

    @Fiskker I'm not sure, haha, that's why I came here hoping to get some help. I only know how to display the random values. I don't know how to take away the duplicates or put them in ascending order. I'm not really good when it comes to arrays, we've hardly learned about them.

    @userxbw I'm unfamiliar with bubble sort.

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by csmith03 View Post
    @Fiskker I'm not sure, haha, that's why I came here hoping to get some help. I only know how to display the random values. I don't know how to take away the duplicates or put them in ascending order. I'm not really good when it comes to arrays, we've hardly learned about them.
    Which question are you not sure about?
    I think it is a good practice to think about the algorithm before doing any coding.
    How would you sort the numbers algorithmically?

    You can also look up the different sorting types in the web, this will give you some ideas.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Quote Originally Posted by Fiskker View Post
    Which question are you not sure about?
    I think it is a good practice to think about the algorithm before doing any coding.
    How would you sort the numbers algorithmically?

    You can also look up the different sorting types in the web, this will give you some ideas.

    Well, as of right now, as you said, displaying the elements is no problem. To answer your question, I'm at a dead end. I have no coding backround to take the code I have at this moment posted above and sort it in ascending order. I'm not sure if I'd need another for loop persay or how I would set that up. This is my first year programming and he kind of just sprung this on us.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    google bubble sort, and how to remove duplicate numbers in an array C programming

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int set_random_range(float min, float max);
    
    int main(void)
    {
    
    
    
    int i, temp,size = 100, j,k;
    int array[size];
    
    // fill array with random 1 .. 20
    
    for(i=0; i< size;i++)
    {
        array[i] = set_random_range(1.0,20.0);
    }
    
    
    
    for(i=0;i< size;i++)
      printf("i= %d : element = %d\n",  i, array[i]);
     
      printf("\n removing dups  : ");
     /* cycles through the array elements */
       for (i = 0; i <= size; i++) {
         /* cycles through array elements + 1 
          * kepping it one step ahead **/
          for (j = i + 1; j <= size;) {
        /* checks for dups */
             if (array[j] == array[i]) {
           /* picks up where it is at, then reassign value into the element */
                for (k = j; k <= size; k++) {
                   array[k] = array[k + 1];
                }
                // keeps track of total size of number set not element in array
                // that stays at 100, moves it back or deincurments it by one thus moving 
                // the array length back one on each match. even though the 
                // array itself stays at 100 lenght. 
                size--;  
             } else
           // if no match it moves to next element 
                j++;
          }
       }
     
       for (i = 0; i <= size; i++) {
          printf("%d \n", array[i]);
       }
      
         // Bubble sorting begins 
        
        for (i = 0; i <= size ; i++)
    
        {
    
            for (j = 0; j < (size - i - 1); j++)
    
            {
         
         
                if (array[j] > array[j + 1])
                {
    
                    temp = array[j];
    
                    array[j] = array[j + 1];
    
                    array[j + 1] = temp;
    
                }
                
    
            }
    
        }
     
        printf("Sorted array is...\n");
    
        for (i = 0; i < size; i++)
    
        {
    
            printf("%d\n", array[i]);
    
        }
     return 0;
    }
    
    int set_random_range(float min, float max)
    {
       return ((int)(min + max * rand() / RAND_MAX));
    }
    do not forget to seed your numbers (for whatever reason, if he is asking you to do that too in this one , maybe it is a windows thing, I run Linux and have no issues with rand() without seeding first, it generates a pool at boot up)
    Last edited by userxbw; 10-15-2017 at 07:30 PM.

  9. #9
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by csmith03 View Post
    Well, as of right now, as you said, displaying the elements is no problem. To answer your question, I'm at a dead end. I have no coding backround to take the code I have at this moment posted above and sort it in ascending order. I'm not sure if I'd need another for loop persay or how I would set that up. This is my first year programming and he kind of just sprung this on us.
    I understand it is your first year and it is something new, but I'm asking you to think about the problem as you would think about sorting a deck of cards yourself, that is thinking it step by step.
    By using logic you can reach the conclusion by yourself, you need not know programming in order to do it.

    Anyway, you can read a lot in the web and come back and ask for any doubts, once you are clear (or not) on how you would sort an array in your head, then we can get to the coding part.

  10. #10
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    What if you placed the random seed generator inside the for loop? That way the seed is different for each instance and be more random. Or at least I would think.

    After looking up time(), it looks like it's just UNIX epoch time in seconds. The current code would execute in less than a second but still interesting for something lasting more than a few seconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Program to print 3 numbers in ascending order
    By jadedreality in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 07:32 PM
  3. List of numbers in random order
    By KneeGrow in forum C++ Programming
    Replies: 11
    Last Post: 02-08-2005, 03:25 AM
  4. Arranging numbers in ascending order
    By Aero in forum C++ Programming
    Replies: 7
    Last Post: 12-22-2001, 07:52 AM
  5. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 07:14 PM

Tags for this Thread