6 Replies - 252 Views - Last Post: 04 December 2014 - 09:18 AM Rate Topic: -----

#1 into_2_c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
/// HapSad Number Investigation
/// CS7A

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

#include "std_lib_facilities.h"

class HapSad {
    private:
        bool happy;
        unsigned x, cycle_entry, dist_to_cycle, start, stop;
        vector<HapSad> longest_sad_cycle, current_sad_cycle, hs;
    public:
        vector<unsigned int>sequence;
        unsigned cycle_length;
        void show_sequence();
        bool repeated_value(unsigned x, vector<unsigned> v);
        HapSad(unsigned x);
        HapSad() {};        /// NEEDED to run the class hapSad
};

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

void HapSad::show_sequence()    {
    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;
}

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

HapSad::HapSad(unsigned seed)  {
    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);
    }
}

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

void find_longest_sad_cycle(vector<HapSad>& longest_sad_cycle, vector<HapSad>& current_sad_cycle, vector<HapSad>hs, int j)  {
    if(hs[j].cycle_length==8)   {
        current_sad_cycle.push_back(hs[j]);
        if(current_sad_cycle.size()>=longest_sad_cycle.size())
            longest_sad_cycle=current_sad_cycle;
    }
    else
        current_sad_cycle.clear();
}

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

HapSad find_longest_sequence(HapSad longest_sequence,vector<HapSad>hs,int j)    {
        if(hs[j].sequence.size()>=longest_sequence.sequence.size())
            longest_sequence=hs[j];
        return longest_sequence;
}

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

int main()  {
    unsigned start, stop;
    HapSad longest_sequence;
    vector<HapSad> longest_sad_cycle, current_sad_cycle, hs;
    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.push_back(HapSad(i));
        hs[j].show_sequence();
        cout << "Cycle length= " << hs[j].cycle_length <<'\t';  ///tab sequence to right of cycle length.
        find_longest_sad_cycle(longest_sad_cycle,current_sad_cycle,hs,j);
        longest_sequence = find_longest_sequence(longest_sequence,hs,j);
}
cout <<'\n'<< "------------------------------------------------------------------------" << '\n';

    if(longest_sad_cycle.size()>0)
        cout<< "\nCycle From " <<longest_sad_cycle[0].sequence[0] << " to " << longest_sad_cycle[longest_sad_cycle.size()-1].sequence[0]
            << ". " << "\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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 10479
  • View blog
  • Posts: 40,435
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6193
  • View blog
  • Posts: 23,920
  • 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;.
Was This Post Helpful? 0
  • +
  • -

#4 into_2_c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 4595
  • View blog
  • Posts: 14,504
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 into_2_c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
122	        cout<< "\nCycle From " <<longest_sad_cycle[0].sequence[0] << " to " << longest_sad_cycle[longest_sad_cycle.size()-1].sequence[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?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 4595
  • View blog
  • Posts: 14,504
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1