I'm working with C program, and have a simple C code to generate data like follow:
#include <stdio.h>
#include <time.h>
#include <math.h>
double x[100];
int i;
srand(3);
for (i = 0; i < 100; i++)
{
x[i] = (double) rand() / (RAND_MAX + 1.0);
}
I want to pass this array into matlab, and do the following calculate:
y = transpose(x)
Mdl = arima(1,0,0)
[EstMdl,EstParamCov,logL,info] = estimate(Mdl,y)
a = EstMdl.AR
b = EstMdl.Variance
How can I pass array x to matlab and get a and b back from matlab using C language?
Thanks in advance.
Related
In this example I want to print out the number 4. This is a simplified version of my problem, but my issue is the same. After assigning b a value (in this case 4), I want to print out the 4th element of the array, not directly, but by using a separate integer (c). However a 0 gets printed out as a result. I have no idea why. Would be glad if you could help. Thanks a lot in advance!
#include <stdio.h>
#include <stdlib.h>
int numbers[10], a, b, c;
int main() {
for (a = 0; a < 11; a++) {
numbers[a] = a;
}
b = 7 - 3;
numbers[b] = c;
printf("%d", c);
return 0;
}
Instead of setting the 4th element of the array, you should read it and store it into c:
c = numbers[b];
Also note that the initialization loop runs one step too far, assigning non-existent element numbers[10].
Here is a modified version:
#include <stdio.h>
int main() {
int numbers[10], a, b, c;
for (a = 0; a < 10; a++) {
numbers[a] = a;
}
b = 7 - 3;
c = numbers[b];
printf("%d\n", c);
return 0;
}
This question already has answers here:
Generate random double number in range [0, 1] in C
(2 answers)
Closed 2 years ago.
I would like to generate a random, real number in the interval [0,1].
I would like to set a pointer, say n, for the number so whenever I stated n, it will be referred to the random generated number.
I have searched on StackOverflow and on Google, but most of them are for C++ or for integers.
I have tried this code suggested to me in the answers:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double n;
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
printf("%f", n);
return 0;
}
However, I can only get a value 0.00000000.
How could I fix my program?
You can use:
#include <time.h>
srand(time(NULL)); // randomize seed
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
srand() sets the seed which is used by rand to generate pseudo-random numbers. If you don't call srand before your first call to rand, it's as if you had called srand(1) (serves as a default).
If you want to exclude [1] use:
(double)rand() / (double)((unsigned)RAND_MAX + 1);
Full solution:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
double get_random() { return ((double)rand() / (double)RAND_MAX); }
int main()
{
double n = 0;
srand(time(NULL)); // randomize seed
n = get_random(); // call the function to get a different value of n every time
printf("%f\n", n); // print your number
return 0;
}
Every time you run it you will get a different number for n.
This shows how to get random real numbers in the range 0..1 but please note that they are not uniformly distributed. There are only (RAND_MAX+1) discrete values.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int i;
double n;
srand((unsigned)time(NULL)); // seed the random num generator ONCE only
for(i = 0; i < 3; i++) { // get 3 random numbers
n = (double)rand() / RAND_MAX; // in the range 0 ... 1
printf("%f\n", n); // use correct format specifier for the var
}
return 0;
}
My program output:
0.622608
0.814081
0.878689
I am using Visual Studio 2010 and programming in C. I am attempting to produce a random integer value via the rand() method. Here is the code:
/*main.cpp*/
int main (void)
{
InitBuilding();
return 0;
}
/*building.cpp*/
//includes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//data structure
typedef struct
{
int type; //building type
} BUILDING;
//global variables
BUILDING g_aBld[200];
//initialization
void InitBuilding(void)
{
srand((unsigned)time(NULL));
for(int cntBld = 0; cntBld < 200; cntBld++)
{
g_aBld[cntBld].type = (rand() % 3);
}
}
After debugging I've realized that 0 is continually generated for each iteration of the loop. I've used this exact code before in other programs, and it worked fine. I have no idea why it wouldn't be working now.
Thanks in advance for any replies.
g_aBld[cntBld].type = (rand() % 3);
Don't use mod to reduce the range of rand because this can interoperate badly with the way your random number generator initializes itself. Try, for example:
g_aBld[cntBld].type = rand() / (RAND_MAX / 3);
Project compiled with the addition of
int main( int argc, char ** argv )
{
InitBuilding();
}
With this added the code worked producing types of 0, 1, 2
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C Build error when getting the value of sin()
I am trying to integrate a function using 100 intervals within the period, so I'm using a for loop. The function contains cos(stuff here), but it won't run within the for loop for some reason. If I move it out of the for loop, it works fine. I have #include , so I don't understand. Any help is appreciated. Thanks.
#include <stdio.h>
#include <math.h>
//float t = 0.000000;
double T = .001;
int n = 100;
double pi = 3.141592654;
double a[5];
double b[5];
double function_results[100];
double anfunction_results[100];
double bnfunction_results[100];
double final = 0.000000000000;
double anfinal = 0.000000000;
double k = 0.000000;
double function(float t){
double f = 3*pow(t,2);
return f;
}
int main()
{
//double w = 2*pi/T;
double h = T/n;
int i = 1;
for(; i<100; i++){
double iterate = -T/2 + h*i;
function_results[i] = function(iterate);
final = final + function_results[i];
}
a[0] = (h/3)*(function(-T/2) + final+function(T/2))/T;
printf("%.12f \n",a[0]);
int p = 1;
int l = 1;
for(; l<=5; l++){
for(; p<100; p++){
double iteration = -T/2 + h*p;
k = l+.0000001;
anfunction_results[p] = 3*pow(iteration,2)*cos(k*iteration*2.00000*pi/T*pi/180.00000);
anfinal = anfunction_results[p] + anfinal;
}
a[l] = (2/T)*(h/3)*(3*pow(-T/2,2)*cos(-T/2.0000) + anfinal + 3*pow(T/2,2)*cos(k*T/2.00000*2.00000*pi/T*pi/180.00000));
}
//printf("%.12f \n",a[1]);
//printf("%.12f \n",a[2]);
//printf("%.12f \n",a[3]);
//printf("%.12f \n",a[4]);
//printf("%.12f \n",a[5]);
return 0;
}
You are probably not linking to the math library. Add -lm to your linker options.
(I don't know why it would "run fine" with cos() moved out of the for loop, but this explanation is not very clear on your part. If you get an undefined reference for cos() in one part of the program, you should get it in a different part as well, if the compiler options are the same in both cases.)
Im trying to make a program that calculates out a math equation, Im getting stuck on how i generate a random number from 0.00 to 1.00 and store it in a variable a.
this is my code so far, im stuck to how now take that number and store it for future use. I need to store that random number in a, and hten use it in a loop, and then generate a new random number and use it in the 2nd cycle of the loop.
EDIT
this is what i have been working on now, it is suppose to calculate the number of times a random number is inside the area, count it, and then devide by the number of times run, but im not getting any output
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void initrand(void)
{
srand(time(0));
}
float randfloat(void)
{
return rand()/(float)RAND_MAX;
}
int main(void)
{
int n = 10;
float x;
float y;
float pi = 3.1415;
float rootxy;
initrand();
int z = 0;
int inside = 0;
x = randfloat();
y = randfloat();
float area = 0.25 * pi;
float calculatedpi;
rootxy = sqrt(pow(x,2) + (pow(y,2)));
while (z < n){
if (rootxy > area) {
inside++;
z++;
}
else{
return 0;
}
calculatedpi = (inside/n);
}
printf("%f", calculatedpi);
}
There are a few issues with your code:
You shouldn't use nested functions. Some compilers support them as an extension but it's not standard. Define randfloat and initrand outside main
The function initrand does too little. Why not call srand((time(0)); from main ?
Your initrand function is declared as returning a double but it doesn't return anything (and the way it's named it shouldn't). If you need to use such a function, why not make it return void ?
You should rarely use float. Why not use double ?
That said, you can do this to store that random value:
double randdouble()
{
return rand()/((double)RAND_MAX + 1);
}
int main()
{
double x = randdouble();
/* ... */
}
I think you want something like this:
#include <stdlib.h>
#include <time.h>
void initrand(void)
{
srand(time(0));
}
float randfloat(void)
{
return rand()/(float)RAND_MAX;
}
int main(void)
{
initrand();
float a = randfloat();
return 0;
}
You can't nest functions like in some other languages.
You had non-matching parentheses in the initrand function.
I fixed the declarations of your functions, use void when there are no parameters, initrand doesn't return anything.
Your division by RAND_MAX+1 was a little messed up. Simply divide by RAND_MAX and the result will be in the closed interval [0,1]. And the syntax for the conversion to float was not quite right.
If you want to get random double numbers in a specified range you can use this function
// Return a random double from a to b
double randomDouble(double a, double b)
{
return = ( rand() / ( (double)RAND_MAX + 1.0))
* (b - a) + a;
}