0

For the question, "Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i", my code in C is: "The Code simply won't execute!" What do you think is the problem here?

int main()
{
    int no, i,j, product = 1;
    int* ar1 = (int*)malloc(sizeof(int) * no);
    int* ar2 = (int*)malloc(sizeof(int) * no);
    printf("Enter the number of elements in array: \t");
    scanf("%d", &no);

    printf("Enter the elements: \n");
    for(i=0; i<no; i++){
    scanf(" %d", &ar1[i]);
    }

    for(i=0; i<no; i++){
        for(j=0; j<no; j++){
            while(j!=i){
                product=ar1[i]*product;
            }
        }
        ar2[i]=product;
    }
    for(i=0; i<no; i++){
            printf("The output array is: \n");
        printf("%d", ar1[i]);
    }
return 0;

}

Jupiter
  • 19
  • 5
  • 5
    `no` is unitialized when you use it in malloc. – Eraklon Mar 19 '20 at 18:19
  • 2
    `while(j!=i)` is an infinite loop (if it runs at all) and should be `if(j!=i)`. Note too that `int product` can overflow, I suggest using `long long` and follow that through. Also `product` is initialised to `1` only once, perhaps you intended to initialise it before the inner loop. – Weather Vane Mar 19 '20 at 18:29
  • Similar to [this quesion](https://stackoverflow.com/questions/60762597/im-just-trying-to-scan-strings-into-an-array-what-am-i-doing-wrong). The C language does things in the order that you specify. You need to get the order right. – user3386109 Mar 19 '20 at 18:35

3 Answers3

2

These declarations

int no, i,j, product = 1;
int* ar1 = (int*)malloc(sizeof(int) * no);
int* ar2 = (int*)malloc(sizeof(int) * no);

invoke undefined behavior because the variable no is not initialized and has indeterminate value.

At first you should ask the user how many elements in the array he wants and only after that to allocate dynamically arrays or use variable length arrays.

These loops

for(i=0; i<no; i++){
    for(j=0; j<no; j++){
        while(j!=i){
            product=ar1[i]*product;
        }
    }
    ar2[i]=product;
}

are also invalid. For example the inner while loop can be infinite. And the variable product should be initialized in each iteration of the outer loop.

In this loop

for(i=0; i<no; i++){
        printf("The output array is: \n");
    printf("%d", ar1[i]);
}

this call pf printf

    printf("The output array is: \n");

should be outside the loop and in this call of printf

    printf("%d", ar1[i]);

you have to use ar2 instead of ar1.

Also declare variable where they are used. Otherwise it is difficult to understand their meanings when you do not see at once where they are used.

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    printf( "Enter the number of elements in array: " );

    size_t n = 0;

    scanf( "%zu", &n );

    int *a1 = malloc( n * sizeof( int ) );
    long long int *a2 = malloc( n * sizeof( long long int ) );

    printf( "Enter the elements: " );

    for ( size_t i = 0; i < n; i++ )
    {
        scanf( "%d", &a1[i] );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        a2[i] = 1;
        for ( size_t j = 0; j < n; j++ )
        {
            if ( i != j ) a2[i] *= a1[j];
        }
    }

    printf( "The output array is: " );

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%lld ", a2[i] );
    }

    putchar( '\n' );

    free( a1 );
    free( a2 );

    return 0;
}

The program output might look like

Enter the number of elements in array: 10
Enter the elements: 1 2 3 4 5 6 7 8 9 10
The output array is: 3628800 1814400 1209600 907200 725760 604800 518400 453600 403200 362880 

Pay attention to that as a product of elements of an array can be very big then it is better to allocate the second array as having the element type long long int to avoid an overflow.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

There are few errors. I mentioned in the code as comments for clarity. Also checking that malloc return value is not null is not a bad idea.

int main()
{
    int no, i, j, product = 1;
    int* ar1;
    int* ar2;

    printf("Enter the number of elements in array: \t");
    scanf("%d", &no);

    // FIX 1: using malloc after reading in no
    ar1 = malloc(sizeof(int) * no);
    ar2 = malloc(sizeof(int) * no);

    printf("Enter the elements: \n");
    for(i = 0; i < no; i++){
        scanf(" %d", &ar1[i]);
    }
    for(i = 0; i < no; i++) {
        for(j = 0; j < no; j++) {
            if (j != i) { // FIX 2: while caused infinite loop, if needed here
                product = ar1[j] * product; // FIX 3: you need to use j as index
            }
        }
        ar2[i] = product;
        product = 1; // FIX 4: you have to set product to 1 for correctness
    }
    for(i = 0; i < no; i++) {
        printf("The output array is: %d\n", ar2[i]); // FIX 5: you used ar1 here, not ar2
    }

    // FIX 6: freeing the allocated memory
    free(ar1);
    free(ar2);

    return 0;
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29
1

first of all, you should first take no as input then use it.

    printf("Enter the number of elements in array: \t");
    scanf("%d", &no);
    int* ar1 = (int*)malloc(sizeof(int) * no);
    int* ar2 = (int*)malloc(sizeof(int) * no);

second, this loop is an infinitive loop:

     for(i=0; i<no; i++){
        for(j=0; j<no; j++){
            while(j!=i){
                product=ar1[i]*product;
            }
        }
        ar2[i]=product;
    }

when i=0 and j=0 you will enter this loop and j==i so you won't enter while ,but when j become 1 so j!=i you will enter while and it will never end ,because this 1 != 0 is always true.

hanie
  • 1,863
  • 3
  • 9
  • 19