Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. All functions should be written AFTER the main procedure. 2. A function proto

ID: 650782 • Letter: 1

Question

1. All functions should be written AFTER the main procedure.
2. A function prototype should be written for each function and placed BEFORE the main
procedure.
3. Each function should have a comment explaining what it does.
4. Each function parameter should have a comment explaining the parameter.
5. Prompt for the number of elements of the list.
6. Allocate an array whose size equals the number of elements specified by the user.
7. Read into the array the elements of the list.
8. Write a function count() which counts the number of negative elements and the number of
non-negative elements of an array. The function should take four parameters, the array, the
number of elements in the array, the number of negative elements and the number of nonnegative
elements.
The function sets the number of negative and non-negative elements but does not return any
value.
9. In the main program, call the function count() to get the number of negative elements and
the number of non-negative elements of the array.
10. Allocate an array whose size equals the number of negative elements.

12. Write a function split() which takes as input 3 arrays, A, B, and C, and stores the negative
elements of A in B and the non-negative elements of A in C. (Use better array names than A,
B, and C). The size of array B should be exactly the number of negative elements in array A.
The size of array C should be exactly the number of non-negative elements in A.
As you copy elements from A to B or C, count the number of elements copied to each array.
When the function completes, check that the number copied equals exactly the array size. If
it does not, print an error message and exit.
The function should take six parameters, array A, the size of array A, array B, the size of
array B, array C, and the size of array C.
The function modifies arrays B and C but does not return any value.
13. Write a function print_array() which prints the elements of an array.
The function should take two parameters, the array and the array length.
The function does not modify or return any values.

14. In the main program, call the function split(), passing the input array and the arrays
whose sizes equal the number of even and odd elements.
15. In the main program, print the phrase

Explanation / Answer

#include <stdio.h>
void count(int arr[],int len,int *x, int *y);
void split(int *arr[],int *neg[],int *pos[],int Alen,int Blen,int Clen);
void print_array(int arr[],int length);
int main()
{
int elements; /*to store elements given by user */
printf("Enter how many elements you want ");
scanf("%d",&elements);
int array[elements]; /* allocating array of of size user entered*/
int i; /* variable used for for loop to read values into integer*/
int negative=0,nonnegative=0;/* negative and non-negative store values */
for(i=0;i<elements;i++){
printf("enter number");
scanf("%d",&array[i]);
}
count(array,elements,&negative,&nonnegative);
int negative[negative]; /* declaring negative arrray */
int positive[nonnegative];
split(&array,&negative,&positive,sizeof(array),sizeof(negative),sizeod(positive));

printf("Now printing arrays ");
print_array(array,size0f(array)); /* printing array */
print_array(negative,size0f(negative));
print_array(positive,size0f(positive));
free(array); /* making array free */
free(positive);
free(negative);
return 0;
}
void count(int arr[],int len,int *x, int *y){ /* counting values negative and non negative */
int i;
for(i=0;i<len;i++){
if(arr[i]<0){
*y++;
}
else{
*x++;
}
}
}
void split(int *arr[],int *neg[],int *pos[],int Alen,int Blen,int Clen){/* split and fill arrays*/
int i,countneg=0,countpos=0; /* calculating array for my loop and count is for checking elements */
for(i=0;i<Alen;i++){
if(*arr[i]<0){
*neg[i]=*arr[i];
countneg++;
}
else{
*pos[i]=*arr[i];
countpos++;
}
}
if(countneg!=Blen){
printf("error occured");
exit(0);
}
if(countpos!=Clen){
printf("error occured");
exit(0);
}
}
void print_array(int arr[],int length){/* printing the arrays*/
int i;/* for loop variable*/
for(i=0;i<length;i++){
printf("%d",&arr[i]);
}
printf(" ");
}

output

Enter how many elements you want 5

enter number 1

enter number 2

enter number -1

enter number -2

enter number 3

Now printing arrays

1 2 -1 -2 3

-1 -2

1 2 3