I'm trying to write an effective function that receives an array the size of n
and a
and b
.
The function should search all the numbers in the array such that b-a < array[i]
and collect them to a new sorted array called incoming
.
For instance, for the input 11,12,8,15,3,12,3,12
, b=15
, a=8
the output would be a 6
size array that will contain the values 8,11,12,12,12,15
(anything that is higher than (b)15-(a)8 ).
This is my own code attempt:
#include<stdio.h>
#include<stdlib.h>
int* f5(int arr[], int n, int a, int b, int* p)
{
int i,min,minIndex;
int* incoming = (int*)malloc(*p*sizeof(int));
for ( i = 0; i < n; i++)
{
if (arr[i]>(b - a))
{
incoming[i] = arr[i];
(*p)++;
}
}
return *incoming;
}
void main()
{
int arr[] = { 12,3,12,3,15,8,12,11 };
int p, i;
int incoming[] = f5(arr, sizeof(arr) / sizeof(arr[0]), 8,15, &p);
printf("The size is: %d and the new marahc is: ", p);
for (i = 0; i < p; i++) {
printf("%d", incoming[i]);
}
free(incoming);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…