# Simple Hailstone program using Dev C++

Page 1 of 1

## 5 Replies - 7791 Views - Last Post: 22 November 2011 - 12:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256449&amp;s=b995d01c71e5c31b7678dcff8c55b855&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 programgirl

Reputation: 0
• Posts: 6
• Joined: 20-November 11

# Simple Hailstone program using Dev C++

Posted 20 November 2011 - 09:01 AM

I am working on a simple Hailstone program for numbers 1 through 200 but i am having some trouble. I did the calculations to make sure it output the correct maximum number of steps per number. Everything worked but once i put in the for loop the ouput was wrong?? Can you tell me what you see is incorrect? I am only a beginner so please only use simple word to explain it to me. This is in Dev C++ download. Any help is greatly appreciated (as soon as possible!) Thank You!

```#include <iostream.h>
int main()
{
int counter=0;
int first=0;
int second=0;
int temp=0;
int number=1;
int i=0;

for (i=1; i<=200; i++){

counter=0;
first=0;
second=0;
temp=0;
temp=i;
cout<<i<<" ";
while (!(first==4 || second==2 || temp==1)) {

if (temp%2==0)
temp=temp/2;
else
temp=temp*3+1;
counter=counter+1;

first=second;
second=temp;
}
cout<<"Number of interactions: "<<counter<<endl;

}

cout<<endl;

return(0);
}
```

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 20 November 2011 - 09:06 AM

Is This A Good Question/Topic? 0

## Replies To: Simple Hailstone program using Dev C++

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Simple Hailstone program using Dev C++

Posted 20 November 2011 - 09:09 AM

Please tell us what your expected output is and what you are actually receiving. Not everyone here is well-versed in the "Hailstone" program and what the expectations are here.

### #3 programgirl

Reputation: 0
• Posts: 6
• Joined: 20-November 11

## Re: Simple Hailstone program using Dev C++

Posted 20 November 2011 - 09:22 AM

I am trying to find the maximum number of steps for each number from 1 to 200. So the for loop incrememnts by one every time but the number of interactions is wrong. for example, the number 200 is supposed to have 26 steps until the code ends in 4, 2, 1. But this program is telling my it only needs 25 steps.. It was tellling me all of the correct numbers of steps (I have a list) until i added the for loop. My friend tells me i am only missing one important line right bellow the for loop but i cannot figure it out.

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Simple Hailstone program using Dev C++

Posted 20 November 2011 - 11:24 AM

Should the terminating condition of 4,2,1 count as an interaction? If so, does your program account for that?

### #5 programgirl

Reputation: 0
• Posts: 6
• Joined: 20-November 11

## Re: Simple Hailstone program using Dev C++

Posted 20 November 2011 - 11:51 AM

The sequence should end as soon as the first 4 2 1 appears.

and the 4 2 and 1 should count each as one interaction

im not sure if the program i displayed is counting those steps. Each number displayed a number interactions but they were off. I re did some of the lines and now it workes good. here it is:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int counter=0;
int first=0;
int second=0;
int third;
int temp=0;
int i=0;
int tempnumber=0;
int tempcounter=0;

cout<<"The following are the number of steps required for all numbers from 1 to 200 to create the repeating sequence 4 2 1:"<<endl;
cout<<endl;
cout<<"Number";
cout.width(10);
cout<<"Steps"<<endl;

for (i=1; i<=200; i++){

counter=0;
first=0;
second=0;
third=0;
temp=i;
third=temp;

cout.width(6);
cout.setf(ios::right);
cout<<i;

while (first!=4 || second!=2 || third!=1){

if (temp%2==0)
temp=temp/2;
else
temp=temp*3+1;

first=second;
second=third;
third=temp;
counter++;

};
cout.width(10);
cout.setf(ios::right);
cout<<counter<<endl;

if (counter>tempcounter){
tempnumber=i;
tempcounter=counter;
}
}
cout<<endl;
cout<<"Number "<<tempnumber<<" had the highest number of steps of "<<tempcounter<<"."<<endl;
cout<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
```

But now i need to eliminate some of the variables. There are deffinately too many but i dont know which can be taken out or condensed

### #6 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Simple Hailstone program using Dev C++

Posted 22 November 2011 - 12:04 AM

```int hailstone(int n)
{
int steps = 0;
while( n > 1 )
{
cout << n << ' ';
if( n % 2 == 1) n = 3*n + 1;
else n = n / 2;
++ steps;
}
cout << n << endl;
return  ++steps; // return ++steps to count 1
}

```

Then just call this function in a loop for n = 1 to 200