How Can You Fix Compilation Issues in Your 1-D Random Walk Program?

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C++ program designed to simulate a one-dimensional random walk. Participants explore issues related to compilation and runtime behavior, particularly focusing on unexpected output and input prompts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their program's intended functionality, which includes calculating averages based on random walk outcomes.
  • Another participant suggests using block comments to isolate problematic code sections and identify issues affecting input prompts.
  • Several participants point out syntax errors in the code, specifically regarding variable initialization and incorrect characters used for zero.
  • One participant shares their experience running the corrected code in a different compiler, noting the output they received.
  • Concerns are raised about the nature of random number generation, with one participant explaining that different runs should yield different outputs.
  • Another participant questions why the program does not prompt for input and instead produces random numbers, indicating a potential misunderstanding of the program's behavior.
  • Participants discuss the importance of comments in code for clarity, especially for users unfamiliar with the program's variables and expected inputs.

Areas of Agreement / Disagreement

Participants generally agree on the presence of syntax errors and the nature of random number generation. However, there is disagreement regarding the specific cause of the program not prompting for input, with some attributing it to code errors and others suggesting it may be a misunderstanding of the program's output behavior.

Contextual Notes

Limitations include unresolved syntax errors, potential misunderstandings of random number behavior, and the lack of comments in the code that could clarify variable roles for users.

Themis
Messages
7
Reaction score
0
Hi

I created a program to run a random walk in 1d with only input the number of steps and the number of walks. I used a random number generator to produce only two outcomes (step forward and step back) and in the main function I want to calculate the average of x (over a number of walks) and the mean square average of x. For that reason I introduced two loops the outer loop for running the number of walks and the inner loop that runs until the number of steps. Finally for each inner loop i want to calculate the x(not the average) and the for each x calculated in each inner loop sum them for the number of walks and then divide them by the number of walks to find the average.

Code:
//Random Walk in 1-d

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int yo(void) {
 
    int state;
     state = 1 + rand() % 2;
    
     return state;
    
}
   

int main () {
 
  int nwalks, x,xsum;
  int state=0;
  int nsteps=0;
  double msd=;
  double xaverage=0.0;
 
  count << "Give The number of Steps:" <<endl;
  cin >> nsteps;
  count << " Give number of Walks:"<< endl;
  cin >> nwalks;
 
   srand(time(0));  
   xsum=o;
   msd=o;
   for (int i=1; i <= nwalks; i++){
     x=0;
      for (int j=1; j <= nsteps; j++){
     state = yo();
     if (state == 1)
        x=x+1;
        else
          x=x-1;
       
       xsum= xsum + x;
       msd= msd + x*x;
   
      }      

   }
  
   count << " <x(N)>="<< xsum/nwalks <<" <x^2 (N)="<< msd/nwalks<<endl;
  
   return (0);
}

The compilation with g++ was ok but when i run it the results are some random numbers. It doesn't even asks for the inputs. What I am doing wrong? I am an amateur in c++
 
Technology news on Phys.org
I don't have a computer or a compiler right now, but what happens when you use a block of comments (/* */) to make the compiler ignore everything from "srand(time(0))" and "endl;". Does it ask for input then? You can use this strategy to find out which line of code is doing something you don't want it to.
 
Themis said:
Hi

I created a program to run a random walk in 1d with only input the number of steps and the number of walks. I used a random number generator to produce only two outcomes (step forward and step back) and in the main function I want to calculate the average of x (over a number of walks) and the mean square average of x. For that reason I introduced two loops the outer loop for running the number of walks and the inner loop that runs until the number of steps. Finally for each inner loop i want to calculate the x(not the average) and the for each x calculated in each inner loop sum them for the number of walks and then divide them by the number of walks to find the average.

Code:
//Random Walk in 1-d

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int yo(void) {

    int state;
     state = 1 + rand() % 2;
  
     return state;
  
}
 

