I'm having problems compiling a program in C using the function drand48(). I wanted to know if and how I can fix this issue.
I've written a program in C which should generate random numbers and confront them with 5 input numbers. I wanted to use drand48() (because it's the function our professor wants us to use during our exam) but my ide (Dev C++ 6.3.0) keep telling me:
"[Warning] implicit declaration of function 'srand48'; did you mean 'srand'? [-Wimplicit-function-declaration]"
even though I've included "stdlib.h". I have tried to do the same on many other ide (from Eclipse to CodeBlocks) and they all print the same error and don't compile the program.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
srand48(1102003);
int i, num[5], n, count=0;
double random[100], ran, t;
//Input
printf("Digit 5 numbers\n");
for(i=0; i<5; i++) {
scanf("%d", &num[i]);
}
//Sequence Generation
for (n=0; n<100; n++) {
ran=drand48();
random[n]=round(ran*100);
}
//Value check
for(n=0; n<100; n++) {
for (i=0; i<5; i++){
if (num[i]==random[n]) {
count+=1;
}
}
}
printf("You guessed %d numbers\n", count);
system("PAUSE");
}
Here is a workaround:
At the too of your projekt (after includes) add the following lines.
#if !(_SVID_SOURCE || _XOPEN_SOURCE)
double drand48(void) {
return rand() / (RAND_MAX + 1.0);
}
long int lrand48(void) {
return rand();
}
long int mrand48(void) {
return rand() > RAND_MAX / 2 ? rand() : -rand();
}
void srand48(long int seedval) {
srand(seedval);
}
#endif
These will not provide the same values as the rand48 functions (for the same seed), but will behave the same.
From the linux man page:
These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead.
So you may always want to use rand() instead.
Related
I am trying to make a program that generates a random number (this part works), and then prints something depending on whether the number is greater or less than 555,555,555. While the correct thing prints if the number is over 555,555,555, nothing at all prints when the number is below 555,555,555.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int rand();
time_t secondsFromEpoch = time(NULL);
srand(secondsFromEpoch);
printf("%d\n", rand());
if (rand() > 555555555) {
printf("hello");
}
else if (rand() < 555555555) {
printf("goodbye");
}
return 0;
}
You're calling rand multiple times. Each time you call it, it returns a different number.
Save the random number in a variable and use that.
int r = rand();
printf("%d\n", r);
if (r > 555555555) {
printf("hello");
}
else if (r < 555555555) {
printf("goodbye");
}
Here is my factorial program—this is executing and giving a correct result:
#include <stdio.h>
int main()
{
int n;
printf("enter the no=");
scanf("%d", &n);
fun(n);
printf("%d\n", fun(n));
return 0;
}
int fun(int n)
{
if(n == 0)
return 1;
else
return fun(n - 1) * n;
}
This is my program to compute the power of a number—this is giving 0 instead of the correct result and yet is almost identical:
#include <stdio.h>
int main()
{
int m, n;
printf("enter the no=");
scanf("%d%d", &m, &n);
pow(m, n);
printf("%d\n", pow(m, n));
return 0;
}
int pow(int m, int n)
{
if(n == 0)
return 1;
else
return pow(m, n - 1) * m;
}
Both are running on same compiler.
Why is my factorial program working but my almost identical power program is not working?
A few issues are present here. First and foremost, you didn't declare a prototype for your function before calling it the first time. To do so, you need to place int pow(int, int); above main. This lets the compiler know exactly what your function expects and what it returns.
Ordinarily, this wouldn't cause the behavior you're seeing (though it is bad practice), but there's also already a function named pow in the C library. Since you never gave it a definition of your own, it's being implicitly included in your code. Now, it's expecting you to put two doubles in and get a double out.
Add the prototype at the top and rename your function, and you'll fix both of these issues at once.
Demo
(Also, for what it's worth, you've got an unnecessary call.)
#include <stdio.h>
int powr(int, int); // helps avoid compiler warnings
int main()
{
int m, n;
printf("enter the no=");
scanf("%d%d", &m, &n);
powr(m, n); // unnecessary
printf("%d\n", powr(m, n));
return 0;
}
int powr(int m, int n)
{
if(n == 0)
return 1;
else
return powr(m, n - 1) * m;
}
For a little backstory behind this: The GNU C compiler (presumably what you're using) has implicit declarations for most of the Standard C Library functions that can be optimized in target-specific ways. pow is one of them.
To fix this, you should rename your pow function to something not reserved by the Standard C library and provide a prototype for it, like so:
#include <stdio.h>
int power(int m, int n);
If you compile in strict C89/C90 compliance mode, you don't even need to provide a prototype due to implicit function declaration rules. However, if you compile with any other standard (which is the default and highly recommended), you'll need to provide a prototype for that function, as shown above.
I'd also like to note that you have an unnecessary call to your power-computing program (also present in the factorial-computing program):
scanf("%d%d", &m, &n);
power(m, n); // here
printf("%d\n", power(m, n));
I am new C programmer and I am working on a school project where I have to approximate the value or pi. My professor stated that we have to declare all integer terms using long double. The console shows me asking the user for the number of terms to approximate pi given a function. I enter 1 for the number of terms however the code returns -0.00000000 instead of 4.00000000.
#include <stdio.h>
#include <math.h>
long double approx1(int terms)
{
long double pi = 0;
long double num = 4;
long double denom = 1;
int i;
for(i=1; i <= terms; i++)
{
if(i%2 != 0)
{
pi=pi+(num/denom);
}
else
{
pi=pi-(num/denom);
}
denom = denom + 2;
}
printf("%.8Lf\n", pi);
}
int main()
{
int terms;
long double pie;
printf("input number of terms, input 0 to cancel\n");
scanf("%d", &terms);
while(terms != 0)
{
if(terms > 0)
{
pie = approx1(terms);
printf("%.8Lf\n", pie);
printf("GG mate\n");
break;
}
else
{
printf("Incorrect input, please enter a correct input\n");
scanf("%d", &terms);
}
}
}
I haven't had any success in getting it to work( it works with float though). What am I doing wrong? (I am using Code Blocks with the included compiler btw.)
You forgot to add a return statement in your approx1() function. Withoout that return statement, if you make use of the returned value, it invokes undefined behavior.
Quoting C11 standard, chapter §6.9.1
If the } that terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined.
Ok, so as a beginner programmer, I have been tasked with creating a simple math quiz program. It is supposed to prompt the user for how many questions to ask, congratulate or inform the user when their answer is either right or wrong. And then print out the number correct and the number incorrect at the end of the program. I have done all of this successfully, the only issue with my code now is that it asks the same questions over and over. I'm at a loss here so any help would be appreciated, thanks.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i;
int response;
int correctAnswers = 0;
int incorrectAnswers = 0;
printf("\nMath Quiz\n");
printf("Please enter # of problems you would wish to try:");
scanf("%d", &response);
if(response == 0)
{
printf("\nThanks for playing!\n");
return 0;
}
for(i=0; i<response; i++)
{
int answer = 0;
int a = rand() % 12;
int b = rand() % 12;
printf("\n%d * %d = ",a ,b);
scanf("%d", &answer);
if((a * b) == answer){
printf("\nCongratulations You are correct!\n");
correctAnswers++;
}
else{
printf("Sorry you were incorrect!\n");
incorrectAnswers++;
}
}
printf("\n\nYour Results:\n\n\n");
printf("Number Incorrect: %d\n", incorrectAnswers);
printf("Number Correct: %d\n", correctAnswers);
if(correctAnswers > incorrectAnswers){
printf("You Passed!\nGood work!\n\n");
}
else{
printf("You did not pass!\nYou need more work!\n\n");
}
return 0;
}
Additionally, any critiques as far as formatting are more than welcome. Thanks!
You need to understand how the randon number generator works in C.
rand() generates only pseudorandom numbers. This means that every time you run your code you will get exactly the same sequence of numbers.
Use the srand function to generate random numbers based upon a source number. If you want one that changes often, use the system time.
srand(time(NULL));
Also include the header file time.h to use the time function.
Call that function before any calls to rand(). If you don't call srand() before a call to rand() in your program, it is as if srand(1) was called: the seed value will be 1 at every execution of the program and the generated sequence will be always the same.
Use this srand in your code, like this...
int a;
int b;
srand(time(0));
a = rand() % 12;
b = rand() % 12;
I've been working on a project euler problem, which by their very nature coerce you to use data types with big storage.
#include <stdio.h>
#include <conio.h>
#define num 600851475143
int main()
{
long long i, j, count=0, number=num, k;
for(i=2;number!=1;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if((i%j)==0)
{
count++;
}
}
for(k=0;k<100000000;k++)
{}
if(count==2)
{
printf(" %d\n", i);
if(number%i==0)
{
number/=i;
printf(" %d\n", number);
printf("%d\n", i);
i=2;
}
}
}
getch();
return 0;
}
When I compile and run the program, there is nothing printed for number. I have tried various printf conversions %ll, %l, I have changed data types. I am using GNU GCC compiler. What should I do?
You should (re)read the documentation, I guess.
%ll didn't work since ll is not a complete specifier, it's just a modifier for the actual conversion specifier, which should follow.
Try %lld.
The correct format for printf is %lld. Moreover you should use a prefix for your constant num, because this integer constant is too large to be hold in long type.
#define num 600851475143LL
Perhaps should you avoid lower-case macro's identifiers?