Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is the error in my program?

+1 vote
asked Aug 31, 2021 by sapna (130 points)
#ifndef UNIFORM_H
#define UNIFORM_H

double unif();
double unif(double a, double b);
long unif(long a);
void seed();

#endif // UNIFORM_H

#include "uniform.h"
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
double unif()
{
    return rand() / double(RAND_MAX);
}

 double unif(double a, double b)
  {
     return (b-a)*unif() + a;
  }
long unif(long a)
 {
   if (a < 0) a = -a;
   if (a==0) return 0;
   return long(unif()*a) + 1;
}
 void seed()
 {
     srand(time(0));
 }

1 Answer

0 votes
answered Sep 19, 2021 by Peter Minarik (101,420 points)

I don't see any problem with the shared code. Unless that's all of your code. You need to have a main() function in your code: that's the entry point of the program.

uniform.h

#ifndef UNIFORM_H
#define UNIFORM_H

double unif();
double unif(double a, double b);
long unif(long a);
void seed();

#endif // UNIFORM_H

uniform.cpp

#include <cmath>
#include <cstdlib>
#include <ctime>

#include "uniform.h"

using namespace std;

double unif()
{
    return rand() / double(RAND_MAX);
}

double unif(double a, double b)
{
    return (b-a)*unif() + a;
}

long unif(long a)
{
    if (a < 0) a = -a;
    if (a==0) return 0;
    return long(unif()*a) + 1;
}

void seed()
{
    srand(time(0));
}

main.cpp

#include <iostream>

#include "uniform.h"

int main()
{
    seed();
    for (int i = 0; i < 10; i++)
        std::cout << unif() << std::endl;
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...