Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Problem with effective function and arrays in C

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I corrected your f5 function because you were not allocating your new array properly, since you were doing the malloc with (*p) before it was set to the number of elements you needed to store. After that, I ordered the incoming array in ascending order as shown in the example output with qsort. I also added the part where you take input values from the user, since in the code you've posted you were probably just trying that specific test case.

#include<stdio.h>
#include<stdlib.h>

// compare function for qsort
int mycompare (const void* a, const void* b) {
    int val1 = *(const int*)a;
    int val2 = *(const int*)b;
    
    return (val1 - val2);
}
int* f5(int arr[], int n, int a, int b, int* p) {
    int target = b - a;
    int i;
    
    for (i=0;i<n;i++) { // storing number of values > (a-b)
        if (arr[i]>target) (*p)+=1;
    }
    
    int* incoming;
    if ((incoming= malloc((*p)*sizeof(int)))==NULL); // should always check malloc errors
    {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    int j = 0;
    i = 0;
    while (i<n && j<(*p)) { // storing values in new array
        if (arr[i]>target) {
            incoming[j] = arr[i];
            j++;
        }
        i++;
    }
    
    return incoming;
}

void main() {
    int n, a, b, i;
    
    printf("Insert 'a' value: ");
    scanf("%d", &a);
    printf("Insert 'b' value: ");
    scanf("%d", &b);
    printf("Insert array size: ");
    scanf("%d", &n);
    
    int arr[n];
    printf("Insert array values: 
");
    for (i=0;i<n;i++) {
        scanf("%d", &arr[i]);
    }
    
    int size = 0;
    int *incoming = f5(arr, n, a, b, &size);
    qsort(incoming, size, sizeof(int), mycompare); // sorting array
    
    printf("The size is: %d and the new marahc is: ", size);
    for (i=0; i<size;i++) {
        printf("%d ", incoming[i]);
    }
    free(incoming);
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...