2

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.

2
  • change_array indeed modifies the array so that the modification are visible to the caller. You need to post the real code, preferably a complete program. Commented Mar 9, 2012 at 20:21
  • OK, the new code makes it clear that the problem is simply the printf format string. Commented Mar 9, 2012 at 20:49

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Since both you and Als approved that it works, then I think it might be my other functions that are causing the unwanted output. I will try to look at my work closely again.
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.

Comments

1

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

2 Comments

Thank you, as I said to TSouthwick, I think it might be my other functions that are causing the unwanted output. I will try to look at my work closely again.
Just posted my entire program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.