Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, how do values from an array get inputted into a function?

Tags:

arrays

c

I have been having difficulty with arrays in functions. I want the array1 in the main program loop to share the same value as array1 in the function, but am having severe difficulty. I understand this may be easy to some, but as a first year programming student problems like this are expected. Would appreciate the help.

#include <stdio.h>

int FillArray(int array1[9])
{

    int array1[9], array2[9], i, n=0;


    for (i = 0; i < 9; ++i)
    {
        if  (array1[i] > 0)
            array2[i] = array1[i] * 2;
            else
                array2[i] = array1[i] * 10;

        printf("\n%d", array2[10]);
    }
    return 0;
} /* End of FillArray Function */

int main()
{
    int array1[9] = { 40, 13, -5, 22, 10, 80, -2, 50, 9, -7 };
    FillArray(array1[9]);


}
like image 932
Nick Avatar asked Feb 20 '26 04:02

Nick


1 Answers

In the fillarray() the printing statement is not valid. array2[10] is not a valid range as the loop will run upto 9. So make it less or equal to 9. It will be like this

array2[i]

Or you can write it as

array2[9]

And you can pass the array1[] in the main function as an parameter. You need to rewrite it like this

int main(int array1[])

Or you can do this in the main function

Int main()
{ 
     /* Write what you want /*
      array1[] = { _the_values_you_want to_give}
      fillarray(array1)

Hope this will help

like image 123
Sandipan Avatar answered Feb 21 '26 19:02

Sandipan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!