Home > Enterprise >  Use a function to change the contents of the array, i.e. multiply each number in the array by 2
Use a function to change the contents of the array, i.e. multiply each number in the array by 2

Time:03-01

Question

Use your function to change the contents of the array, i.e. multiply each number in the array by 2.

When your function has finished and your program continues in your main(), print the contents of your array in your main().

See if the changes made to the contents of the array in your function can be seen. If not, why?

Further

I'm trying to multiply the original array by 2 onto another array. Can anyone spot where I've went wrong?

#include <stdio.h>
#include <math.h>

#define SIZE 5

//function signatures
int getMultiples(int[]);

    

//main function
int main() 
{
    //main variables
    int array[SIZE];
    int multiples[SIZE];

    printf("\nPlease enter 5 numbers into an array.\n");
  

    for(int i = 0; i < SIZE; i  )
    {
        scanf("%d", &array[i]);
    }
    
    multiples[] = getMultiples(array);
    
    
    printf("\nThis program will multiply all numbers by 2\n\n");

    for (int i = 0; i < SIZE; i  )
    {
        printf("%d\n", multiples[i]);
    }
    
    return 0;
}

int getMultiples(int arr[]) 
{
    //function variables
    int i;
    int multiples[SIZE];
    
    for (i = 0; i < SIZE; i  ) 
    {
        multiples[i] = arr[i] * 2;
    }

    return multiples[];
}

CodePudding user response:

This statement

multiples[] = getMultiples(array);

is syntactically and semantically invalid. This construction multiples[] is wrong and arrays do not have the assignment operator.

Also the definition of the function getMultiples is also wrong.

Again this statement

    return multiples[];

is invalid.

What you are trying to do is to return the local array

    int multiples[SIZE];

but the function return type is int. At least you needed to declare the return type as int *.

But in any case the local array that has automatic storage duration will not be alive after exiting the function.

If to use your approach then the function can look the following way

void getMultiples( int a1[], const int a2[], size_t n ) 
{
    for ( size_t i = 0; i < n; i   ) 
    {
        a1[i] = 2 * a2[i];
    }
}

and in main the function is called like

getMultiples( multiples, array, SIZE );

Pay attention to that the function definition should not depend on the magic number SIZE.

By the way in your assignment there is written

Use your function to change the contents of the array, i.e. multiply each number in the array by 2.

It means that you need to change the source array,

In this case the auxiliary array multiples is redundant. The function could be defined the following way

void getMultiples( int a[], size_t n ) 
{
    for ( size_t i = 0; i < n; i   ) 
    {
        a[i] *= 2;
    }
}

and called in main like

getMultiples( array, SIZE );

CodePudding user response:

You promise to return a single int.

int getMultiples(...);

You don't:

return multiples[];

You attempt to assign to a whole array (either a single int or an array....).

multiples[] = getMultiples(array);

That does not work in C.
And judging from what happens when trying your code, your compiler should have told you.

  • Related