Shapes with structs and arrays

Hello ladies and gents, my name is xeuz and this is my first post. I'm trying to figure out how to connect the structs to arrays so that i can make shapes starting with an initial grid then changing the colors based on which shape is inputed. any advice would help, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
#include <string>

enum class Shapes 
    { FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI };

enum class Colors 
    { BLACK, WHITE };

struct Pixel { Colors color; };
    struct Pixel b = { Colors BLACK }
    struct Pixel w = { Colors WHITE }

void CreateShape(Pixel p[], int nPixels, Shapes Shape);
    const int Width, Height = 0;
        for (Width = 0; Width < 10; width++)
void Draw ( Pixel p[], int nPixels);

    int iShape [10] = {1,2,3,4,5,6};

Pixel p[2];

int main(){

}
You might do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
enum class Shapes 
    { FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI };

enum class Colors 
    { BLACK, WHITE };

const int ShapeSizeX = 4;
const int ShapeSizeY = 4;

const Colors FILLEDShapeColors[] = {Colors::WHITE, Colors::WHITE, ....}; // ShapeSizeX * ShapeSizeY

const int GridSizeX = 4;
const int GridSizeY = 4;

Shapes GridShapes[GridSizeX][GridSizeY] = { {Shapes::FILLED, ... }, ... }; // Initial grid / GridSizeX * GridSizeY

void DrawShape(const Colors colors[])
{
...
}

void DrawShape(const Shapes shape)
{
  switch(shape)
  {
    case Shapes::FILLED:
      DrawShape(FILLEDShapeColors);
      break;
...
  }
}

void DrawGrid()
{
  for(int i = 0; i < GridSizeX; ++i)
  {
    for(int j = 0; j < GridSizeY; ++j)
    {
      DrawShape(GridShapes[i][j]);
    }
  }
}

in main()
{
  DrawGrid();
  return 0;
}
Just to give you an idea how to start.
Hello xeuz,

You have a good start.

Using an enum class these lines:
1
2
struct Pixel b = { Colors BLACK }
struct Pixel w = { Colors WHITE }


Should be written as:
1
2
struct Pixel b = { Colors::BLACK }
struct Pixel w = { Colors::WHITE }

See http://www.cplusplus.com/doc/tutorial/other_data_types/#enum_class

Line 22 should be inside "main". It is best to avoid using global variables like this.

An array like this will work, but a std::vector<Pixel> pixel; is a better choice.

"main" should contain the code to call the functions and direct the program flow.

Line 16 const int Width, Height = 0;. Both variables, as with all regular variables, should start with a lower case letter. Upper case letters are used for "class"es and "struct"s. "width" has no value assigned and "height" is set to zero. Both being defined as a "const" these values can never be changed. The for loop should be giving you an error because you are trying to change a variable that can not be changed. I believe what you want here is: int width{}, height{};. The empty {}s will initialize the variables to zero or you could put a number inside the {}s.

Unless you have a need for "width" outside of the for loop it is better to write the for loop as:for (int width = 0; width < 10; width++). And be careful you started with "Width", but ended with "width++. The difference between the capital and lower case "W" is a completely different variable.

I did not catch this at first, but line 11 ends with a semi-colon which ends the "struct". The next two lines being indented gave the impression that they should belong to the "struct", but they do not.

I think what you meant is:
1
2
3
4
5
struct Pixel
{ 
    struct Pixel b = { Color::BLACK }
    struct Pixel w = { Colors::WHITE }
};

With the {}s the "=" is not needed.

Line 15 is a nice prototype. Lines 16 and 17 just because they are indented does not make them part of the function. The semi-colon at the end of line 15 makes it a prototype not a function. Again I think what you meant is:
1
2
3
4
5
6
7
8
9
10
void CreateShape(Pixel p[], int nPixels, Shapes Shape)
{
    int width, weight = 0;

    for (int width = 0; width < 10; width++)  // <--- better way to write for loop.
    {
        // <--- needs some code here.
    }  //  End for loop.
}  // End function.

The {}s in column 1 make it a function. The for loop needs to do something inside the {}s. And line 3 is not really needed.

And you have the same problem with lines 18 and 20.

As I said you have a good start, but need to fix the problems and add something to "main" to make use of all this.

Hope that helps,

Andy
Topic archived. No new replies allowed.