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;
}