Dear All,

I'm new to C, and would appreciate your help about the below code.
The purpose of the program is to multiply the elements of the array and print the result. The user enters the value for how many elements he wants to have multiplied, and then the function should calculate.
For example, if the user enters 3, the result should be 1*2*3=6. For 4, the result should be 1*2*3*4=24. But, when I run the program, I always get very big and false numbers.

#include <stdio.h>

int
multiply_array_elements(const int ar1[], int n, int multiplication);

int
main(void)
{
    const int ar1[] = {1, 2, 3, 4, 5};
    int n, multiplication;
    
    printf("Please enter the number of the elements you want to have multiplied\n");
    scanf("%d", &n);
      
    printf("Multiplication of the first %d elements of the array is %d\n", n, multiply_array_elements(ar1, n, multiplication));
    
    system("pause");
    return(0);
    
}

int
multiply_array_elements(const int ar1[], int n, int multiplication)
{    
    int i;
    
    for(i = 0; i < n; ++i){
        multiplication *= ar1[i];
    }
    
    return(multiplication);
}

Recommended Answers

All 3 Replies

Dear All,

I'm new to C, and would appreciate your help about the below code.
The purpose of the program is to multiply the elements of the array and print the result. The user enters the value for how many elements he wants to have multiplied, and then the function should calculate.
For example, if the user enters 3, the result should be 1*2*3=6. For 4, the result should be 1*2*3*4=24. But, when I run the program, I always get very big and false numbers.

#include <stdio.h>

int
multiply_array_elements(const int ar1[], int n, int multiplication);

int
main(void)
{
    const int ar1[] = {1, 2, 3, 4, 5};
    int n, multiplication;
    
    printf("Please enter the number of the elements you want to have multiplied\n");
    scanf("%d", &n);
      
    printf("Multiplication of the first %d elements of the array is %d\n", n, multiply_array_elements(ar1, n, multiplication));
    
    system("pause");
    return(0);
    
}

int
multiply_array_elements(const int ar1[], int n, int multiplication)
{    
    int i;
    
    for(i = 0; i < n; ++i){
        multiplication *= ar1[i];
    }
    
    return(multiplication);
}

From what I see you have not initialized the variable "multiplication".

In short...
In your main function you never assigned a value to multiplication but you try to show its value in line 15

printf("Multiplication of the first %d elements of the array is %d\n", n, multiply_array_elements(ar1, n, multiplication));

It won't work since variable multiplication has no value

But, when I run the program, I always get very big and false numbers.

when you pass multiplication to the multiply_array_elements function what do you think is its first value?

Many thanks for the replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.