954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A Problem With Multiplication Of Array Elements

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);
}
c_learner
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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".

dheaven
Newbie Poster
12 posts since Jan 2012
Reputation Points: 22
Solved Threads: 2
 

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 valueBut, 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?

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Many thanks for the replies.

c_learner
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: