Good Morning everyone,

My intention is not to get someone to do the whole code to help me but I'm currently learning c++ and I'm working on a homework project to create 10 small programs inside of 1 code and I'm having a hard time doing the last one. It seems this is related to a non Fibonaci numerical series .

The program needs to print the first 200 elements following a conditional numerical series 1-1-3-6-8-8-10-20 where:

a) first 2 digits are 1
b) 3 rd digit is the result of the 2nd +2
c) 4rd digit is the result of the 3rd digit x2
d) 5th digit is the result of the 4th + 2
e) 6th digit is the same as the 5th

Once the above process is over the cycle will be restarted starting on letter b , I've managed to acomplish the letter a by using if condition inside the for loop but I've exhausted all of my knowledge to do points b,c,d and e without suceeding. Below is my code:

int main()
        int Num1 = 200, a = 0, b= 0, c=0;

        for (int i = 1; i <= Num1; i++)
            if (i == 1)
                cout << "  1, ";
            if (i == 2)
                cout <<"1, ";

             // This are comments of operations I've tried and I get the results I need but 200* 4 = 800 results
            //next = a + b
            //z = y + 2;
            //cout << " " << z << ","; //It will print 3
            //z = z * 2;
            //cout << " " << z << ",";//It will print 6
            //z = z + 2;
            //cout << " " << z << ",";//It will print 8
            //z = z;
            //cout << " " << z << ",";//It will print 8

            //y = z;


        cout << "\n\n";
        return 0;

My first suggestion - stop using a, b, c, and i as variable names. Give them meaning, they will help.

My second suggestion is to consider using a placeholder as a loop helper - just because you are looping though "i" doesn't mean you have to use it for anything other than how many iterations you have gone through.


int iLup = 0;
int iStep = 1;
int iMax = 200;
int iVal = 1;
for (; iLup < iMax; iLup++)
     if (iStep == 1) //(this can be step "a")
        //concatenate stuff
     else if (iStep == 2) //(this can be step "b")
       //do some math and concat...
     else if (...) //(etc....)

     if (iStep > 6)
        //do something special here ;)

using std::cout is fine as a buffer... but if you use the method I suggested you are going to have do either do an additional if check to see if iLup == 199 to not append the final comma, or get some magical buffery (yay char arrays!) and build your string into memory before outputting the string. If you do that, rememeber that all "strings" in c/c++ must be null terminated :)

You will probably need to learn about itoa() and strcat_s() and either learn about pointers (to point to the last comma and convert it to null (0)).

Alternatively, you can learn more about the std::string (#include <string>) object and use that instead. Either one should be acceptable for academic work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.