The program I am working on requires the user to enter an integer value and then based on that value print an array of zero's. I have been able to do this when the size of the array is preset however when attempting to do it on a user input I get compiler errors. I have tried researching on a fix however I have come upon nothing since most of the fixes use dynamic memory allocation
which we have not yet taught how to use. This is the code I have come up with so far.
In my attempts at finding out why I get errors is that I found if I don't initialise the array with 0
the array is printed to the size the user inputs however of course the array output numbers are completely random.
When I have the size of the array initialised I get the error variable-sized object may not be initialized
with a small arrow pointing to the j
within the square brackets in int j, ar[j]={20};
.
Is there any other way that doesn't use dynamic memory allocation
?
#include <stdio.h>
int main(void){
int i;
int j, ar[j]={0};
printf("Please enter the value:");
scanf("%d",&j);
for(i=0; i<j;i++){
printf("%i\n",ar[j]);
}