Question

The second assignment will use C++ syntax and control flow stuctures (e.g. loops, switch statements, functions,...

The second assignment will use C++ syntax and control flow stuctures (e.g. loops, switch statements, functions, const int variables, structs, the standard library, enums, array, etc.) to create a pattern drawing application. A user of your application will be able to select a shape to draw that will be printed out to standard output using std::cout. This application will draw a fixed size 8x8 pattern. The patterns your application will support are: filled, half-left filled, banded, checkered, a square, an x and a lower left triangle. Your C++ application code must have the following components. 1. A file called A02.cpp with a main(. . . ) function and the comments shown below with your name, student id, and submission date: // // Name : YOUR_FIRST & LAST_NAME // SSID : YOUR_STUDENT_ID // Assignment #: ASSIGNMENT_2 // Submission Date : DATE_YOU_SUBMIT // // Description of program : // .... your code goes here 2. An enum Shapes describing the patterns to draw and an enum Colors with colors black & white. enum class Shapes { FILLED , HALF_RIGHT , BANDED , CHECKERBOARD , SQUARE , X , LOWER_LEFT }; enum class Colors { BLACK , WHITE }; 3. A struct type called Pixel with a single enum Colors data member. struct Pixel { Colors color ;}; 4. An array of Pixel structs to create a pixel buffer 5. WIDTH and HEIGHT integer constants of the pattern. To create, use either #define or const int at global scope. These can be used to initialize the Pixel array described in step 4. 6. At least two functions with the following prototypes: void CreateShape ( Pixel p [] , int nPixels , Shapes shape ); void Draw ( Pixel p [] , int nPixels ); Where p is an array of Pixel structs; nPixels is the number of pixels in the pattern (in our case 8x8); and shape is a Shapes enum. 7. The CreateShape(. . . ) function will set the colors in the Pixel array. For example, if the filled pattern has been selected by the user, then all the pixel colors will be set to Colors::BLACK. 8. The Draw(. . . ) function will output ’|||’ for a pixel that is Colors::WHITE or three spaces “ “ for a pixel color that is Colors::BLACK. 9. The program will have a loop driven menu system to ask a user which shape to draw and ’q’ to exit. See the example output provided in this handout. Example Output: >A02.exe Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: filledEnter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: half_right Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: banded Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: checkered Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: square Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit. Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: lower_left Enter in a shape to draw: filled, half_right, banded, checkered, square, x, or lower_left)? Enter ’q’ to exit.: q goodbye

Homework Answers

Answer #1

Code:

//

// Name : YOUR_FIRST & LAST_NAME

// SSID : YOUR_STUDENT_ID

// Assignment #: ASSIGNMENT_2

// Submission Date : DATE_YOU_SUBMIT

//

// Description of program :

//

#include <iostream>

#include <string>

#include <algorithm>

#define WIDTH 10

#define HEIGHT 10

#include <iomanip>

using namespace std;

//define enumerator shapes

enum Shapes {

FILLED,

HALF_RIGHT,

BANDED,

SQUARE,

CHECKERBOARD,

LOWER_LEFT,

X

};

//defining enumerator colrs

enum Colors {

WHITE,

BLACK

};

//defining structre for pixels to store the pattern

struct Pixel {

Colors value;

};

//crate shape to load

void CreateShape(Pixel p[], int nPixels, Shapes shape) {

switch (shape) {

case FILLED:

    for (int i = 0; i < nPixels * nPixels; i++)

      p[i].value = WHITE;

break;

case HALF_RIGHT:

for (int i = 0; i < nPixels * nPixels; i++) {

    //checking for borders for 0, nPixels

    int s;

    if (i >= 10) s = i % 10;

    else s = i;

    if (s >= nPixels / 2) {

      p[i].value = WHITE;

    } else {

      p[i].value = BLACK;

    }

}

break;

case LOWER_LEFT:

for (int i = 0; i < nPixels * nPixels; i++) {

    //checking for borders for 0, nPixels

    if (i / nPixels == nPixels - 1 || i % nPixels == 0) {

      p[i].value = WHITE;

    } else {

      p[i].value = BLACK;

    }

}

break;

case CHECKERBOARD:

    for (int i = 0; i < nPixels * nPixels; i++) {

      //check whether row is even or not

      if ((i / nPixels) % 2 == 0) {

        // alternating colors

        if (i % 2 == 0)

          p[i].value = WHITE;

        else

          p[i].value = BLACK;

      } else {

        //alternating colors

        if (i % 2 == 0)

          p[i].value = BLACK;

        else

          p[i].value = WHITE;

      }

    }

    break;

case SQUARE:

    for (int i = 0; i < nPixels * nPixels; i++) {

      //checking for borders for 0, nPixels

      if (i / nPixels == 0 || i / nPixels == nPixels - 1 || i % nPixels == 0 || i % nPixels == nPixels - 1) {

        p[i].value = WHITE;

      } else {

        p[i].value = BLACK;

      }

    }

    break;

case BANDED:

    for (int i = 0; i < nPixels * nPixels; i++) {

      //alternating rows

      if ((i / nPixels) % 2 == 0) {

        p[i].value = WHITE;

      } else {

        p[i].value = BLACK;

      }

    }

    break;

case X:

    for (int i = 0; i < nPixels * nPixels; i++) {

      //get diagonals

      if ((i / nPixels) == (i % nPixels) || (i / nPixels) + (i % nPixels) == nPixels - 1) {

        p[i].value = WHITE;

      } else {

        p[i].value = BLACK;

      }

    }

    break;

}

}

void Draw(Pixel p[], int nPixels) {

for (int i = 0; i < nPixels * nPixels; i++) {

    if (i % nPixels == 0 && i != 0)

      cout << endl;

    if (p[i].value == WHITE)

      cout << "|||";

    else

      cout << setw(3) << "";

}

cout << endl;

}

int main() {

Pixel p[WIDTH * HEIGHT];

string userChoice;

do {

cout << "\nCheckerboard, Square, Lower_left, Filled, Half_Right, Banded, X, q to quit: ";

cin >> userChoice;

                transform(userChoice.begin(), userChoice.end(), userChoice.begin(), ::tolower);

    if (userChoice == "checkerboard")

      { CreateShape(p, 10, CHECKERBOARD); Draw(p,10); }

    else if (userChoice == "square")

      { CreateShape(p, 10, SQUARE); Draw(p,10); }

    else if (userChoice == "lower_left")

      { CreateShape(p, 10, LOWER_LEFT); Draw(p,10); }

    else if (userChoice == "filled")

      { CreateShape(p, 10, FILLED); Draw(p,10); }

    else if (userChoice == "half_right")

      { CreateShape(p, 10, HALF_RIGHT); Draw(p,10); }

    else if (userChoice == "banded")

      { CreateShape(p, 10, BANDED); Draw(p,10); }

    else if (userChoice == "x")

      { CreateShape(p, 10, X); Draw(p,10); }

    else if (userChoice == "q")

      return 0;

    else

      cout << "!!Invalid Choice!!";

} while (userChoice != "q");

return 0;

}

Output:


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT