I leave you here the code of a vector that I have made but I have no idea why it does not do what it should.
I have put a variable called cantidad
, and a vector[cantidad]
so that the user can manage the values of the array but it only enters up to 6 values.
For example: How many values do you want in the array?: 7 , then enter up to 6...
#include <stdio.h>
int main()
{
printf("ARRAY VARIABLE\n\n");
int cantidad;
int vector[cantidad];
printf("Introduzca la cantidad de valores del array: ");
scanf("%d",&cantidad);
for(int i=0;i<cantidad;i++)
{
printf("%d valor del array: ",i+1);
scanf("%d",&vector[i]);
}
printf("\nValores introducidos\n\n");
for(int i=0;i<cantidad;i++)
{
printf(" %d",vector[i]);
}
return 0;
}
Notice that in those two lines of code... you are defining the vector. and the size. But you are never defining how much quantity is worth. And what you define later does not solve the problem, because vector is already sized. Therefore, you must define vector, after knowing how much quantity is worth. And it lets you enter 6 or 7 or 9 miraculously.
that is, your code should go like this: