Kaan T.
Kaan T.

Reputation: 11

How to pass arrays to functions

I am trying to write a program which calculates some bags and weights. I wrote it without using functions but I have to use functions and I am really bad at it.

The code normally works, but I just can't implement it with functions. It stops working after printing array A, and just 0s when printing array B.

My code is:

#include <stdio.h>
#include <math.h>
int f1(int N);
int f2(int N);
int f3(int N, float A[20]);

int main(void)
{
    int N;
    f1(N);
    return 0;
}

int f1(int N)
{
    for(;;)
    {
        printf("Enter N(the number of bags) (Between 1 and 20): ");
        scanf("%d", &N);
        if (N < 1 || N > 20)
        {
            continue;
        }
        else
        {
            break;
        }
    }
    f2(N);
}

int f2(int N)
{
    float A[20];
    int i;
    for(i=0; i<N;i++)
    {
        printf("Enter the weight of the bag with potatoes %d: ", i+1);
        scanf("%f", &A[i]);
    }

    printf("\n\nThe weights of the initial bags (the A array):\n");
    for(i=0; i<N;i++)
    {
        printf("%.1f " ,A[i]);
    }
    f3(N, &A[20]);
}

int f3(int N, float A[20])
{
    int i;
    float B[10];
    printf("\n\nNow we equalize the weights of bags.\n");
    if (N%2 == 0)
    {
        for(i=0;i<N/2 ;i++)
        {
            B[i] = fabsf(A[i] - A[N-1-i]);
        }
    }
    else
    {
        for(i=0;i<N/2 ;i++)
        {
            B[i] = fabsf(A[i] - A[N-1-i]);
        }
        B[N/2] = A[N/2];
    }

    if (N%2 == 0)
    {
        for (i=0; i<N/2; i++)
    {
        if (A[i] < A[N-1-i])
        {
            A[N-1-i] = A[i];
        }
        else
        {
            A[i] = A[N-1-i];
        }
    }
    }
    else
    {
        for (i=0; i<N/2; i++)
        {
            if (A[i] < A[N-1-i])
            {
                A[N-1-i] = A[i];
            }
            else
            {
                A[i] = A[N-1-i];
            }
        }
        A[N/2] = 0;
    }

    printf("\nThe weights of the new bags (the B array):\n");
    if (N%2 == 0)
    {
        for(i=0; i<N/2 ;i++)
        {
            printf("%.1f " ,B[i]);
        }
    }
    else
    {
        for(i=0; i<N/2 ;i++)
        {
            printf("%.1f " ,B[i]);
        }
        printf("%.1f", B[N/2]);
    }

    printf("\nThe new weights of the initial bags (the A array):\n");
    for(i=0;i<N;i++)
    {
        printf("%.1f ", A[i]);
    }
}

Upvotes: 1

Views: 107

Answers (2)

Gabriel Ilharco
Gabriel Ilharco

Reputation: 1679

To pass an array or pointer as an argument when calling a function in C, you just need to pass it name, in your case,

f3(N, A);

Also, when declaring the function, the length of the array doesn't matter, because C performs no bounds checking for formal parameters. Although it will work this way, it is best to change

int f3(int N, float A[20])

to

int f3(int N, float A[])

or

int f3(int N, float* A)

Upvotes: 2

NathanOliver
NathanOliver

Reputation: 180415

To pass an array to a function just use its name.

f3(N, &A[20]);

should be

f3(N, A);

Upvotes: 5

Related Questions