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;
}