1 Attachment(s)
troubleshooting beginning array code
Hi everyone! i'm new to C and i'm having a bit of trouble getting my program to work properly. Its using the sorting algorithm on the score of the student's tests and storing them into arrays which are returning some wrong values - here is the information file (AG_SPRING.TXT)
1001 86
1094 70
1419 79
1919 81
2092 80
3199 82
3456 90
4009 81
4098 64
4308 84
4567 86
4770 70
4810 83
5809 95
6008 84
and code
Code:
#include <stdio.h>
#define ARY_SIZE 26
// Function Declarations
int getData (FILE* sp1, int stu[], int sco[]);
void sort (int size, int sco[]);
void printWelcome (FILE* sp2);
void printGoodbye (FILE* sp2);
void printResults (FILE* sp2, int stu[], int sco[]);
// Main
int main (void)
{
FILE* sp1;
FILE* sp2;
int blank;
int stu[ARY_SIZE];
int sco[ARY_SIZE];
int closeStatus;
int size;
if(!(sp1 = fopen("AG_SPRING.TXT", "r")))
{
printf("Error opening AG_SPRING.TXT for reading");
;
}// if open input
if(!(sp2 = fopen("SORTED.TXT", "w")))
{
printf("Error opening SORTED.TXT for writing");
;
} // if open input
printWelcome(sp2);
size = getData(sp1, stu, sco);
sort(size, sco);
printResults(sp2, stu, sco);
printGoodbye(sp2);
fclose(sp1);
closeStatus = fclose(sp2);
if (closeStatus == EOF)
{
printf("File close error.\a\n");
;
} // if close error
printf("File successfully created\n");
scanf("%d", &blank);
return 0;
}
int getData (FILE* sp1, int stu[], int sco[], int size)
{
int stuID = 0;
int score = 0;
int loader = 0;
while (loader < ARY_SIZE && fscanf(sp1,"%d %d", &stuID, &score) != EOF)
{
// Load values into the Arrays
if (score >= 0)
{
stu[loader++] = stuID;
sco[loader++] = score;
}
else
printf("\nData point %d invalid. Ignored. \n",
stuID);
// Test to see if loader is full
if (loader == ARY_SIZE)
printf("\nToo much data. Process what read.\n");
}
return loader;
} // getData
void sort (int size, int sco[])
{
int loadPosition = 0;
int loadMin = 0;
int temp = 0;
int loadPositionb = 1;
// advance the position through the entire array
for (loadPosition = 0; loadPosition < ARY_SIZE - 2; loadPosition++)
{
/* assume the min is the first element */
loadMin = loadPosition;
/* test against all other elements */
for (loadPositionb; loadPositionb < ARY_SIZE - 1 ; loadPositionb++)
{
/* if this element is less, then it is the new minimum */
if (sco[loadPositionb] < sco[loadMin])
{
/* found new minimum; remember its index */
loadMin = loadPositionb;
}
}
// Swap positions include stu array when you fix the number problems
temp = sco[loadPosition];
sco[loadPosition] = sco[loadMin];
sco[loadMin] = temp;
}
return;
}
void printWelcome (FILE* sp2)
{
fprintf(sp2, "These are the Student IDs and scores sorted from lowest to highest\n\n");
return;
}
void printGoodbye (FILE* sp2)
{
fprintf(sp2, "Thank you for using this program! Goodbye!");
return;
}
void printResults (FILE* sp2, int stu[], int sco[])
{
int i;
for (i = 0; i < ARY_SIZE; i++)
{
fprintf(sp2, "%d %2d \n\n", stu[i], sco[i] );
}
printf("\n\n");
return;
}
thanks in advance!