Fill Array with Valid Inputs coderinme

Fill Array with Valid Inputs coderinme

Fill Array with Valid Inputs coderinme
Now let us fill an array with elements depending upon the range given by the user. The user gives a particular set of range of numbers. The program will work to fill the array with only those elements which lies in the given range.

So Write a function to have a user enter some number of integers into an array. The values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integers array and size of array are passed into function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform the user of the error, but do not count as a valid input.

#include<stdio.h>
#define SIZE 50
void f(int a[],int n) {
int temp,i = 0;
while( i < n )
{
printf("Enter the %dth number: ",i);
scanf("%d",&temp);
if( ! ( temp >= -100 && temp <= 100 ) ) {
printf("You must enter values between [-100,100] !!!\n");
continue;
}
a[i] = temp;
i++;
}
printf("Your entered numbers are: ");
for( i = 0; i < n ; i++ ) {
printf("%d,",a[i]);
}
}
int main()
{
int a[SIZE],n;
printf("Enter How much number you want to enter: ");
scanf("%d",&n);
f(a,n);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Fill Array with Valid Inputs coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.