Thread: Help with this code please!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    16

    Help with this code please!

    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
    Last edited by zetaXX; 07-23-2013 at 06:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM