0
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int length;
    do{
        length = get_int("Length: ");
    }
    while (length < 1);

    int twice[length];

    twice[0] = 1;
    printf("%i\n", twice[0]);

    for (int i=1; i < length; i++)
    {
        twice[i] = 2*twice[i-1];
        printf("%i\n", twice[i]);
    }
}

The goal of this code is this: User will prompt a value which will be the range of the array and array will start from 1 and multiple the previous value and assign it to the next array. Let's say user promted 5, the output will be:

Length: 5
1
2
4
8
16

This code works fine but when i change int i = 1 to int i = 0 i get this result:

Length: 5
1
43864
87728
175456
350912
701824

Why do I get those numbers and even though i didn't assign them?

I expect to see where those numbers come from.

ayanova
  • 3
  • 3

0 Answers0