Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

simple array

This topic is 2625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well I am writing a simple array.I want my output to be "RedGreenBlueRedGreen"
"BlueRedGreenBlueRed"
"GreenBlueRedGreenBlue"

right now I get "RedGreenBlueRedGreenBlueRedGreenBlueRedGreenBlueRedGreenBlue"

my code is the following

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<iostream>

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]using[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]namespace[/font][/font][/font][font="Consolas"][font="Consolas"] std;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] space;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#define[/font][/font][/font][font="Consolas"][font="Consolas"] WIDTH 3

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#define[/font][/font][/font][font="Consolas"][font="Consolas"] HEIGHT 5

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] bricks[HEIGHT][WIDTH];

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] main()

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][font="Consolas"]([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] n=0; n< HEIGHT; n++)

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][font="Consolas"]([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] m=0; m< WIDTH; m++)

{

bricks[n][m]=m % 3;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](bricks[n][m]==0)

{

cout << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"Red"[/font][/font][/font][font="Consolas"][font="Consolas"];

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](bricks[n][m]==1)

{

cout << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"Green"[/font][/font][/font][font="Consolas"][font="Consolas"];

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](bricks[n][m]==2)

{

cout << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"Blue"[/font][/font][/font][font="Consolas"][font="Consolas"];

}

}

}

cin >> space;

}

?

[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
cout << "RedGreenBlueRedGreen" << std::endl;
cout << "BlueRedGreenBlueRed" << std::endl;
cout << "GreenBlueRedGreenBlue" << std::endl;

There you have the output you want ;)

I don't know exactly what you are doing but I guess you are practising programming so you want to keep the loops and arrays. Put the following code at the end of the innermost loop and you will get the output you want.
if ((n * WIDTH + m) % 5 == 4)
{
cout << std::endl;
}

Share this post


Link to post
Share on other sites
Well, first of all, your post suggests you want a width of 5 and a height of 3, but your code suggests the opposite.

All you really have to do is put this outside the } of the for() loop that counts up to width using m:

cout << "\n";

Be sure to put it inside the for() loop that counts up to height, and you should be good.


Note that there are plenty of ways to improve on your code; if you're interested, say the word :-)

Share this post


Link to post
Share on other sites
ApochPiQ, now is you chance! Go on and tell him about #define, using namespace std, void main and the unnecessary use of arrays.

Share this post


Link to post
Share on other sites
You might find this article useful, but really it just boils down to this:

  • Practice
  • Be willing to make mistakes
  • Try to learn from those mistakes
  • Rinse, repeat

    So you're already on the right track :-)



    Here's a few suggestions:

    • You don't use the space variable outside of main(), so it shouldn't be global but instead local to main(). Keep things close to where you use them, this helps with organization.
    • Ditto for bricks.
    • Read this about #define - you should use a const variable instead. (Actually, read as much of that site as you feel comfortable trying to digest - it's got some great info!)
    • Try to get into the habit of being very consistent with indenting and spacing in your code. It helps make it easier to read. I don't know how much of this you already do, though, because it looks like the forum ate some of your formatting (sorry!)
    • Your three if() statements are mutually exclusive, ie. they can never all be true. Therefore you can use "else" to make the code clearer and a little more efficient
    • Magic numbers are bad; you've got a good start by using WIDTH and HEIGHT but you can extend this to the colors as well. Look up "enum" for some ideas
    • void main() is illegal. You should write int main() instead. You don't have to return anything from main if you don't want to.

      Taking that into account, we end up with code that looks like this:

      #include <iostream>

      using namespace std;

      int main()
      {
      enum Colors
      {
      COLOR_RED,
      COLOR_GREEN,
      COLOR_BLUE,
      COLOR_NUM_COLORS
      };

      const unsigned HEIGHT = 3;
      const unsigned WIDTH = 5;

      int bricks[HEIGHT][WIDTH];

      for(int n = 0; n < HEIGHT; n++)
      {
      for(int m = 0; m < WIDTH; m++)
      {
      bricks[n][m] = m % COLOR_NUM_COLORS;

      if(bricks[n][m] == COLOR_RED)
      {
      cout << "Red";
      }
      else if(bricks[n][m] == COLOR_GREEN)
      {
      cout << "Green";
      }
      else if(bricks[n][m] == COLOR_BLUE)
      {
      cout << "Blue";
      }
      }

      }

      int space;
      cin >> space;
      }



      Bonus exercise: look up the "switch" statement and rewrite this program to use it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!