Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Short Programs


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Thanks..
    38 weeks 3 days ago
  • Hmmm,Interesting one,thx for
    39 weeks 4 days ago
  • Buyer beware
    40 weeks 3 days ago
  • REPLY:Actual Processes Involved
    40 weeks 4 days ago
  • Back to Ruby
    49 weeks 6 days ago
  • Links provided
    1 year 13 weeks ago
  • Module for wordpress to Drupal 6.x
    1 year 13 weeks ago
  • The wordpress plugin looks
    1 year 14 weeks ago
  • Good point..... You're
    1 year 14 weeks ago
  • Many thanks. If I'm going to
    1 year 14 weeks ago

New forum topics

  • Imported posts only visible to user1
  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
more

Sponsored links

Steve's Stuff
All about my running and from time to time other stuff

Improve Memory
All about memory techniques, excellent and relevant articles.

Array Solution - Half and half, 10 per line

Submitted by Steve on Sat, 29 Dec, 2007 - 23:40
  • C++
  • array
  • modulus

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.

‹ Enhancement # 3 - Trap number followed by non numeric up Calculate Pace (Running Program) ›
  • Printer-friendly version
  • Add new comment
  • 2459 reads
Sat, 29 Dec, 2007 - 23:57
#1
Steve
User offline. Last seen 1 week 3 days ago. Offline
Joined: 27 Mar 2008
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
Steve's Stuff
Improve Memory

Top
  • reply
Sun, 30 Dec, 2007 - 22:47
#2
Steve
User offline. Last seen 1 week 3 days ago. Offline
Joined: 27 Mar 2008
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
Top
  • reply
Sat, 23 Feb, 2008 - 11:26
#3
Steve
User offline. Last seen 1 week 3 days ago. Offline
Joined: 27 Mar 2008
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
Steve's Stuff
Improve Memory

Top
  • reply
Sat, 23 Feb, 2008 - 12:49
#4
Steve
User offline. Last seen 1 week 3 days ago. Offline
Joined: 27 Mar 2008
\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.

Steve
Steve's Stuff
Improve Memory

Top
  • reply
Wed, 24 Jun, 2009 - 16:10
#5
Guest
Thanks..

The first 25 and second 25 elements are benefit for me.

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

Thanks for sharing the code. cissp certification

Top
  • reply
  • Post reply

 Subscribe in a reader

free hit counter


RoopleTheme