troubleshooting beginning array code

This is a discussion on troubleshooting beginning array code within the C Programming forums, part of the General Programming Boards category; Hi everyone! i'm new to C and i'm having a bit of trouble getting my program to work properly. Its ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    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!
    Attached Files Attached Files
    • File Type: c hw8.c (2.8 KB, 38 views)

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Are you trying to implement a bubble sort here?

    If so you could use some minor tweaking. the first for loop:
    Code:
    for (loadPosition = 0; loadPosition < ARY_SIZE - 2; loadPosition++)
    needs to compare the number to every other number in the array but itself. Not itself and something else.

    Code:
    loadMin = loadPosition;
    I am not sure what you are trying to accomplish here.

    The second for loop:
    Code:
    for (loadPositionb; loadPositionb < ARY_SIZE - 1 ; loadPositionb++)
    Needs to start at the next number in the loop and compare itself to every other member. So perhaps a starting value of 1 through ARY_SIZE.

    Now to compare and swap. This needs to be inside the inner most loop. Basically the check is:

    Code:
    for{
         for{
       
    AM I less than the number before me
         IF YES THEN  SWAP me with the number before me (perhaps you could use a temp tro facilitate.
    
         }
    }
    EDIT: Just wondering if you realized you never used your size variable for any of your loops and also you haven't sorted you array with student ids.
    Last edited by AndrewHunter; 06-24-2011 at 01:58 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ask yourself... "If the file fails to open do I want this to continue processing?"
    Hint: The answer is "No". So you need to exit....

    Code:
    sp1 = fopen("AG_SPRING.TXT", "r")
    if (!sp1)
      { printf("Error opening AG_SPRING.TXT for reading");
         exit(1); }

    This is going to create problems with your arrays and may cause bounds errors because you are double incrementing your index counter...
    Code:
    if (score >= 0)
     {
         stu[loader++] = stuID;
         sco[loader++] = score;
      }
    Try it like this....

    Code:
    if (score > -1)   
     { stu[loader] = stuID;
        sco[loader] = score;
        loader++;  }

    Also don't sprinkle semicolons around on blank lines... they can have some very surprising effects on your code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Please
    By jsking09 in forum C Programming
    Replies: 3
    Last Post: 03-03-2010, 02:37 AM
  2. Replies: 6
    Last Post: 06-30-2009, 10:37 AM
  3. Troubleshooting code
    By bcianfrocca in forum C++ Programming
    Replies: 11
    Last Post: 11-17-2005, 05:46 PM
  4. beginning array question
    By Noobie in forum C Programming
    Replies: 22
    Last Post: 05-01-2003, 04:18 AM
  5. Troubleshooting DDE
    By musicafterhours in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2001, 08:26 AM

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