Can anyone help me with a few C functions? I can\'t use any arrow notation or br
ID: 3936657 • Letter: C
Question
Can anyone help me with a few C functions? I can't use any arrow notation or brackets, just pointers and pointer arithmetic. In function main I have to first read the input size from the user, performing an error check using the check_size function. Call functions initialize_array and print_array to store and print the random numbers. Then display the results as shown in the output below. In main I must also declare an integer pointer and malloc space for it based on the size the user enters, assign the address of this pointer/array to another integer pointer and pass this pointer to all the functions.
The functions are:
void initialize_array(int *, int ): This function takes an integer pointer and the input size and stores random numbers using the integer pointer. The random numbers range from the value 1 to 5 only.
void print_array(int *, int): This function takes an integer pointer and the input size and prints out random numbers using the integer pointer.
int check_size(int): This function takes an integer number and checks if the number is between 1-100 or not. If it is, it returns 1, otherwise returns 0.
Sample Output:
Enter the size of the input: -12
Invalid input enter the size of the array again: 123
Invalid input enter the size of the array again: 0
Invalid input enter the size of the array again: 6
Input array
1 2 1 5 2 5
Enter the size of the input: 3
Input array
1 4 1
Explanation / Answer
#include <stdio.h>
int check(int n)
{
if(n>=1 && n<=100)
return 1;
else
return 0;
}
void initialize_array(int *arr, int n)
{
int i;
for(i=0;i<n;i++)
*(arr+i) = rand()%5 +1;
}
void print_array(int *arr, int n)
{
int i;
printf("Input Array ");
for(i=0;i<n;i++)
printf("%d ",*(arr+i));
}
int main(void)
{
int n;
printf("Enter the size of the input: ");
scanf("%d",&n);
while(check(n)!=1)
{
printf("Invalid input enter the size of the array again: ");
scanf("%d",&n);
}
int *arr = (int *)malloc(sizeof(int)*n);
initialize_array(arr,n);
print_array(arr,n);
return 0;
}
OUTPUT:
Enter the size of the input: -12
Invalid input enter the size of the array again: 0
Invalid input enter the size of the array again: 123
Invalid input enter the size of the array again: 6
Input Array
4 2 3 1 4 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.