int main () {

  int nwalks, x,xsum;
  int state=0;
  int nsteps=0;
  double msd=;
  double xaverage=0.0;

  cout << "Give The number of Steps:" <<endl;
  cin >> nsteps;
  cout << " Give number of Walks:"<< endl;
  cin >> nwalks;

   srand(time(0));
   xsum=o;
   msd=o;
   for (int i=1; i <= nwalks; i++){
     x=0;
      for (int j=1; j <= nsteps; j++){
     state = yo();
     if (state == 1)
        x=x+1;
        else
          x=x-1;
     
       xsum= xsum + x;
       msd= msd + x*x;
 
      }    

   }

   cout << " <x(N)>="<< xsum/nwalks <<" <x^2 (N)="<< msd/nwalks<<endl;

   return (0);
}

The compilation with g++ was ok but when i run it the results are some random numbers. It doesn't even asks for the inputs. What I am doing wrong? I am an amateur in c++
Your code has syntax errors, so won't compile. The three syntax errors I see are the following
  • double msd=;
  • xsum=o;
  • msd=o;
In the first line, you're missing the initialization value.
In the second and third lines, you have used the letter o instead of the numeral 0.

After I fixed these errors, I ran your code in Visual Studio 2013 and got this output:
Code:
Give The number of Steps:
20
 Give number of Walks:
14
 <x(N)>=-3 <x^2 (N)=245.429
 
Mark44 said:
Your code has syntax errors, so won't compile. The three syntax errors I see are the following
  • double msd=;
  • xsum=o;
  • msd=o;
In the first line, you're missing the initialization value.
In the second and third lines, you have used the letter o instead of the numeral 0.
This isn't my question, but I'm curious nonetheless - what could cause g++ to compile something with such obvious errors?
 
Heatherfield said:
This isn't my question, but I'm curious nonetheless - what could cause g++ to compile something with such obvious errors?
No idea.

But see my previous post -- I added a bit after you saw it and replied.
 
Heatherfield said:
This isn't my question, but I'm curious nonetheless - what could cause g++ to compile something with such obvious errors?
Yes Sorry for that. those are mistakes i did after i compiled the program and forgot to erase them
 
Mark44 said:
Your code has syntax errors, so won't compile. The three syntax errors I see are the following
  • double msd=;
  • xsum=o;
  • msd=o;
In the first line, you're missing the initialization value.
In the second and third lines, you have used the letter o instead of the numeral 0.

After I fixed these errors, I ran your code in Visual Studio 2013 and got this output:
Code:
Give The number of Steps:
20
Give number of Walks:
14
<x(N)>=-3 <x^2 (N)=245.429

Any idea why after i corrected the mistakes when i run the program again it produces random numbers and not the same output as yours?
Is it the compiler? I use g++ because the professor wants it.
 
What happens if you comment out everything apart from the essentials (main function, includes, et cetera) and the requests for input (count and cin)?
 
Themis said:
Any idea why after i corrected the mistakes when i run the program again it produces random numbers and not the same output as yours?
Two different runs of the program should produce different output, even with the same numbers as inputs. That's the nature of using random numbers.
When I ran your code I entered 20 for the no. of steps and 14 for the no. of walks. Your code has zero comments and no information in the prompts about what these variables are supposed to represent, other than one is steps and the other is walks. A naive user such as myself won't know the difference between these. Comments in your code and more helpful comments would make it easier for a user to know what to enter.

Your instructor is likely to deduct points if there are no comments in your code.
Themis said:
Is it the compiler? I use g++ because the professor wants it.
As I said, different program runs will produce different output, and the same is true if you use a different compiler.
 
  • #10
I had the impression that the program only gave randomn numbers and no input prompt? Or do I need to drastically refine my fine-reading skills?
 
  • #11
Mark44 said:
Two different runs of the program should produce different output, even with the same numbers as inputs. That's the nature of using random numbers.
When I ran your code I entered 20 for the no. of steps and 14 for the no. of walks. Your code has zero comments and no information in the prompts about what these variables are supposed to represent, other than one is steps and the other is walks. A naive user such as myself won't know the difference between these. Comments in your code and more helpful comments would make it easier for a user to know what to enter.

Your instructor is likely to deduct points if there are no comments in your code.

