We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Loops and more
Page Index Toggle Pages: 1
Loops and more (Read 1770 times)
Loops and more
Mar 5th, 2008, 5:30am
 
Seeking assitance with loops/nested loops ect...please message me using the processing.org message system.

Thank you
Re: Loops and more
Reply #1 - Mar 5th, 2008, 7:53am
 
Please elaborate more on the specific problem.

It's generally considered polite to post full questions/answers on the forum so everyone can benefit from everyone else.
Re: Loops and more
Reply #2 - Mar 5th, 2008, 2:41pm
 
I'm looking to make a binomial, bellcurve chart for coin flips.

I need some assitance with loop and nested loop set up.
I would post my current code, however I don't want anyone to laugh at it. Plus all I have are mostly int named.
Re: Loops and more
Reply #3 - Mar 5th, 2008, 3:04pm
 
navaji,

I can guarantee you, that no one on this board will laugh at your code. You must remember, that even the best coders out there, once where complete beginners too and had go write "simple" code and make many mistakes to reach the expertise they have now.

Even if you do have flaws or misconceptions in your code, the users here will gladly help you point them out and tell you how you can improve your program.
Re: Loops and more
Reply #4 - Mar 5th, 2008, 5:41pm
 
This is what I have thus far, all I have are declaired "int"...I don't even know where to start to make a "for" statment.  I know how they work not how to write them.

example = for (n = 1; n <= 10; ++n)

I know that for (init_expression; loop_condition; loop_expression)
followed by program statement

Problem is I have no idea where to start for what I'm working on, I need a little more syntax help to get this going.

Code ----

int oneOfTen ;
int twoOfTen ;
int threeOfTen ;
int four0fTen ;
int fiveOfTen ;
int sixOfTen ;
int sevenOfTen ;
int eightOfTen ;
int nineOfTen ;
int tenOfTen ;
int high = 400 ;
int wide = 400 ;
int numHeadsThisTime ;
int trialNumber ;
int flipNumber ;

size ( wide, high ) ;
background (80, 180, 80 ) ;
stroke ( 0xFF00FFFF ) ;



Re: Loops and more
Reply #5 - Mar 5th, 2008, 6:25pm
 
Yeah, don't worry about people laughing. 'Tis all good Smiley

Ok, I'm not sure what exactly is what you need to do to get this bellcurve of flip coins. I mean, I know what a bellcurve is, but I'm not sure exactly what do you want to acomplish.

What I can suggest is, try to think of the logical steps to solve your problem *before* you program anything. That way you can see if you need for loops, while loops, etc.

What I mean is, for example: "I need to first write down the state of each of ten coins (write down if they are tails or heads), then, count each tails and each heads and add them toghether. Then divide them by PI and multiply by 2..."

Or whatever you need to do. Obviously mine was an absurd example.

If you put the problem this way, I can help you out on what exactly do you need. Specifically, here's an example of a nested for loop:

for(int i=1;i<=10;i++){
  for(int j=1;j<=10;j++){
      println("i * j = " + i*j);
  }
}

This will print the multiplication table, i.e. 1x1=1...3x4=12...up to 10x10=100
Re: Loops and more
Reply #6 - Mar 5th, 2008, 7:20pm
 
I'm trying to show a grapical representation of how many "heads" you flip out of ten tries.  There will be 1000 trials each comprised of 10 flips...that would be 10,000 flips.

So there would need to be a Boolen random function included. The program would also need to keep track of how many "heads" where found in the 10 flip trials, than send that information to the "Bell Curve" (Possibly some kind of bar graph).  After each trial is logged to the bell curve, resest and do it all over again for the outter "for, loop" 1000 times.

I guess its more complex than 1st thought =/ hopefully the above explination help paint a better picture.

example for the bol

Boolean isHeads = false;
isHeads = ( random (0, 1)> 0.5) ;

I made the "int" listed above for the program to beable to track the results.  However thats about all I know.


Re: Loops and more
Reply #7 - Mar 5th, 2008, 8:08pm
 
Ok, this is not as hard as you might think.

You need to do two things. Generate the array of "flips", and display the array in a BellCurve.

If you need 10 flips each trial, with 1000 trials, then you need an array to store the coins like

Code:

boolean[][] coins=new boolean[1000][10];


Then you need to fill it, like:
Code:

float r;
for(int trial=0;trial<1000;trial++){
for(flip=0;flip<10;flip++){
r=random(0,1);
if(r<0.5){
coins[trial][flip]=true;
}
else{
coins[trial][flip]=false;
}

}
}


Each array location represents a "heads" if its true, or false otherwise (or whatever you like). So then you just have to work out the math for building the bellcurve, and you are done.

**Sorry for the code formatting, but its hard to indent in the forums for some reason : / **
Re: Loops and more
Reply #8 - Mar 5th, 2008, 8:33pm
 
I see arrays allow you to type less than you would using an "for" loop code.

Can this be done in a "for" lope code to better understand the syntax?

I'll try and copy/paste your code and see if I can get it to integrate into the little bit I have.
Re: Loops and more
Reply #9 - Mar 6th, 2008, 5:11am
 
acapulco, I still need to converse...contact me by using the sites message system.

Please =/
Re: Loops and more
Reply #10 - Mar 9th, 2008, 4:18am
 
Here is a small "for loop" demo:

Code:
void setup() {
size(125,125);
background(153);
noLoop();
//
int imax,jmax;
//
imax=4;
for (int i=0; i<imax; i++) {
print(i+" ");
}
println();
//
imax=4;
jmax=4;
for (int i=0; i<imax; i++) {
print(i+":");
for (int j=0; j<jmax; j++) {
print(j+".");
}
print(" ");
}
println();
//
}//setup()

void draw() {
}//draw()

The output is:

0  1  2  3  
0:0.1.2.3.  1:0.1.2.3.  2:0.1.2.3.  3:0.1.2.3.
Page Index Toggle Pages: 1