Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:04:04+00:00 2026-05-16T01:04:04+00:00

Hiya im trying to write a code for basic 1D Monte-Carlo integration. To do

  • 0

Hiya im trying to write a code for basic 1D Monte-Carlo integration. To do so I need a list of pseudo-random numbers which I can then input into a function (stored in another subroutine). I’ve given the list of random numbers a pointer but when I try to dereference it in main I get “error: incompatible types when assigning to type ‘double[11]’ from type ‘double’ ” . Could anyone tell me where I’m going wrong? My code can be found here:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef RAND_MAX
#define RAND_MAX 2147483648
#endif
#define N 10

double function(double x);
double* rdm(void);

void main(void)
{
   double* Random_number_list;
   int i;
   double sum = 0.0, sum2 = 0.0, X[N+1],S, Random_number_list2[N + 1];
   double F[N+1], lower, upper, avg, avg2;
   printf("Lower Bound:  ");
   scanf("%lf", &lower);
   printf("Upper Bound:  ");
   scanf("%lf", &upper);

   Random_number_list2 = *Random_number_list;

   for (i = 0; i <= N; i++) {
      X[i] = ((upper - lower)*Random_number_list2[i]) + lower;
      F[i] = function(X[i]);
      sum = sum + F[i];
      sum2 = sum2 + (F[i] * F[i]);
   }
   avg = sum / N;
   avg2 = sum2 / N;
   S = (upper - lower) * (avg + sqrt((avg2 - (avg * avg)) / N));
   printf("The Monte Carlo approximation is %lf\n", S);  
}

double function(double x)
{
   double y;
   y = sin (x);
   return y;
}

double* rdm(void)
{
   double* Random_number_list = calloc(N + 1, sizeof(double));
   int i;

   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list;
}

Many Thanks. Jack Medley

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:04:05+00:00Added an answer on May 16, 2026 at 1:04 am

    Given:

     int * a;
     int b[5];
    

    then you can write:

     a = b;  // copying a pointer, same as a = &b[0];
    

    However, you cannot write:

     b = a;  // b's memory is allocated, you cannot swap it out.
    

    Which means, lose Random_number_list2, and work with just Random_number_list.

    You want:

     double* Random_number_list = rdm();
    

    and later

     // added space, so it's clearer you're multiplying and not dereferencing.
     X[i] = ((upper - lower) * Random_number_list[i]) + lower;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hiya, I'm trying to make a small loop which presents an error when the
Hiya, my code currently has three functions each producing a massive array of random
I have been trying to get this to work and can't. I have a
hiya, i have the following code but when i try and create a new
I wanted to use the following code from here: How can I save all
hiya i have a massive job trying to sort thousands of records all is
When running the following block of code, FF and Chrome output typeof(hiya) = string
Hiya I am working on an FTP client, can someone tell me the difference
Hiya I just want to do some windows script host scripts, and I know
Hiya, I'm sort a newbie when it comes to hibernate so I'm going to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.