As I said, different program runs will produce different output, and the same is true if you use a different compiler.
Thanks a lot for the your advice I will add comments when i finally run the program.
My fault I didn't make it clear : When i say it produces random numbers I mean that when i run it, it doesn't ask for inputs, it just makes a list of 24 random numbers and that's it. My output should be 2 numbers x(N) and x^2(N).

I installed visual studio and it runs now. Don't know why but it works

How about now ? I added comments
Code:
//Random Walk in 1-d

#include <iostream>  // the libraries we need to run the program
#include <cstdlib>
#include <ctime>
using namespace std;
// The function yo is the random generator that gives two possible states
// If it gives 1 the guy walks one step forward if 2 it goes one step back
int yo(void) {

    int state;
    state = 1 + rand() % 2;

    return state;

}
// End of random generator function

int main() {

    int nwalks, x, xsum; // nwalks is the number of walks we want
                         // x is the displacement from the point of origin( we start at zero --see later in loop)
                         // xsum is adding the x's in each inner loop
    int state = 0;    
    int nsteps = 0;
    double msd;          //msd is the mean square displacement
 

    count << "Give The number of Steps:" << endl;  // The program asks for input the number of steps we want the guy to walk
    cin >> nsteps;
    count << " Give number of Walks:" << endl;    // The number of walks input, is how many times we want to repeat the random walk
    cin >> nwalks;

    srand(time(0)); // this is the seed of our generator
    xsum = 0;     // we start our computation of xsum and msd from zero
    msd = 0;
    for (int i = 1; i <= nwalks; i++){ // this loop is for repeating the scenario according to our input of nwalks
        x = 0; // When the inner loop completes each task we want for the next nwalk x to start again from zero
        for (int j = 1; j <= nsteps; j++){ // This is the random walk simulation for nsteps input
            state = yo(); //we set our variable state to take whatever value our random generator gives
            if (state == 1) // If the generator gives 1 then the guy makes a step forward and so we add 1 to x
                x = x + 1;
            else
                x = x - 1; // If the generator gives 2 then the guy makes a step back and we subtrack 1 from x

            xsum = xsum + x; // This is the distance the guy has from x=0 after nsteps
            msd = msd + x*x; // This is the x^2 (not the average value)

        }

    }
    //The simulation must calculate the average of x and the average of x^2--so <x>= all x's divided by the nwalks
    // the same stands for the msd
    //We expect that <x> is almost zero (equal propability for back and forward) and <x^2> almost equal to the number of steps
    count << " <x(N)>=" << xsum/nwalks << " <x^2 (N)>=" << msd/nwalks << endl;

    return (0);
}
I have one logical question because the results are not very good. The <x> must be almost zero and the <x^2> almost equal to nsteps in this case.
The last two equations are in the right place? or to I need something extra?
xsum = xsum + x; msd = msd + x*x;

When the inner loop completes (after nsteps) then it calculates xsum and msd. Then it repeats the same for nwalks. I want to add each xsum and msd and divide them by nwalks to take the average. Do i express this correct in my program?

Heatherfield said:
I had the impression that the program only gave randomn numbers and no input prompt? Or do I need to drastically refine my fine-reading skills?

Sorry Heatherfield i wasn't very clear. I hope with this post i am better

Thanks a lot again
 
  • #12
Themis said:
I have one logical question because the results are not very good. The <x> must be almost zero and the <x^2> almost equal to nsteps in this case.
The last two equations are in the right place? or to I need something extra?
xsum = xsum + x; msd = msd + x*x;
The two assignment statements (don't call them equations) are in the wrong place. As you say just below, xsum and msd are calculated. This means they should be outside the inner loop
Themis said:
When the inner loop completes (after nsteps) then it calculates xsum and msd. Then it repeats the same for nwalks. I want to add each xsum and msd and divide them by nwalks to take the average. Do i express this correct in my program?
The calculation for xsum looks OK to me, but I'm not sure about the msd. Is msd for a given run just the square of the current displacement at the end of a walk? If so, then I guess it's OK.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
22
Views
5K
  • · Replies 3 ·
Replies
3
Views
736
  • · Replies 9 ·
Replies
9
Views
3K
Replies
29
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
4K