Thread: Need to be pointed in right direction

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Need to be pointed in right direction

    This what I'm suppoesed to be doing. Allocate enough memory to hold 50 integers using malloc and assign the pointer returned by malloc to "ia". Using only "ip" populate the array with fifty random integers. Using only "ip" display the contents of the array both forward and backard. The function randnum was given. I'm not getting 50 random integers. Could someone tell me the problem area(s) where I should be looking at?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int randnum(int range);
    
    int main () {
    
        int    *ia, *ip;
        
        srand(time(NULL));
        ia = malloc (50 * sizeof(int));
            
        if (ia == NULL) {
             
             printf("Cannot malloc\n");
             exit(1);
        }
            
        for (ip = ia; ip < ia + 50; ip++)
               *ip=randnum(50);
               printf("%-3d", *ip);
    
        return 0;
    }
    
    int randnum(int range) {
    
        int    v;
        v = rand();
        return (v * range / (RAND_MAX + 1));
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're trying to have a range of zero to fifty or something (the 'range' parameter), why are you doing it the hard way?
    Code:
    int randnum( int range )
    {
        return rand() % range;
    }
    Oh, the problem though is that you are only printing one number. You're not printing every time through the loop. Try surrounding those two lines with a pare of braces.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well using division by RAND_MAX does produce better result than mod. But of course this requires that you actually do the division correctly.

    Code:
    int randnum(int range) {
    
        int    v;
        v = rand();
        return (v * range / (RAND_MAX + 1));
    
    }
    Lets look at this
    v = int
    range = int
    RAND_MAX = int
    1 = int

    So you are only doing integer division.
    Now there are some other major problems

    v * range can easily overflow the range an int can hold,
    RAND_MAX + 1 could very likely overflow an int

    Prelude has posted a very nice C++ random function that I won't try to reproduce here. Go search for it.

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You can find Prelude's article post about random functions here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats not what I was refering to. Actually I realized after I posted this is the C forum and she posted it on the C++ forum using C++.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I was having trouble searching for Prelude's post in the C forum, lol

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I have just one more question. I need to print the array forward and backwards. I think I have it down, but I want to add a couple of newlines between printing the whole array forwards and then backwards. I don't want columns.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int randnum(int range);
    
    void main (void) {
    
        int    *ia, *ip;
        
        srand(time(NULL));
        ia = malloc (50 * sizeof(int));
                
        if (ia == NULL) {
               
             printf("Cannot malloc\n");
             exit(1);
        }
            
        for (ip = ia; ip < ia + 50; ip++) {
            
               *ip=randnum(1000);
               printf("%-4d", *ip);
               
               if (ia <= ip) {
                
                    printf("%-4d", *ip);
               }
        }
         
        printf("\n");
        free(ia);
    
    }
    
    int randnum(int range) {
    
        int    v;
        v = rand();
        return (v * range / RAND_MAX + 1);
    
    }

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int randnum(int range);
    
    void main (void) {
    
        int    *ia, *ip;
        
        srand(time(NULL));
        ia = (int*)malloc (50 * sizeof(int));
                
        if (ia == NULL) {
               
             printf("Cannot malloc\n");
             exit(1);
        }
            
        for (ip = ia; ip < ia + 50; ip++) {
            
               *ip=randnum(1000);
               printf("%-4d", *ip);
        }
    	
    	printf("\n\n\n"); //newlines
    	
    	//array backwards
    	for(ip--;ip > ia; ip--) {
    		printf("%-4d",*ip);
    	}
         
        printf("\n");
        free(ia);
    
    }
    
    int randnum(int range) {
    
        int    v;
        v = rand();
        return (v * range / RAND_MAX + 1);
    
    }
    What exactly do you mean by you don't want columns. If you don't want to format the output then remove the -4d format specifier and leave only:
    Code:
    printf("%d", *ip);
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adreess of pointed structure element
    By limp in forum C Programming
    Replies: 1
    Last Post: 06-12-2009, 05:54 AM
  2. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  3. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM
  4. Finding the direction to another object
    By Person Man in forum Game Programming
    Replies: 9
    Last Post: 12-05-2001, 02:56 PM
  5. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 06:32 PM