-1
Hello would like to know what would be the ideal method to generate random numbers within a vector in C using the function Rand(), I get an error when I try to run the code below.
follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define vMax 5
main(){
srand(time(NULL));
int vetor[vMax] = rand() % 10 + 1;
for(int i = 0; i < vMax; i++){
printf("%d",vetor[i]);
}
}
the error basically says that the array needs to be initialized with keys but when I put the keys it tmb returns me ANOTHER error, so logically this would not be the way to generate random elements of the vector. Thank you from now on, every comment will be helping me in learning.
it might not be this, but try to put it like this:
int vetor[vMax] = rand() % (10 + 1);
– IanMoone
@Ianmoone Its solution returns error, but thanks for adding to the topic
– rebel_vitor
Note that when you do: int vector[Vmax] = Rand() %10 + 1; you are making an assignment to the Vmax index element, however the valid indices for your vector vary from 0 to Vmax-1. You are accessing a memory position outside the area reserved for your vector.
– anonimo