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


Groups > comp.lang.c > #111988

Arrays sorting!!

From Christopher <info@invalid.invalid>
Newsgroups comp.lang.c
Subject Arrays sorting!!
Date 2017-06-12 04:55 +0100
Organization Aioe.org NNTP Server
Message-ID <ohl2ii$b85$2@gioia.aioe.org> (permalink)

Show all headers | View raw


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 — Next 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