Array Solution - Half and half, 10 per line

Problem : Declare and initialize an integer array having 50 elements. The value of the first 25 elements to be the square of the index element and the remaining 25 elements, the values to be treble the index element. Further, the results are to be printed 10 per line.

This was a problem put forward from this thread.

My solution.

// array-fifty.cpp  --  Assign elements, print 10 per line.
// http://www.daniweb.com/forums/thread102743.html
// Steven Taylor
// 30 Dec, 2007.

#include <iostream>
using namespace std;

int main()
{
    int alpha[50];
    for (int i=0;i<50;i++)
    {
        if (i<25)
            alpha[i] = i * i;
        else
            alpha[i] = i * 3;
        if (i % 10 == 0)
            cout << "\n" << alpha[i] << "\t";
        else
            cout << alpha[i] << "\t";
    }

    // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    cin.get();
    return 0;
}

As can be seen the code is relatively simple. The difficult part was working out how to print 10 per line. In the end, the modulus function was the only way I could think of to achieve this requirement. If there's another way, I'm open to suggestions.

As I write this another suggestion popped up suggesting that a condition is not necessary regarding the first 25 and second 25 elements. Suggestion as follows:

for(int i =0; i<25; i++) {
   alpha[i] = i * i;
   alpha[49-i] = (49-i) * 3;
}

Clever. I wonder how long it would have taken me to think of this approach. It's all a part of the learning experience.

Steve
Joined: 27 Mar 2008
User offline. Last seen 20 hours 18 min ago.
Still two loops

The suggestion where no test condition is used to assign the elements, although clever, still requires a second loop merely to iterate through the entire array, in order to output 10 elements per line. The particular code snippet cannot simply replace my original for loop as it only iterates 25 times and therefore the following modulus condition only checks against 25 elements.

Steve
Joined: 27 Mar 2008
User offline. Last seen 20 hours 18 min ago.
Slight improvement - no need for (if / else) just (if)
// array-fifty.cpp  --  Assign elements, print 10 per line.
// http://www.daniweb.com/forums/thread102743.html
// Steven Taylor
// 30 Dec, 2007.

#include <iostream>
using namespace std;

int main()
{
    int alpha[50];
    for (int i=0;i<50;i++)
    {
        if (i<25)
            alpha[i] = i * i;
        else
            alpha[i] = i * 3;
        if (i % 10 == 0)
            cout << endl;
        cout << alpha[i] << "\t";
    }

    // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    cin.get();
    return 0;
}

Basically changed the if (i % 10 == 0) snippet to be a straight if test condition. Previously it was an if else test condition. Saved one line of code.

n/a
Steve
Joined: 27 Mar 2008
User offline. Last seen 20 hours 18 min ago.
wondering why

Following is an anonymous comment - from the old site

why did you use "/t"? and what does it mean? I tried the program with the new line(/n? but the numbers didn't output the correct way...?? why did /t work?

Steve
Joined: 27 Mar 2008
User offline. Last seen 20 hours 18 min ago.
\t is a tab character

\t is a tab character. In using this it merely positions each number at a tab position. For the rows/ines that follow all the numbers line up nicely.

If you were to insert \n in place of \t the object of the program wouldn't be achieved as each number would be printed on a separate line.
Try replacing the \t with " " (at least one space between double quotes). The numbers will be correctly positioned along the line but they will not line up nicely with the rows/lines that follow.

For further reference it's the backslash character \ that causes this effect. This is also known as an escape character. It's purpose, when used within a double-quoted string is NOT to print the character that follows it but to issue a command. So when the program sees \n within a double quoted string, it doesn't print 'n' it issues a newline.

Thanks for dropping by and hoped this helped.

free hit counter