Project due tonight, I need some quick help please?
I have to have this project done by tonight, I've been messing with it for hours, basically there are two array lists that must be read sorted and printed. Everything works except its not sorting. I'm trying to use Selection Sort to sort the two arrays from greatest to least. Any thoughts? i've tried everything and cannot seem to get this done.
Code:
/**
* Project 6 by Rick ~~~~
*/
#include <stdio.h>
#define size 40
void printArrayLF(int [] , int [], int , FILE *);
void sort(int [] , int [], int , FILE *);
main()
{
int e, ctr, total, idNum, j1, j2, j3, totalScore;
int i, x;
int id[size];
int ts[size];
float avg;
FILE *Din = fopen("data6a.txt", "r");
FILE *Dout = fopen("project6output.txt", "w");
e = fscanf(Din, "%d %d %d %d", &idNum, &j1, &j2, &j3);
fprintf(Dout,"Ricky ~~~~ - Cm111 - Project 6 - 04/19/2011\n\n");
fprintf(Dout," ");
fprintf(Dout," Candidate Results\n\n");
fprintf(Dout," Id J1 J2 J3 Total Score\n");
ctr = 0;
total = 0;
while(e == 4) {
totalScore = j1 + j2 + j3;
fprintf(Dout, "%10d %6d %6d %6d %11d \n", idNum, j1, j2, j3, totalScore);
ctr++;
total = total + totalScore;
id[ctr] = idNum;
ts[ctr] = totalScore;
e = fscanf(Din, "%d %d %d %d", &idNum, &j1, &j2, &j3);
}
fprintf(Dout," ");
fprintf(Dout, "%1d", ctr);
fprintf(Dout, " Total Candidates\n");
avg = 1.0* total / ctr;
fprintf(Dout," ");
fprintf(Dout, "Average total score is ");
fprintf(Dout, "%5.2f", avg);
sort(id, ts, ctr, Dout);
}
void sort(int a[], int b[], int ctr, FILE *Dout) {
int m, i, k, tempA1, tempA2, tempB1, tempB2;
i = 0;
for(k = 1; k<ctr+1; k++) {
if(b[i] < b[k]) {
tempB1 = b[i];
tempB2 = b[k];
b[i] = tempB2;
b[k] = tempB1;
tempA1 = a[i];
tempA2 = a[k];
a[i] = tempA2;
a[k] = tempA1;
}
i++;
}
printArrayLF(a, b, ctr, Dout);
}
void printArrayLF(int a[], int b[], int ctr, FILE *fl)
{
int l, k, x, e, check, ctr1 = 0;
FILE *Din = fopen("data6b.txt", "r");
e = fscanf(Din,"%d", &check);
fprintf(fl, "\n\n\n Summarized Results\n");
fprintf(fl, " Id totalScore\n");
for(x = 1; x < ctr; x++) {
fprintf(fl,"%10d %7d\n", a[x], b[x]);
}
fprintf(fl, "\n\nScore Check:\n");
while(e == 1) {
Scheck(check,a, b, ctr);
if(Scheck() != -1) {
fprintf(fl, "%9d %3d\n", check, Scheck());
}
e = fscanf(Din,"%d", &check);
}
fprintf(fl, "End of Program");
}
int Scheck(int c, int a[], int b[], int ctr) {
int l, k;
for (l = 0; l < ctr + 10; l++) {
if(c == a[l]) {
k = b[l];
return k;
}
}
return -1;
}
/**
* End Project 6 by Rick ~~~~
*/
Here are the data inputs too
Input A
Code:
3 3 4 10
7 9 1 0
1 5 3 7
13 8 10 8
17 7 4 5
2 6 5 1
6 0 7 4
14 1 4 3
5 7 9 9
19 0 2 5
4 2 0 3
16 8 7 4
9 10 8 0
10 1 6 2
12 4 9 0
15 7 5 10
11 2 3 6
3
10
7
14
8
16
20
11
18 5 5 4
Input B
Code:
3
10
7
14
8
16
20
11