Passing modified array values back to main function in C

11,676

Solution 1

There is nothing wrong with change_array. It does indeed modify the values in the caller's array.

The problem lies the printing function, print_array. You are using the wrong format string for your printf. You need to use %f rather than %ld since these are floating point values and not integers.

Solution 2

Arrays are always passed by reference, so that is not the issue. It works as expected when I add a print function and an extra end curly brace. Can you provide a minimal working example (MWE) so that we can just run the code? (I know this should probably be a comment, but I'm 3 points shy of being able to comment)

Solution 3

I modified your code to make in to a minimal sample and it works fine for me:

  #include <stdio.h>
  #define SIZE 5
  void change_array(double x[], int s);


   int main()
   {
        double x[SIZE] = {3, 5, 6, 12, 32};
        printf("The array is as: \n");
        for(int i = 0;i<SIZE;i++)
            printf("\n%f",x[i]);

        //fill_array(x, SIZE);

        //print_array(x, SIZE);
        change_array(x, SIZE);
        printf("After change, the array is: \n");
        for(int i = 0;i<SIZE;i++)
            printf("\n%f",x[i]);
        // print_array(x, SIZE);
        return 0;
    }


    void change_array(double x[], int s)
    {
        int i=0;

        for (i=0; i<s; i++) 
        {
            if (x[i] < 10) 
            {
                 (x[i] = (x[i] * 2));
            }
        }
    }

Here is the output:

The array is as:

3.000000
5.000000
6.000000
12.000000
32.000000

After change, the array is:

6.000000
10.000000
12.000000
12.000000
32.000000
Share:
11,676
Steven Chen
Author by

Steven Chen

Updated on June 05, 2022

Comments

  • Steven Chen
    Steven Chen almost 2 years

    Sorry if the title is still ambiguous.

    I'm doing this assignment for school and below are my defined function prototypes, the main function and the change_array function.

    The overall objective of this program is to allow users to input 5 different numbers and be stored into an array. Then what the change_array function does is to double (multiply by 2) any numbers that are below 10, however, it is currently not doing what it is intended to do. I'm really stuck, so I was wondering if anyone can point out my mistakes. I'm not asking for an exact answer, I just need some pointers and guidance.

    What is going wrong is that the change_array function is not changing any of the values given by the users. So for example, if the user inputs, "3, 5, 6, 12, 32", the output of my program is still "3, 5, 6, 12, 32". But what I really want is, "6, 10, 12, 12, 32" after the arrays are passed back from the change_array function.

    EDITED with complete program:

    #include <stdio.h>
    #define SIZE 5
    void fill_array(double x[], int s);
    void change_array(double x[], int s);
    void print_array(double x[], int s);
    
    main()
    {
        double x[SIZE];
    
        fill_array(x, SIZE);
        printf("The array is as: \n");
        print_array(x, SIZE);
        change_array(x, SIZE);
        printf("After change, the array is: \n");
        print_array(x, SIZE);
    }
    
    void fill_array(double x[], int s)
    {
        int i=0;
    
        printf("Please input 5 Non-Negative double numbers\n");
        for (i=0; i<s; i++) {
            printf("Number %d: ", i+1);
            scanf("%d", &x[i]);
            printf("\n");
        }
    }
    
    void change_array(double x[], int s)
    {
        int i=0;
    
        for (i=0; i<s; i++) {
            if (x[i] < 10) {
                  (x[i] = (x[i] * 2));
            }
        }
    }
    
    void print_array(double x[], int s)
    {
        int i=0;
    
        for (i=0; i<s; i++) {
            printf("%ld \t", x[i]);
        }
        printf("\n");
    }
    

    My code is written in C.