Question: I cannot get my read array function to work properly. This is a C program. I have to enter numbers into the array, but I
I cannot get my read array function to work properly. This is a C program.
I have to enter numbers into the array, but I crash once I type in a number. Also I need to flush out character data when entered.
#include
#define MAXSIZE 10
void swap(int arr[],int i,int j);
void print(const int arr[],int size);
void sort(int arr[],int size);
int read(int arr[]);
int main()
{
int arr[MAXSIZE];
int size=read(arr);
print(arr,size);
sort(arr,size);
print(arr,size);
}
void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void print(const int arr[],int size)
{
printf(" The Array is ");
int i;
for(i=0;i printf("%d ",arr[i]); printf(" "); } void sort(int arr[],int size) { int min,min_index,i,j; for( i = 0;i<=size - 2;i++) { min = arr[i]; min_index = i; for(j=i+1;j { if (arr[j] < min) { min = arr[j]; min_index = j; } } swap(arr, i, min_index); } } int read(int arr[]) { int s,size,i; printf("Enter numbers into array. "); scanf("%d", arr[i]); while ((s=scanf("%d", &size)) != EOF && i { if (s==0) { printf("Invalid character data. "); while(getchar()!=' '); } else { printf("Enter numbers into array. "); scanf("%d", arr[i]); arr[i]=size; i++; } } return i; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
