Thread: inputting unknown array

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    inputting unknown array

    What my program is supposed to do is take in an unknown sized array of random integers and then sort the integers and display some stats about them. Right now everything works except when im taking in my array in order for me to stop the inputting numbers i have to type a letter for the program to proceed to sort things.

    heres my code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void sort(int arr[], int i);
    void math(int arr[], int i, float *sum, float *largest, float *smallest, float *mean, int *count);
    
    int main()
    {
    	int *arr;
    	int i;
    	int value;
    	float sum, largest, smallest, mean;	
    	int cap;
    	int count;
    
    	cap = 10;	
    	arr = malloc(cap * sizeof(int));
    
    	count = 0;
    	
    	while ( 1 == scanf ("%d", &value))
    	{	
            	if ( count >= cap )
    		{
    			printf("\nThreshhold of %d reached ", cap);
    			cap += 10;
    			printf("\nIncreased array size to %d \n", cap);
    			arr = (int*)realloc ( arr, cap * sizeof( int ));
              	}
             	arr[ count++ ] = value;
            }
    	
    	for(i=0; i<count; i++)
    	{
    		printf("%d ", arr[i]);
    	}
    
    	sort(arr, i);
    	math(arr, i, &sum, &largest, &smallest, &mean, &count);
    
    	printf("*arr = ");
    
    	for( i = 0; i < count; ++i)
    	{
    		printf("%d ", arr[i] );
    	}
    	printf("\n");
    
    	printf("The sum of the array is: %f\n", sum);
    	printf("Largest: %f\n", largest);
    	printf("Smallest: %f\n", smallest);
    	printf("Mean: %f\n", mean);
    	
    	free( arr );
    	return 0;
    }
    
    void sort(int arr[], int i)
    {
    	int k;
    	int bound = i-1;
    	int t;
    	int last_swap;
      
    	while (bound) 
    	{
    		last_swap = 0;
    		for ( k=0; k<bound; k++ )
    		{
    			t = arr[k];
    			if ( t > arr[k+1] ) 
    			{
    				arr[k] = arr[k+1];
    				arr[k+1] = t;
    				last_swap = k;
    			}
    		}
    		bound=last_swap;        
    	}
    }
    
    void math(int arr[], int i, float *sum, float *largest, float *smallest, float *mean, int *count)
    {
    
    		
    	for(i=0; i<*count; i++)
    	{
    		*sum = arr[i] + *sum;
    	}
    	
    		
    	*largest= arr[0];
    	for(i=0; i<*count; i++)
    	{
    		if(arr[i]>*largest)
    		{
    			*largest = arr[i];
    		}
    	}
    	
    	*smallest = arr[0];
    	for(i=0; i<*count; i++)
    	{
    		if(arr[i]<*smallest)
    		{
    			*smallest = arr[i];
    		}
    	}
    	
    	*mean = *sum/10;
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dr Saucie View Post
    Right now everything works except when im taking in my array in order for me to stop the inputting numbers i have to type a letter for the program to proceed to sort things.
    And you would prefer that the program magically and correctly guess when you are done?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    well i have tried a few things but what ever I do messes up the program even more, Im just looking for some advice on what to do NOT THE ANSWER!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dr Saucie View Post
    well i have tried a few things but what ever I do messes up the program even more, Im just looking for some advice on what to do NOT THE ANSWER!
    You mean you have tried a few things to see if you can get it to magically guess the answer?

    I'm sorry, I'm not trying to tease you. I just do not understand what your problem or question is: do you want a better idea, other than entering the letter at the end?

    IMO there probably isn't one. Using a clearly defined "DONE" signal is simple (simple is always better than unnecessarily complicated) for the programmer and simple and easy for the user. It's the equivalent of filling in a form on a web page and pressing "submit" to indicate you have entered all the data.

    The only thing you might want to add for a finished app is a confirm dialogue, like "Are you sure you are done?".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array, size unknown at compile time
    By gah_ribaldi in forum C# Programming
    Replies: 10
    Last Post: 12-04-2009, 05:42 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM