I'm implementing a basic program which generate 2 random number. The problem is the result of first number looks like it is following some kind of pattern, but the second still looks right.
Output:
6584 679
6587 1427
6591 9410
6594 156
7733 3032
7737 3780
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
srand(time(NULL));
int a = rand()%10001, b= rand()%10001;
printf("%d %d", a,b);
return 0;
}
So what is the problem here and how to fix it.
Any help would be appreciated.
I am using windows 10 64 bits, gcc 8.1.0.
time(NULL) value acts as the same seed value every time you run the program. The reason behind that your CPU will generate a similar starting NULL time for every time. To get rid of this kind of effect you need to play with the seed value, so that even if your computer starts with the same time(NULL) value, it needs to get seed different than the other runs. For that purpose, you can simply do the following:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(){
srand((unsigned)time(NULL) * (unsigned)getpid());
int a = rand()%10001, b= rand()%10001;
printf("%d %d", a,b);
return 0;
}
Full credit to #pmg, thanks for the comments to improve the solution.
Related
I am trying something in C on hp-nonstop(tandem),
As part my task is to wait for sometime.
I try to use the
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("Something");
sleep(5);
printf("Something");
fflush(stdout);
}
}
It's compiling without any problem,
While running it is giving ABENDED: each time different no.
The result calling sleep() from guardian environment is undefined. That might be leading to ABEND that you mentioned. If you want to wait for some time in guardian hp-nonstop environment, you should call DELAY(). It takes centi-seconds as arguments. So if you want to add delay of 5 seconds, you should call it as DELAY (500). You also need to include the header #include<cextdecs(DELAY)>
I'm a novice C programmer and am completely stuck on debugging a program that relies on the CFITSIO package [sorry in advance if I get the terminology wrong below]. I have two fits that I am reading in: 1. "BFmask.fits", 2. "DFmask.fits". These are two files that either have a 1 or a 0 as elements.
These are defined as double precision pointers as below:
double *maskdark,*maskbright;
Memory allocation is as follows:
maskdark=malloc(size*sizeof(double)); //dark mask
maskbright=malloc(size*sizeof(double)); //bright mask
where "size" is a long variable with a value of 490000
THE PROBLEM: When I execute the program, it apparently does not properly read in BFmask.fits or DFmask.fits (i.e. does not properly read in entries for maskbright or maskdark).
HOWEVER, when I declare a third variable called 'mask' (i.e. double *mask;), then the program DOES execute properly.
What is the cause and solution to this problem?
How do I avoid similar ones in the future?
I have checked dozens of times to make sure this variable is not used anywhere in the program. This makes no sense to me.
#include <stdio.h>
//For declaration of open, close
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
//GSL Matrix STuff
#include <gsl/gsl_vector.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_blas.h>
//CFITSIO
#include <fitsio.h>
int main ()
{
long sizetotdm,cmwfsbrightindex,cmwfsdarkindex;
long size,sizex,sizey;
double *maskdark,*maskbright;
//double *mask; //if I uncomment this, then the program works properly somehow
fitsfile *fptr;
float nullval;
int status, anynull;
cmwfsbrightindex=0;
cmwfsdarkindex=0;
sizex=700; sizey=700; size=sizex*sizey;
/* Define Masks for Dark Field and Bright Field */
maskdark=malloc(size*sizeof(double)); //dark mask
maskbright=malloc(size*sizeof(double)); //bright mask
/* Read in Dark and Bright Field Mask files */
sprintf(s_file,"./06_21_19_mask/BFmask.fits");
fits_open_file(&fptr,s_file,READONLY,&status);
fits_read_img(fptr,TDOUBLE,1,size,&nullval,maskbright,&anynull,&status);
sprintf(s_file,"./06_21_19_mask/DFmask.fits");
fits_open_file(&fptr,s_file,READONLY,&status);
fits_read_img(fptr,TDOUBLE,1,size,&nullval,maskdark,&anynull,&status);
for (i=0;i<size; i++)
{
if (*(maskbright+i)==1)
{
cmwfsbrightindex+=1;
}
if (*(maskdark+i)==1)
{
cmwfsdarkindex+=1;
}
}
printf("the number of pixels is %ld %ld \n",cmwfsbrightindex,cmwfsdarkindex);
}
Results should be ...
the number of pixels is 24212 24952 //this happens when I define an unused variable
The results are ...
the number of pixels is 0 0 //this happens when I comment out the unused variable
This code of C generates only multiples of 7 in Xcode:
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
printf("%d", rand()%28);
}
It will also generate only multiples of 7 with multiples of 7 put in place of 28 (rand()%56 for example does the same thing). Any idea what's causing this problem? Thanks a lot for your help!
I'm trying to create a program that needs to get string input from the user, since it's has to work with portuguese words i'm using wchar_t,the problem is,
C seems to have a will, because when i need it to work it doens't but then out of the blue it works in some simple test.
the following code worked 5 minutes ago and now it doesn't:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
/
int main(){
setlocale(LC_ALL,"Portuguese");
wchar_t meu[3];
fgetws(meu,3,stdin);
fputws(meu,stdout);
return 0;
}
I want to check if the user input is purely integer or a float. I attempted to do this by using floor and ceilfand comparing the values to the original x value in a function. However, this seems to be a tad bit problematic as the function keeps returning 0 instead of 1 for certain numbers like 5.5, when floor(5.5)!=5.5 and ceilf(5.5)!=5.5. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x)//in a separate file
{
if (floor(x)==x && ceilf(x)==x)
{
//printf("%f",floor(x));
return 0;
}
else {
return 1;
}
}
int main()
{
char line[] = " +----+----+----+----+----+----+----+----+----+----+---+";
char numbers[] = " 0 5 10 15 20 25 30 35 40 45 50";
float balls,slots;
int slot[9];
printf("==========================================================\nGalton Box Simulation Machine\n==========================================================\n");
printf("Enter the number of balls [5-100]: ");
scanf("%f",& balls);
if (balls>100 || balls<5){
printf("/nInput is not within the range. Please try again.");
}
else if (intchecker(balls)==1){
printf("/nInput is not an integer. Please try again.");
}
else {
printf(" This is an integer.");
//some more code here
}
}
I tried placing just the intchecker code in another project, which seems to work fine without any bugs, unlike in the previous project, where when I used a printf statement to check if the floor(x) value was correct, it kept showing different answers, e.g. "-2.000000" when input was 5.2. This is my code for the second project:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float x;
scanf("%f",&x);
if (floor(x)==x && ceilf(x)==x){
printf("Integer");
return 0;
}
else {
printf("Non-Integer");
return 1;
}
}
How is it possible that the second code works perfectly when the first code does not? Is something wrong with my way of writing/calling the function?(I am relatively new to functions-only 2 weeks of exposure so far)
I searched online and have seen many answers to check if input is integer or float, even on stackoverflow.com itself, but my wish is not to find out other ways to check if input is integer or float (for if I wished to do that, I could just google it, and there are many such questions on stackoverflow.com as well), but to comprehend why my first code does not work, for, as far as I know, it ought to work well without any of the bugs it is currently facing.
Any help is greatly appreciated!:)
Assuming a missing function declaration:
main.c is missing the prototype for int intchecker(float x) so main.c assumes the old-school prototype of int intchecker(int x) and the code exhibits undefined behavior. Anything could happen.
Add prototype in main.c or put it in separate.h and include that header file here and in separate.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x);
int main(void) {
...