Home > Mobile >  How to add other elements to an array[size] (index 0 is already set)
How to add other elements to an array[size] (index 0 is already set)

Time:11-13

im really new to programming, c is my first language

i want to create an array with a size set by scanf it should start with startValue (second scanf) and then printf the rest of the array but with startvalue 1

so for example if i type in:
3 // --> size of the array
4 // --> my starting point/startValue

the programm should give me: 4 5 6

after several hours of watching videos/reading online and trial and error i just dont know how to solve this problem :( i really appreciate your help thank you

#include <stdio.h>

int main() {
    int size;
    int startValue;
    scanf("%d\n %d", &size, &startValue);

    int array[size];
    array[0] = startValue;

    for(int i = 0; i<size; i  ) {
        printf("%d\n", array[i]);
    }

    return 0;
}

CodePudding user response:

As you want desired output: 4 5 6 for this you need to assign value to the rest of elements also as did with

array[0] = startValue;

I've tried to assign it the way you wanted, may be this would help you to understand

#include <stdio.h>

int main() {
    int size;
    int startValue;
    scanf("%d\n %d", &size, &startValue);

    int array[size];

    for(int i = 0; i<size; i  )
    {
        array[i]= startValue;
        printf("%d ", array[i]);
        startValue=startValue 1;
    }

    return 0;
}

I'm assigning values to array[0] & array[1] through this

array[i]= startValue;

and incrementing the value of startValue by 1 through this

startValue=startValue 1;

line

  • Related