# Can I have any feedback on my HapSad Program?

Page 1 of 1

## 6 Replies - 373 Views - Last Post: 04 December 2014 - 09:18 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=359295&amp;s=f1b8ce4e8443aa012bb3dfaac9fad416&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 into_2_c++

Reputation: 0
• Posts: 6
• Joined: 22-November 14

# Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 08:37 AM

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).

```
/// James Vrionis
/// CS7A

/// #include <iostream>
/// #include <vector>
/// using namespace std;

#include "std_lib_facilities.h"

private:
bool happy;
unsigned x, cycle_entry, dist_to_cycle, start, stop;
public:
vector<unsigned int>sequence;
unsigned cycle_length;
void show_sequence();
bool repeated_value(unsigned x, vector<unsigned> v);
};

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

for(int i=0; i<sequence.size();i++) {
cout<<sequence[i]<< " ";
}
cout<<'\n';
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

bool HapSad::repeated_value(unsigned x, vector<unsigned> v) {
dist_to_cycle=0;
for(int i = 0; i < v.size(); ++i)   {
if(x == v[i])  {                                                                                                                //                                      //
return true;
}
dist_to_cycle++;               //increments till first.
}                                  // begin ... to end
return false;
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

int sum = 0;
unsigned x=seed;
bool repeat = false;
cycle_length=0;
sequence.push_back(seed); ///put into seq.
while(repeat==false)    {
vector<unsigned> digits;
while(x>0)  {               /// numbers to be squared
unsigned digit = 0;
digit = x%10;
digits.push_back(digit);    ///put digit into vector of digits
x /= 10;
}
sum=0;
unsigned H;
for(int i=0;i<digits.size();i++)    {       ///square the numbers
H=digits[i];
sum = sum + H*H;
}
x=sum;
repeat = repeated_value(sum,sequence);      ///call bool repeated_value
if(repeat==true)
cycle_length=sequence.size()-dist_to_cycle;  ///subtract the sequence length by the dist to cycle
else
sequence.push_back(sum);
}
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

if(hs[j].cycle_length==8)   {
}
else
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

if(hs[j].sequence.size()>=longest_sequence.sequence.size())
longest_sequence=hs[j];
return longest_sequence;
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

int main()  {
unsigned start, stop;
cout << "\nEnter start and stop values separted by a space, in order to construct a vector of HapSad: ";
cin >> start >> stop;
for(unsigned i = start, j = 0; i<stop; i++, j++)    {
hs[j].show_sequence();
cout << "Cycle length= " << hs[j].cycle_length <<'\t';  ///tab sequence to right of cycle length.
longest_sequence = find_longest_sequence(longest_sequence,hs,j);
}
cout <<'\n'<< "------------------------------------------------------------------------" << '\n';

<< ". " << "\nThe Longest consecutive sad cycle is " << longest_sad_cycle.size() <<". " << '\n';
else
cout<< "\nThere are no Consecutive Sad Cycles ";
cout<< "\nThe longest sequence is " << longest_sequence.sequence.size() <<". " << '\n';
for(int i = 0; i<longest_sequence.sequence.size();i++)
cout << longest_sequence.sequence[i] << ", ";
cout << '\n';
}

///---------------------------------------------------------------------------------
///---------------------------------------------------------------------------------

```

Is This A Good Question/Topic? 0

## Replies To: Can I have any feedback on my HapSad Program?

### #2 modi123_1

• Suitor #2

Reputation: 11617
• Posts: 45,788
• Joined: 12-June 08

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 08:40 AM

Is there a specific question?

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 08:45 AM

Instead of cout << '\n'; I would suggest cout << endl;.

### #4 into_2_c++

Reputation: 0
• Posts: 6
• Joined: 22-November 14

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 08:53 AM

i was wondering why vector<unsigned in> sequence and unsigned cycle_length arent able to be private?

### #5 jimblumberg

Reputation: 4912
• Posts: 15,348
• Joined: 25-December 09

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 09:02 AM

Because find_longest_sad_cycle() is not a member function of your class it can't access the private member variables of your class.

Jim

### #6 into_2_c++

Reputation: 0
• Posts: 6
• Joined: 22-November 14

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 09:06 AM

thanks jim. my last question is
```if(longest_sad_cycle.size()>0)
123	            << ". " << "\nThe Longest consecutive sad cycle is " << longest_sad_cycle.size() <<". " << '\n';
124	    else
125	        cout<< "\nThere are no Consecutive Sad Cycles ";
126	        cout<< "\nThe longest sequence is " << longest_sequence.sequence.size() <<". " << '\n';
127	    for(int i = 0; i<longest_sequence.sequence.size();i++)
128	        cout << longest_sequence.sequence[i] << ", ";
129	        cout << '\n';
130	}

```

output problem ----->
The longest sequence is 19.
997, 211, 6, 36, 45, 41, 17, 50, 25, 29, 85, 89, 145, 42, 20, 4, 16, 37, 58,

how can i replace the last comma with a period?

### #7 jimblumberg

Reputation: 4912
• Posts: 15,348
• Joined: 25-December 09

## Re: Can I have any feedback on my HapSad Program?

Posted 04 December 2014 - 09:18 AM

Decrease your loop by one iteration, then print the last number, with the period, after the loop.

Jim