Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112085

Re: Arrays sorting!!

From Real Troll <Real.Troll@Trolls.com>
Newsgroups comp.lang.c
Subject Re: Arrays sorting!!
Date 2017-06-12 16:55 -0400
Organization Aioe.org NNTP Server
Message-ID <ohmv15$1jm5$1@gioia.aioe.org> (permalink)
References <ohl2ii$b85$2@gioia.aioe.org>

Show all headers | View raw


I've changed a bit in your code and this works on my machine:
(I can't comment on the quality of your sort code because it works so 
keep it as it is!!).

I inserted some printf after each for loop to understand what exactly is 
going on.  It looks like you are trying to get unique numbers by 
populating the array and then generating the random indexes to pick 6 
numbers from 60.

I have also sequenced the array so that it starts from 1 (not zero) as I 
am used to Delphi and so I decided to keep it as in delphi.  I like 
counting from 1 !!!!!

/* Program begins */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void sort (int a[], int n);

int main (void)
{
     int i, j, k, Nums[ 61 ];

     srand( ( int ) time( NULL ) ) ;

     for ( i = 1; i < 61; i++ )
         Nums[ i ] = i;

     for ( i = 1 ; i < 61 ; i++ )
     {
         j = ( rand() % 60 ) + 1 ;
         k = Nums[ i ] ;
         Nums[ i ] = Nums[ j ] ;
         Nums[ j ] = k ;
     }

     printf( "Six random numbers are: \n") ;
     for ( i = 1 ; i < 7 ; i++ )
         printf("Nums[%i] = %i\n", i, Nums[i]);

     sort (Nums, 7);
     printf("\n");

     for ( i = 1 ; i < 7 ; i++ )
         printf("Nums[%i] = %i\n", i, Nums[i]);

     return 0;
}

void sort (int a[], int n)
{
     int i, j, temp;
     for ( i = 0; i < n - 1; ++i )
         for ( j = i + 1; j < n; ++j )
             if ( a[i] > a[j] ) {
                 temp = a[i];
                 a[i] = a[j];
                 a[j] = temp;
             }
}

/* Program ends */



On 12/06/2017 04:55, Christopher wrote:
>
> Can anybody tell me why is this array giving me corruption?
>
> /* Program begins */
> #include <stdio.h>
> #include <time.h>
> #include <stdlib.h>
>
> void sort (int a[], int n);
>
> int main (void)
> {
>
>     int i, j, k, nums[ 60 ];
>
>     srand( ( int ) time( NULL ) ) ;
>
>     for ( i = 0; i < 60; i++ )
>         nums[ i ] = i;
>
>     for ( i = 0 ; i < 60 ; i++ )
>     {
>         j = ( rand() % 60 ) + 1 ;
>         k = nums[ i ] ;
>         nums[ i ] = nums[ j ] ;
>         nums[ j ] = k ;
>     }
>
>
>
>     printf( "Six random numbers are: \n") ;
>     for ( i = 0 ; i < 6 ; i++ )
>         printf("nums[%i] = %i\n", i, nums[i]);
>
>     sort (nums, 6);
>     printf("\n");
>
>     for ( i = 0 ; i < 6 ; i++ )
>         printf("nums[%i] = %i\n", i, nums[i]);
>
>     return 0;
> }
>
> void sort (int a[], int n)
> {
>     int i, j, temp;
>     for ( i = 0; i < n - 1; ++i )
>         for ( j = i + 1; j < n; ++j )
>             if ( a[i] > a[j] ) {
>                 temp = a[i];
>                 a[i] = a[j];
>                 a[j] = temp;
>             }
> }
>
> /* Program ends */
>
> There are just 60 random numbers (1 to 60) out of which I am selecting 
> 6 and sorting them but the corruption occurs in VS2010.
>
> I tried using 61 instead of 60 but the problem doesn't go away!!
>
> I am accessing the numbers using the index numbered 0 to 5 (6 numbers 
> here).
>

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Arrays sorting!! Christopher <info@invalid.invalid> - 2017-06-12 04:55 +0100
  Re: Arrays sorting!! luser droog <luser.droog@gmail.com> - 2017-06-11 21:30 -0700
    Re: Arrays sorting!! bartc <bc@freeuk.com> - 2017-06-12 13:32 +0100
      Re: Arrays sorting!! bartc <bc@freeuk.com> - 2017-06-12 13:48 +0100
        Re: Arrays sorting!! supercat@casperkitty.com - 2017-06-12 07:36 -0700
          Re: Arrays sorting!! David Brown <david.brown@hesbynett.no> - 2017-06-12 21:22 +0200
            Re: Arrays sorting!! supercat@casperkitty.com - 2017-06-12 14:00 -0700
              Re: Arrays sorting!! David Brown <david.brown@hesbynett.no> - 2017-06-12 23:39 +0200
                Re: Arrays sorting!! "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-12 18:47 -0400
  Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 12:03 +0100
    Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 20:23 +0800
      Re: Arrays sorting!! mark.bluemel@gmail.com - 2017-06-12 07:32 -0700
        Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 23:06 +0800
      Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 16:08 +0100
        Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 23:11 +0800
          Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 16:46 +0100
            Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-13 17:49 +0800
    Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 15:57 +0100
  Re: Arrays sorting!! Real Troll <Real.Troll@Trolls.com> - 2017-06-12 16:55 -0400

csiph-web