First of all, sorry about the language of arguments and functions(they are on spanish ). The problem is the following: I need to this function to get a random number between the limits 1 and the number inserted by the user, but I just get a random number that is not between these limits, here is the code.
Code:
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<time.h>
struct Bombo{
int n;
int *v;
};
struct Bombo BomboCrea(int n){
int i = 1;
struct Bombo a;
a.n = n;
a.v = (int *)malloc(sizeof(int)*a.n);
for(i; i<n; i++){
a.v[i] = 1+(rand()%(n-1));
printf("%d,",a.v[i]);
}
return a;
}
void BomboLibera(struct Bombo b){
free(b.v);
}
int BomboExtrae(struct Bombo b){
int a;
int d;
a = b.n;
if(b.v == NULL){
printf("El Bombo esta vacio");
}else{
d = (1+(rand()%(a-1)));
};
return d;
}
int BomboVacio(struct Bombo b){
int a;
if(b.v == NULL){
a = 1;
}else{
a = 0;
}
return a;
}
int main(){
srand(time(NULL));
struct Bombo a;
int b;
int c;
int d;
int i = 0;
printf("Escribe el tamaño del bombo desde 1 a ... \n");
scanf("%d",&b);
BomboCrea(b);
c = BomboExtrae(a);
printf("El numero es %d \n",c);
BomboLibera(a);
d = BomboVacio(a);
system("pause");
return 0;
}
the function BomboCrea works fine, I checked it and this make an array between the 2 limits