Little help?

This is a discussion on Little help? within the C Programming forums, part of the General Programming Boards category; This code is reading in short integer values from a text file outfile.txt. I'm able to pass these values into ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Little help?

    This code is reading in short integer values from a text file outfile.txt. I'm able to pass these values into an array and print the median. What i was hoping to do is repeat that process for the next 11 values and compare the medians. I was hoping for some help with the loop which would allow me to do this, i'm not sure what i should be incrementing and where?
    At the moment all i'm doing is using printf statements to display the medians what i intend to do is compare the two and if one is so much greater than the other do something else but i'm leaving that for now. Any help at all is much appreciated, maybe a function would be better but passing an array into a function presents a whole new headache really doesn't it , here's my code so far
    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *pFile;
    	int start=0,y,n=11,x;
    	short left[11],left2[11];
    
    	pFile = fopen ("outfile.txt", "r");
    
    	for (start; start<n; start++)
    	{
         fscanf (pFile, "%d", &left[x]);
    	}
               for(x=0; x<n; x++) /* Loop to repeat sort 11 times */
                 {
    	          for(y=0; y<n-1; y++) /* Loop to sort the values */
                   {
    		       	if(left[y]>left[y+1]) /*Checks if 1st value is greater than 2nd */
    		        	{
    			        	short temp = left[y+1];
    			         	left[y+1] = left[y];
    			          	left[y] = temp;
                		}
                    }   
                  }
        for (start; start<n; start++)  /* Repeat Process for next eleven values to compare with  */
    	{
         fscanf (pFile, "%d", &left2[x]);
    	}
               for(x=0; x<n; x++) /* Loop to repeat sort 11 times */
                 {
    	          for(y=0; y<n-1; y++) /* Loop to sort the values */
                   {
    		       	if(left2[y]>left2[y+1]) /*Checks if 1st value is greater than 2nd */
    		        	{
    			        	short temp = left2[y+1];
    			         	left2[y+1] = left2[y];
    			          	left2[y] = temp;
                		}
                    }   
                  }
      	    for (x=0; x < n; ++x ) 
            printf ("left[%d] = %d\n", x, left[x]);
            printf ("left2[%d] = %d\n", x, left2[x]);
           printf("Median1 = %d\n", left[5]);  /* Median is equal to middle value in array */
            printf ("Median2 = %d\n", left2[5]);
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Why are you including windows.h?

    Anyway, back to checking out the code.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    not sure how that got in there!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Why not create some functions to do some of the work for you?
    Like
    - read 11 values into an array
    - compute the median

    Then your main will be a lot less cluttered.

    The indentation could use a little work as well. Mixing spaces and tabs is one thing to avoid when posting the code on the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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