Thread: Calculations on an entered array

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    I am creating a simple program, for the entry of an array, which will then sort the array into ascending order, printing out the sorted array, its min, max and median. I have started the code but after many attempts it seems to being going futher backwards each time i change anything.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    main()
    {	
    	int a[100];
    	int i,j, value, n;
    	int min = 100;
    	int max = 0;
    	float median;
    
    	
    	// Upto 100 items can be created in a[i]
    	for(i=0; i<100; i++)
    	{
    		
    		// Input of the numbers into array a[i]
    		scanf("%d", &a[i]);
    		n = i;
    		
    		if(a[i]<100)
    		{
    			// Terminates input and performs the sort; prints the min, max and median values 
    			if(a[i] == -42)
    			{		
    				printf("%s", "Sorted List: ");
    				
    				// Simple insertion sort
     				for(i = 1; i <100; i++)
    				{
            			value = a[i];
      				for (j = i - 1; j >= 0 && a[j] > value; j--) 
    				{
    			                a[j + 1] = a[j];
        	    		        }
           		 		         a[j + 1] = value;
       				}
    				}
    				
    				// Find the min of array
    				if(a[i] < min)
    				{		
     					min = a[i];
    				}
    				else
    				{
     					min = min;
    				}	
    				
    				// Find the max of array
    				if(a[i] > max)
    				{		
     					max = a[i];
    				}
    				else
    				{
     					max = max;
    				}	
    				
    				//Find the median of array
    					
    				//Prints the sorted array
    				for(i=0; i<n; i++)			
    				{	
    					printf("%d ", a[i]);	
    				}
    
    				printf("\n");
    				printf("%s %d\n ", "Min: ", min);
    				printf("%s %d\n ", "Max: ", max);
    				printf("%s %d\n ", "Median: ", median);
    			}
    		}
    		
    		//Error if the number entered is above 100
    		if(a[i]>100) 
    		{
    			printf("Number must be between 0 and 100");
    			a[i] = 0;
    		}	
    	}
    The functions are meant to be performed when -42 is entered without that also being added to the array.
    Your help would be greatly appriciated, i am eager to learn how to accomplish these things.
    Last edited by MV1; 03-09-2009 at 02:31 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by MV1 View Post
    The functions are meant to be performed when -42 is entered without that also being added to the array.
    put a condition to :
    Code:
    scanf("%d", &a[i]);

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sit back for a minute and think about the logic of the program and perhaps write a flowchart.
    Then you need to fix the indentation with this piece of code:
    Code:
    				// Simple insertion sort
     				for(i = 1; i <100; i++)
    				 {
            			value = a[i];
      				for (j = i - 1; j >= 0 && a[j] > value; j--) 
    				{
    			  		a[j + 1] = a[j];
        	    		}
           		 		a[j + 1] = value;
       					}
    				}
    Totally unreadable.
    Finally, make main return int, not nothing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Apoloigies for the code not be indented correctly hope that is sorted now, the flow of the program seems to be a major issue although im notsure the code is up to scratch either.

    As for the condition i thought; if(a[i] == -42) was working without including into the array in one of my other versions but i dont seem to be able to do it again.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    MV1 - input, output, and computations or processing (sorting in your case), should *not* be mixed into the same function.

    In general:

    Get your input - print it (or a part of it), on the screen, so you know you've got the input right.

    Call your processing function(s) - check each one that it's doing what you want,

    Then output your reports to files, and check those.

    I'll be back
    Last edited by Adak; 03-09-2009 at 06:24 PM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Been away and looked at it and had some advice, its pretty much done.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 100
    
    int main()
    {    
    	int a[N] = {0};   
    	int i, j, value, n;   
       
    	// Upto 100 items can be created in a[i]   
        scanf("%d", &a[0]);    
    	for(i = 1; i < N && a[i-1] < 100 && a[i-1] != -42; i++)      
     	
    	 // Input of the numbers into array a[i]   
      	scanf("%d", &a[i]);  
    	n = i;    
     
     	if(a[n-1] > 100) 
     	{    
      		printf("Number must be between 0 and 100");   
        	exit(1);   
        }   
     	
    	 if (a[n-1] == -42) 
     	{       
    		a[n-1] = 0;     
      		n--;    
      	}    
       
      	printf("Sorted List: ");    
    	 
     	// Simple insertion sort    
    	for(i = 1; i < n; i++) 
    	{        
     		value = a[i];       
    	  	for (j = i - 1; j >= 0 && a[j] > value; j--)        
    		a[j + 1] = a[j];       
    		a[j + 1] = value;    
    	}      
    	
    	//Prints the sorted array    
    	for(i = 0; i < n; i++)       
    	printf("%d ", a[i]);     
    	printf("\n");    
    	printf("Min: %d\n", a[0]);    
    	printf("Max: %d\n", a[n-1]);   
    	printf("Median: %f", n % 2 ? a[n/2] : 0.5*(a[n/2] + a[n/2-1]));
    	}

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK.

    I'll edit down the above post of mine.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	for(i = 1; i < N && a[i-1] < 100 && a[i-1] != -42; i++)      
    
    //...
    
    	for(i = 0; i < n; i++)
    These two looks suspect, because they have no starting or ending brace. And your indentation is failing, so it's difficult to see if it's supposed to be a one-line for loop or not.
    And the condition in the first for might just be a little too difficult.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM