beginner help with selection sort

This is a discussion on beginner help with selection sort within the C Programming forums, part of the General Programming Boards category; hi all I am trying to use a selection sort on an array but I keep getting a debug error ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    beginner help with selection sort

    hi all I am trying to use a selection sort on an array but I keep getting a debug error I've never seen before:
    "Run Time Check Failure #2- Stack around the variable 'avg' was corrupted." any help is appreciated thanks
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    
    void averages( int oneTest[ ], int twoTest[ ], int threeTest[ ], int* average1, int* average2, int* average3 );
    void selectionSort ( int list[ ], int last );
    int main( void )
    {
    	int avg1, avg2, avg3;
    
    	/*standardDeviation = ( ( count * square ) - ( sum * sum ) ) / ( count * ( count - 1 ) );*/
    
    	int idNum[10] = {0};
    	int test1[10] = {0};
    	int test2[10] = {0};
    	int test3[10] = {0};
    
    	FILE* readThis;
    	if ((readThis = fopen ("scores.txt", "r"
    		)) ==NULL)  {
    			printf("\nError opening file \n");
    			exit(101);
    	}
    	int i = 0;
    	while (!feof(readThis))  {
    		fscanf( readThis, "%d %d %d %d", &idNum[i], &test1[i], &test2[i], &test3[i] );
    		i++;
    
    	}
    		fclose(readThis);
    
    	averages( test1, test2, test3, &avg1, &avg2, &avg3 );
    	printf("%d\n", test1[9]);
    	selectionSort ( test1, test1[9] );
    	printf("%d\n", test1[9]);
    
    		return 0;
    	}
    void averages( int oneTest[ ], int twoTest[ ], int threeTest[ ], int* average1, int* average2, int* average3 )
    {
    	*average1 = 0;
    	*average2 = 0;
    	*average3 = 0;
    
    	for( int i = 0; i < 10; i++ ){
    		*average1 += oneTest[i];
    	}
    	
    	for( int j = 0; j < 10; j++ ){
    		*average2 += twoTest[j];
    	}
    	
    	for( int k = 0; k < 10; k++ ){
    		*average3 += threeTest[k];
    	}
    	
    	return;
    }
    void selectionSort (int list[ ], int last )
    {
    	int smallest;
    	int tempData;
    
    	for( int current = 0; current < last; current++ )
    	{
    		smallest = current;
    		for ( int walk = current + 1; walk <= last; walk++ )
    			if ( list[walk] < list [smallest] )
    				smallest = walk;
    
    		tempData = list[current];
    		list[current] = list[smallest];
    		list[smallest] = tempData;
    	}
    
    		return;
    }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Quote Originally Posted by chelpme View Post
    hi all I am trying to use a selection sort on an array but I keep getting a debug error I've never seen before:
    "Run Time Check Failure #2- Stack around the variable 'avg' was corrupted." any help is appreciated thanks
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    
    void averages( int oneTest[ ], int twoTest[ ], int threeTest[ ], int* average1, int* average2, int* average3 );
    void selectionSort ( int list[ ], int last );
    int main( void )
    {
    	int avg1, avg2, avg3;
    
    	/*standardDeviation = ( ( count * square ) - ( sum * sum ) ) / ( count * ( count - 1 ) );*/
    
    	int idNum[10] = {0};
    	int test1[10] = {0};
    	int test2[10] = {0};
    	int test3[10] = {0};
    
    	FILE* readThis;
    	if ((readThis = fopen ("scores.txt", "r"
    		)) ==NULL)  {
    			printf("\nError opening file \n");
    			exit(101);
    	}
    	int i = 0;
    	while (!feof(readThis))  {
    		fscanf( readThis, "%d %d %d %d", &idNum[i], &test1[i], &test2[i], &test3[i] );
    		i++;
    
    	}
    		fclose(readThis);
    
    	averages( test1, test2, test3, &avg1, &avg2, &avg3 );
    	printf("%d\n", test1[9]);
    	selectionSort ( test1, test1[9] );
    	printf("%d\n", test1[9]);
    
    		return 0;
    	}
    void averages( int oneTest[ ], int twoTest[ ], int threeTest[ ], int* average1, int* average2, int* average3 )
    {
    	*average1 = 0;
    	*average2 = 0;
    	*average3 = 0;
    
    	for( int i = 0; i < 10; i++ ){
    		*average1 += oneTest[i];
    	}
    	
    	for( int j = 0; j < 10; j++ ){
    		*average2 += twoTest[j];
    	}
    	
    	for( int k = 0; k < 10; k++ ){
    		*average3 += threeTest[k];
    	}
    	
    	return;
    }
    void selectionSort (int list[ ], int last )
    {
    	int smallest;
    	int tempData;
    
    	for( int current = 0; current < last; current++ )
    	{
    		smallest = current;
    		for ( int walk = current + 1; walk <= last; walk++ )
    			if ( list[walk] < list [smallest] )
    				smallest = walk;
    
    		tempData = list[current];
    		list[current] = list[smallest];
    		list[smallest] = tempData;
    	}
    
    		return;
    }
    Hi, it might sound odd, but are you sure you are getting that error from this program or there any other programs that are there wihtin in our environment, if you are running in IDE that could be the case, because I tried running this program, it went fine and also one more thing I doubt about the error is, there is no variable named 'avg' in your program, you might want to double check.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by sbattu View Post
    Hi, it might sound odd, but are you sure you are getting that error from this program or there any other programs that are there wihtin in our environment, if you are running in IDE that could be the case, because I tried running this program, it went fine and also one more thing I doubt about the error is, there is no variable named 'avg' in your program, you might want to double check.
    ah yes you are correct there is no avg in my program I meant to type 'avg1'. So I tried rearranging the variables like this:
    Code:
    	int idNum[10] = {0};
    	int test1[10] = {0};
    	int test2[10] = {0};
    	int test3[10] = {0};
    
    	int avg1, avg2, avg3;
    Interestingly now the same error occurs for idNum, when I press ignore it repeats the same errors for the arrays test1, test2, and test3. The program runs fine in the background I just dont understand this message I am getting. I am a total newbie so can you please explain what you mean by IDE ^^ ty.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    IDE stands for "Integrated Development Environment". It includes an editor, a debugger, a compiler and linker, etc.

    If you are writing your code in an editor like "NotePad", then you don't have an IDE. If you have an editor and pull down menu's or buttons to assist you in programming, then you have an IDE.

    It sounds like your program is using some memory that it shouldn't be. It may take a long time before it crashes the program, if the amount is very small.

    In this line, you're running outside the array, in selection sort:
    Code:
    for( int current = 0; current < last; current++ )
    That for loop is fine and good, but you then reference "current + 1", in the body of the loop. That goes 1 int too far.
    Make it "current < last - 1;".
    Last edited by Adak; 04-26-2009 at 11:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 05:42 PM
  2. Selection Sort problem #2
    By Twigstar in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 08:27 PM
  3. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 09:39 PM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 09:33 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21