Thread: Help Integer division by zero

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Help Integer division by zero

    I am trying to wirte a program that enciphers and deciphers files of any type.

    the first thing i wanted to do was create a linear congruential generator.
    Code:
    unsigned random(){
    int a=214013,c=2531011,m=4294967296;
    x=(a*x+c)%m;
    return x;
    }
    then i want to Use a global static variable to keep the previous X. Also, write a function to initialize the seed (which is the first X)
    Code:
    void startRandom(int seed)
    {
    
    
    x = seed;
    
    
    }
    then i wanted to print to see if it works

    Here is my full code
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    
    static int x;
    
    
    unsigned random()
    {
       int a=214013,c=2531011,m=4294967296;
       x=(a*x+c)%m;
       return x;
     }
     
    void startRandom(int seed)
    {
    
    
     x = seed;
    
    
    }
    
    
    int main(int argc,char*argv[])
    {
       
        int seed = 5;
        for(int i = 0; i<5;i++)
        {
            startRandom(seed);
            printf((const char*)random());
        
        }
        system("pause");
        return 0;
    }
    i am constantly getting "Unhandled exception at 0x00193D11 in Project8.exe: 0xC0000094: Integer division by zero." I am not able to continue with my program because of it. i could really use some help. I am not really good with code so maybe there is something simple that i am doing wrong that why i keep getting this error?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should be compiling with maximum warnings. This is what my compiler reported:

    Code:
    $ gcc main.c -Wall -Wextra
    main.c:9:10: error: conflicting types for ‘random’
    /usr/include/stdlib.h:327:17: note: previous declaration of ‘random’ was here
    main.c: In function ‘random’:
    main.c:11:4: warning: overflow in implicit constant conversion [-Woverflow]
    main.c: In function ‘main’:
    main.c:33:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
    main.c:33:9: warning: format not a string literal and no format arguments [-Wformat-security]
    main.c:26:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
    main.c:26:24: warning: unused parameter ‘argv’ [-Wunused-parameter]


    Code:
    static int x;
     
     
    unsigned random()
    {
       int a=214013,c=2531011,m=4294967296;
       x=(a*x+c)%m;
       return x;
     }
    Be sure your types match. "x" is an int, but you're returning it as an unsigned.

    Code:
    m=4294967296
    The maximum value for a 32-bit int is 2147483647. You probably intended to use an unsigned int, but even so, the maximum value of a 32-bit unsigned int is 4294967295. The value of "m" is one greater than this, so it looks like you're experiencing roll-over, which is causing the division by zero.

    Either use a larger type (i.e. long), or UINT_MAX (from "limits.h").

    Code:
    printf((const char*)random());
    This is not how to print the result. I'm guessing you're trying to cast the return value of "random()" as to print it as a string, but it does not work this way. Just use the appropriate format specifier for the type.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    Thank you very much that solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Short) Integer Division into float variable
    By nigelmercier in forum C Programming
    Replies: 5
    Last Post: 12-18-2015, 05:22 PM
  2. Replies: 12
    Last Post: 01-13-2014, 06:32 PM
  3. Parsing an Integer with modulus and division
    By HoldenR in forum C Programming
    Replies: 6
    Last Post: 10-24-2012, 12:24 PM
  4. Replies: 5
    Last Post: 10-14-2009, 05:47 AM
  5. overloading the division operator with Integer Class
    By silk.odyssey in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2004, 06:59 PM

Tags for this Thread