Thread: conflicting types error question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    4

    conflicting types error question

    I tried to make a Gaussian code, but the compiler gave conflicting types error. I cannot figure out why this happen.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      int i;
      double kk;
    
      srand(time(NULL));
      i = rand();
      kk = myGaussian(i);
      printf ("Your random number is %f\n", kk);
      return(0);
    }
    
    double myGaussian(int x){
        double random_max = 32767; //max int that generate by rand
        double half_random_max = 32767/2;
        double varianece_value = 89481216;  //vairance from 0:32767
        double std_value = 9459.451;    //std from 0:32767
        double temp;
    
        temp = ((1/sqrt(2*pi))*(1/std_value))*exp(-(x-half_random_max)^2/(2*varianece_value));
        return temp;
    
    }

    ==============================
    Except the XOR problem, the code still has the conflicting type error. Please help
    Last edited by king2004s; 01-11-2012 at 10:17 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Just skimming your code, so I could be totally wrong, but I imagine you're not meaning to use the XOR operation in line 24.

    Just in case you don't know what I mean:
    Code:
    temp = ((1/sqrt(2*pi))*(1/std_value))*exp(-(x-half_random_max)^2/(2*varianece_value));
    If you're trying to raise this value to a power of two, you can't do it like this because '^' does the XOR operation.
    Last edited by failure67; 01-11-2012 at 10:06 PM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    4
    Quote Originally Posted by failure67 View Post
    Just skimming your code, so I could be totally wrong, but I imagine you're not meaning to use the XOR operation in line 24.
    Why do you say that I use XOR? I am new, so that I may make stupid mistake.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Look at my updated post. You should use a function like pow for what you're trying to accomplish.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    4
    Quote Originally Posted by failure67 View Post
    Look at my updated post. You should use a function like pow for what you're trying to accomplish.
    Thanks. I used MATLAB for too long and thought that "^" also mean "to the power" in C.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you are trying to implement normal distribution - simplest way is to take the sum of 6 rands
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't paraphrase the compiler error messages, copy and paste the exact error messages here.

    I don't see pi defined anywhere.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Couldn't hurt to prototype your myGaussian function so the compiler knows the expected argument/return types. That could be contributing to a "conflicting types" message. Also, your compiler will spit out a line number along with the error message so you know where to look... might be helpful if you told us that little tidbit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    4
    Quote Originally Posted by iMalc View Post
    Don't paraphrase the compiler error messages, copy and paste the exact error messages here.

    I don't see pi defined anywhere.
    Here is the updated code and the error


    C:\Documents and Settings\dcl\Desktop\cprogramming\test1.c|17|error C2371: 'myGaussian' : redefinition; different basic types|
    ||=== Build finished: 1 errors, 0 warnings ===|




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      int i;
      double kk;
    
      srand(time(NULL));
      i = rand();
      kk = myGaussian(i);
      printf ("Your random number is %f\n", kk);
      return(0);
    }
    
    double myGaussian(int x){
        double random_max = 32767; //max int that generate by rand
        double half_random_max = 32767/2;
        double varianece_value = 89481216;  //vairance from 0:32767
        double std_value = 9459.451;    //std from 0:32767
        double temp;
        double pi = 3.14;
    
        temp = ((1/sqrt(2*pi))*(1/std_value))*exp(-(x-half_random_max)*(x-half_random_max)/(2*varianece_value));
        return temp;
    
    }

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Include math.h.
    Use M_PI for pi.
    Use RAND_MAX for the maximum rand value (it may not be what you assume it is).
    Put a prototype of myGaussian before main (or move the whole function there).

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Like hk_mp5kpdw said, you need to prototype your myGaussian function. Put the following line above main:
    Code:
    double myGaussian(int x);
    In C, the compiler reads top-down, and doesn't know about anything it hasn't seen yet. You call myGaussian on line 12, but the compiler hasn't seen the function yet, so it doesn't know what parameters it should take and what it should return, so it assumes it returns an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting types error
    By mastrxplodr in forum C Programming
    Replies: 4
    Last Post: 04-12-2011, 11:56 AM
  2. error: conflicting types for 'sbp_pos_lookup_1'
    By gerger in forum C Programming
    Replies: 7
    Last Post: 10-05-2010, 11:44 AM
  3. Error! Conflicting Types
    By davewang in forum C Programming
    Replies: 5
    Last Post: 12-05-2008, 08:47 AM
  4. error: conflicting types
    By Rodman in forum C Programming
    Replies: 5
    Last Post: 03-10-2006, 04:20 AM
  5. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM