Thread: Project due tonight, I need some quick help please?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    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
    Last edited by remenissions; 04-28-2011 at 05:11 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    in your sort function...ctr is your counter, with your two arrays a[] and b[], therefore try

    insert
    Code:
    int i, k, temp;
    
    for(i=0; i<ctr; ++i) {
    for(k=i+1; k<ctr; ++k) {
    if (b[i]<b[k]) {
    
    temp=b[i];
    b[i]=b[k];
    b[k]=temp;
    
    temp=a[i];
    a[i]=a[k];
    a[k]=temp;
    }}}

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    ^^be sure to change the < to > if the b[i]>b[k]! my b!

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thanks that worked, i spent so long trying to figure out what i was doing wrong, this project was do tonight but i'll be able to get it slid under his door by morning lol. Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  2. need to calculate it tonight
    By rainrork in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2002, 12:31 AM
  3. Its Late...Due Tonight....Help Please
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-13-2001, 03:18 PM
  4. tonight... tonight...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 11-07-2001, 11:20 PM

Tags for this Thread