code::blocks creates a .dat file but it's empty - c

I've been writing a code of random walk in c language. The program compiles perfectly and I can see the results in the code::blocks console. But I need to create a file .dat for import its data and make a plot of "iterations number vs the mean of the square displacement" in qtgrace.
The problem is that code::blocks creates the .dat file but it's empty. I don't know why this is happening.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
FILE *fp;
int c,i;//c is the number of walkers and i is the iterations number
double x,r; //x is the displacement and r is a random number
double s[2000]; //array for the displacement squared
int n[10]; //array for the number of walkers
double main(){
fp=fopen("caminata.dat","w");
printf("Write the iterations number i=");
scanf("%d",&i);
printf("Write the number of walkers c=");
scanf("%d",&c);
n[10]=0;
s[2000]=0;
for (int j=0; j<c; j++){
//srand((long)time((time_t *)(NULL)));
x=0.;
for(int k=0; k<i; k++){
r=rand()/(double)RAND_MAX;
if(r<=0.5){
x=x+1;
}
if(r>0.5){
x=x-1;
}
s[k]=s[k]+x*x;//
}
}
for (int k=0; k<i; k++){//mean of the square displacement
s[k]=s[k]/c; //divided by the number of walkers
printf("\n%d %lf",k,s[k]);
fprintf(fp, "\n%d %lf",k,s[k]);
}
fclose(fp);
}
This is the plot I must obtain
Random_Walk: iterations number vs the mean of the square displacement

I see 2 times k<i in for loops but I don't see i being defined.
There is 1 chance out of 2 that value is defined to a negative value, in which case it makes sense your file is empty.

Related

How to enter an output into an array in c

My Code so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
int i;
int rollDice;
int firInp;
int secInp;
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): ");
scanf("%d", &firInp);
printf("Enter the amount of throws you want(MAX=499, MIN=1): ");
scanf("%d", &secInp);
srand ( time(NULL) );
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters. Please enter a number in the right parameters.");
}
return 0;
}
I want to have percentages in my code. I think the way to do that is to first enter the output of my code into an array. If there's another way please feel free to let me know.
edit: I want the output to be something like this:
1 3 4 4 4 5
occurrence of 1: 16.6 percent
occurrence of 3: ..and so on
Instead of entering the output of the random function in an array you could just use that array as a counter, and incrementing the array at position rollDice every time a number appears. Than you could easily extract the percentage by summing all the elements of the array and by dividing each element by that sum.
You can create an array of integers, with size equals to the numbers of possible values your dice can output. Then you use it as a counter for the number of occurences you get, the index of the array will represent that output value (you can use rollDice-1 since 0 isn't a possible output of the dice), and the value at the index will be the number of occurences.
After you finish rolling the dice you just have to print the percentage like this:
for (int i=0;i<firInp;i++) { // firInp: n_faces = n_possible_values
printf("Occurrence of %d: %.1f percent\n", i+1, ((float)array[i]*100)/(float)secInp);
}

fprintf inside a for loop is not working properly

I am attempting to create a text file using C that will contain a table of values in Fahrenheit and their Celsius conversion.
I am able to use fprintf properly outside of the for loop but when I put it inside it does not print anything to the file. The code compiles properly but when I try to execute it completes but with exit code "-1073741819"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
FILE *filePointerThree;
double myArray[100];
filePointerThree = fopen("myFileFive.txt", "w");
for(int i=0; i<=100; i++)
{
myArray[i] = (i-32)/1.8;
}
for(int j=0; j<=100; j+=5)
{
fprintf(filePointerThree, "%d degrees F \t %5.2lf degrees C\n", j, myArray[j]);
}
fclose(filePointerThree);
}
Your array needs to be larger to hold 101 values (0 through 100):
double myArray[101];
Upon further review, the code can be simplified to not require an array, as follows. A return 0; at the end of main() will ensure an exit code of 0. Minor: the math.h and stdlib.h includes are not required as fopen() and friends are defined in stdio.h.
#include <stdio.h>
int main()
{
FILE *filePointerThree;
filePointerThree = fopen("myFileFive.txt", "w");
for(int j=0; j<=100; j+=5)
{
fprintf(filePointerThree, "%d degrees F \t %5.2lf degrees C\n", j, (j-32)/1.8);
}
fclose(filePointerThree);
return 0;
}

How to avoid segmentation fault in C when the array is defined?

I have an array A1[ ]={1,0,1,1,..'X',0,1,'X',.1,0...}, as big as 10 Megas of binary integers and some 'X' values (taken as the ASCII integer value), I am thinking to use malloc but I already have the array defined (I am just copy and paste the values before running my code). Any other suggestions on how to avoid segmentation fault? So far this the error that I am geting :(.
The code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
//int A[]={'x','x',1,0,'x',0,'x',1,1,1,...};//up to 10M values
int A[]={0,'x',1,'x',1,0,'x',0,'x','x',1,'x',0,'x','x','x','x','x','x',0};
int j;
#define n (sizeof(A)/sizeof(*A)) // bits
printf("Patt size= %d\n",n);
/*Code will be added here to make some calculations with A*/
printf("A ="); // show input
for(j=0; j<n; j++)
printf(" %d",A[j]);
return 0;
}

Removing repetitions of same numbers in an array

Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++

Problems with arrays

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);

Resources