Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Write a program that displays an arbitrary sized grid in the console using a character that user picks from a list. The program should not exit until the user presses Esc. Accepted file types:...

1 answer below »

Write a program that displays an arbitrary sized grid in the console using a character that user picks from a list. The program should not exit until the user presses Esc.

Accepted file types:

  • application/x-csh.cs

Sample input (user input isbold, it can be anything):

Press ESC to exit, any other key to continue.
Grid rows:5
Grid columns:5
Character options
1) @
2) X
3) 0
Your choice:2


Sample output:

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX


You will need:

  • Get user input, this can be done withConsole.ReadLine(). Don't forget that console input is treated as string, so you'll need to cast it into int, like so:
    Console.Write("Enter number: ");
    int num = int.Parse(Console.ReadLine());
    Console.Write(Environment.NewLine);
    Console.WriteLine("You entered: {0}", num);

    Note:
    Console.Write()writes without newline.Console.WriteLine()automatically inserts a newline.Environment.NewLinemanually inserts a newline.
  • A conditional statement (eitherif-elseorswitch) to handle the character options.
  • A class to hold the grid. It should have parameters for width, height and character used to draw it. As well as getter and setter methods to set these parameters. Refer toclass examplefrom previous assignment if you get stuck here.
  • A method within the grid class that draws it. You'll have to use nested for loops to generate it. Something like this:
    // Outer loop
    for(int i=0; i{
    // Inner loop
    for(int j=0; j{
    // Other stuff here
    }
    }

    Don't forget that loops can take variables as arguments, so you should use the input you get from the user to set them up.
  • Within theMain()class you should have an infinitewhileloop that doesn't stop until Esc is pressed. The easiest way to do so is to create aboolvariable before the loop an set it tofalse. Thewhilecondition should check that that variable is set tofalse, otherwise it will exit. Within the loop, at the top, have anifstatement that sets theboolvariable totruewhen Esc is pressed. Like so:
    ConsoleKey inpt = Console.ReadKey().Key;
    if(inpt == ConsoleKey.Escape)
    {
    // Set your bool to true here
    }
    else
    {
    // Get user input, initialize your class and call the draw grid method here
    }

    Note:
    ConsoleKeyis a special data type that holds key presses.Console.ReadKey().Keyis a method that gets key presses from the keyboard.

Hints:

  • Don't try to do it all at once. Break the problem down into smaller chunks, make them work, then integrate them together. E.g: start by just drawing a grid without user input or a class in Main, now rewrite it as a class and test it by tweaking variables when you initialize it, move on to user input, and so on. Programming is an iterative process, you will never be writing code from top to bottom without going back and forth to tweak it.
  • Draw it out on paper either as a diagram or pseudo code. It's not required that you submit these but they'll help to get your head around the problem. They don't have to follow any specific style, as long as you understand it yourself it's ok.
Answered Same Day Jul 13, 2021

Solution

Neha answered on Jul 14 2021
145 Votes
using System;
class PrintGrid {
static void Main() {
int rows;
int column;
int choice;
string character = "";
ConsoleKeyInfo k;
while (true) {
Console.WriteLine("Press ESC to exit, any other key to continue.");
k = Console.ReadKey(true);
if (k.Key == ConsoleKey.Escape)

eak;
else {
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here