Thread: long problem

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    long problem

    I have this problem i solved from a book. When i try to compile it using turbo C i get output #1 , while with MVC++ i get output #2. Obviously the first output has a problem. has turbo C has a long variable problem or did i just do something wrong here?

    Output #1
    (1) Total: 0
    (2) Total: 0
    (3) Total: 0
    (4) Total: 0
    (5) Total: 0
    (6) Total: 0
    (7) Total: 0
    (8) Total: 0
    (9) Total: 0
    (10) Total: 0
    (11) Total: 0
    (12) Total: 0

    Output #2
    (1) Total: 0
    (2) Total: 1051
    (3) Total: 2024
    (4) Total: 3027
    (5) Total: 4018
    (6) Total: 4869
    (7) Total: 6018
    (8) Total: 5007
    (9) Total: 3968
    (10) Total: 2979
    (11) Total: 2012
    (12) Total: 1027



    PHP Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>

    #define TOTAL_ROLL 36000

    int main()
    {
        
    long result[12] = { };
        
    long rollnum 0ipos;
        
    int dice1dice2ires;

        
    clrscr(); /* removed in mvc++ */
        
    srandtime) );

        for( 
    rollnum 0rollnum != TOTAL_ROLLrollnum++ ){
            
    dice1 = ( rand() % ) + 1;
            
    dice2 = ( rand() % ) + 1;
            
    ires dice1 dice2;
            
    result[ires-1]++;
        }

        for( 
    ipos 0ipos != 12ipos++ ){
            
    printf"(%d) Total: %d\n"ipos+1result[ipos] );
        }

        
    getch(); /* removed in mvc++ */
        
    return 0;


  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    What Microsoft Visual are you using there, is it 6.0, 2003 or 2005?

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Im using 6.0

  4. #4
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    result array holds junk

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    What do you mean it holds junk?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    This is better code:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define	NITER	36000
    #define	NRES	12
    
    int main(void)
    {
    	long	result[NRES];
    	size_t	i;
    	
    	srand(time(0));
    	memset(result, 0, sizeof(result));
    
    	for (i = 0; i < NITER; i++)
    		result[rand() % NRES]++;
    
    	for(i = 0; i < NRES; i++)
    		printf("(%d) Total: %d\n", i, result[i]);
    
    	return (EXIT_SUCCESS);
    }

  7. #7
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    when I say it holds junk, i mean exactly what I said.
    uninitialized variables hold junk. Read your C book.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by valenok
    This is better code:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define	NITER	36000
    #define	NRES	12
    
    int main(void)
    {
    	long	result[NRES];
    	size_t	i;
    	
    	srand(time(0));
    	memset(result, 0, sizeof(result));
    
    	for (i = 0; i < NITER; i++)
    		result[rand() % NRES]++;
    
    	for(i = 0; i < NRES; i++)
    		printf("(%d) Total: %d\n", i, result[i]);
    
    	return (EXIT_SUCCESS);
    }

    This doesnt output the desired output though it works.

    The problem is though. I have 2 dice. A dice has 1 to 6 value.
    The value of the result of the 2 dice should be added. So there should be no incrementation on index 1 and 2 coz the lowest value you could get from 1 dice is 1, so 1 + 1 = 2. Your code increments even those indexes. Value 7 has the highest result rate.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    result[12] is out of bounds. increase the array size.
    if you want to duplicate the logic, do:
    Code:
    result[(rand() % 6) + (rand() % 6) + 2]++;

  10. #10
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    yeah. printf modifier for long is %ld, so use
    Code:
    printf("(%d) Total: %ld\n", i, result[i]);

  11. #11
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by valenok
    result[12] is out of bounds. increase the array size.
    if you want to duplicate the logic, do:
    Code:
    result[(rand() % 6) + (rand() % 6) + 2]++;

    That was a lot lesser code than mine and it works perfect.
    So what do you think i did wrong with mine though?

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Just the ipos declaration. Stupid turbo C

  13. #13
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    1. you did not initialize result array properly
    2. you wrote behind array bounds (result[12])
    3. you printed results using %d, not %ld, that's why it did not work with Turbo C.

  14. #14
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by valenok
    1. you did not initialize result array properly
    2. you wrote behind array bounds (result[12])
    3. you printed results using %d, not %ld, that's why it did not work with Turbo C.
    1. i didnt write out of bounds. the dice result would only generate 2 to 12 minus 1. Thats just enough for the array. There is no way i could have written out of bounds

    2. Whats the proper way to initialize an array?

    3. Yeah i forgot about that.

    I redeclared ipos as int for some stupid reason it worked.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Quote Originally Posted by loko
    1. i didnt write out of bounds. the dice result would only generate 2 to 12 minus 1. Thats just enough for the array. There is no way i could have written out of bounds
    yes you did. maximum index you had was 12.
    for array of size 12 maximum index is 11.

    2. Whats the proper way to initialize an array?
    either declare it static, which implicitely zeroes the array,
    or call memset().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  5. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM