-3

so I figured out how to write this using a for loop

int mult_for(int* array, int len) {
    int mult = 1;
    for (int i = 0; i < len; i++) {
        mult *= array[i];
    }
    return mult;
}

but I'm unsure of how to go about doing the same thing using while.

int mult_while(int* array, int len) {
    int mult = 1; 
    while (int i < len){
    mult *= array[i]
    }
    return 0;
}

I currently have this but am unsure of how to continue from there. The syntax of c is still new, so that doesn't help either.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
balbat
  • 1
  • 1

4 Answers4

1

For loop uses for(initialization; condition; increment/decrement) { body } syntax, which is a common rule for most languages.

The aforementioned three phases (initialization, condition and increment/decrement) are present in every variation of loop, and for a while loop they follow this order:

initialization

while(condition)
{ 
  body
  increment/decrement
}

For your case:

    int i = 1; //initialization
    while (i < len) //while(condition)
    {
      mult *= array[i]; //body
      i++; //increment
    }

Note that:

  • Initialization here refers to initializing the variable(s) to be used inside the loop. There can be multiple variables.
  • Increments/Decrements can come before/after the loop body.
  • You can also have a while loop without defining extra loop variables. Such as in your case, if your using the pre-defined len variable in your code, you can use it as a condition to loop till the value becomes zero by using while(len--) { body }, thereby reducing the need for an extra variable and its subsequent increment/decrement statement.
1

It is my first see that int type-definer as a condition over a while loop. Anyway, you need to increase your counter, i.e. i, one by one to iterate the array over. It stops when i == len as alike happened in your for-loop example.

int mult_while(int* array, int len) 
{
    int mult = 1; 
    int i = 0;
    while (i < len) {
        mult *= array[i];
        ++i;
    }
    return mult; // the result must be returned in lieu of zero
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

You can easily do it by continuously decrementing len until it hits 0:

int mult_while(int const* array, int len) {
    int mult = 1;
    while(len--) {
        mult *= *array++;
    }
    return mult;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
-1

For starters the function should be declared like

long long int mult_while( const int *array, size_t len );

that is the return type should be long long int to avoid overflowing. And the pointer should be declared with the qualifier const because the array is not being changed within the function. The size of the array should have the type size_t,

You need not to declare one more local variable apart from the parameters as it was done in the for loop in your first function implementation.

The function can be implemented the following way using the while loop.

long long int mult_while( const int *array, size_t len )
{
    long long int mult = len == 0 ? 0 : 1;

    while ( len-- ) mult *= array[len];

    return mult;
}

Pay attention to that in general the user can pass the length of the array equal to 0. In this case it will be logically consistent to return 0.

Here is a demonstrative program.

#include <stdio.h>
#include <limits.h>

long long int mult_while( const int *array, size_t len )
{
    long long int mult = len == 0 ? 0 : 1;

    while ( len-- ) mult *= array[len];

    return mult;
}

int main(void) 
{
    int a[] = { INT_MAX, INT_MAX };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }

    putchar( '\n' );

    printf( "The product of the array elements is equal to %lld\n", mult_while( a, N  ) );

    return 0;
}

The program output is

2147483647 2147483647 
The product of the array elements is equal to 4611686014132420609

As you can see for such big numbers stored in the array the result is correct.

It is the only function implementation among presented here in answers that is more or less correct.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • `mult = len == 0 ? 0 : 1;` could just be `mult = len != 0;`. – Aykhan Hagverdili Mar 28 '20 at 18:31
  • @Ayxan IThe expressions in essence are equivalent. – Vlad from Moscow Mar 28 '20 at 18:32
  • *“Pay attention to that in general the user can pass the length of the array equal to 0. In this case it will be logically consistent to return 0.”* No, it’s perfectly consistent and common to define the empty product as 1. *“that is the return type should be `long long int` to avoid overflowing”* `long long int` doesn’t avoid overflowing, just moves the point at which it happens. These points don’t help answer the question or help this implementation be any more correct. – Ry- Mar 28 '20 at 20:18
  • @Ry- How to distinguish such a return value from an actual return value when for example the array consists from 1? The return type long long int is the only integer type that should be used as the integer return type. Your comment is senseless. – Vlad from Moscow Mar 28 '20 at 20:20
  • @VladfromMoscow: Who said you needed to distinguish `{}` from `{1}` by the return value of this function? If you define it as 0, you can’t distinguish it from `{0}`. You can’t distinguish `{1}` from `{1, 1}` either way, nor `{15, 20}` from `{25, 12}`. https://en.wikipedia.org/wiki/Empty_product – Ry- Mar 28 '20 at 20:23
  • @Ry- Returning 0 is logically consistent when the passed size is equal to 0.. There is no need to distinguish from 0. It is a nonsense. Returning 1 is just a bad approach. Having the return type int you are even unable to compute the product for an array with two elements as it is shown in ,my demonstrative program/. – Vlad from Moscow Mar 28 '20 at 20:28
  • 1
    `int` is terrible because it overflows when multiplying two `INT_MAX`, whereas `long long int` is good because it doesn’t overflow until… three `INT_MAX`. Hmm. – Ry- Mar 28 '20 at 20:36
  • @Ry- It is the only integer type that should be used as the return integer type. I already said that your opinion is not interesting to me. – Vlad from Moscow Mar 28 '20 at 20:38
  • 1
    You keep saying that, but you haven’t provided very good evidence for it. The OP says their original works, and you don’t know their requirements, so the only reason you should be correcting it is if you have a good case for why the original is wrong – but, as I’ve pointed out, you don’t. (And, of course, you’re free to disagree – I just thought it would be good to explain my vote. Comments also help to avoid misinforming future visitors, so your interest isn’t really required.) – Ry- Mar 28 '20 at 20:41