using System; using System.Collections.Generic; using System.Linq; using System.
ID: 3716284 • Letter: U
Question
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MapPlotting
{
class Program
{
public static void Main()
{
// Write your program here
// ...
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
Explanation / Answer
Required C# code. Please let me know in case anything is not clear.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MapPlotting
{
class Program
{
public static void Main()
{
// Write your program here
// ...
bool[,] map = new bool[20,40];
int x=-1, y=-1;
string isMore = "y";
while (isMore == "y") {
Console.WriteLine("Place a marker at which X coordinate? (0-39):");
bool isValidX = false;
while (!isValidX) {
if (int.TryParse(Console.ReadLine(), out x)) {
if (x<0 || x>39) {
// out of range
Console.WriteLine("Out of range.");
Console.WriteLine("Place a marker at which X coordinate? (0-39):");
} else isValidX = true;
} else {
// invalid input
Console.WriteLine("Invalid input.");
Console.WriteLine("Place a marker at which X coordinate? (0-39):");
}
}
bool isValidY = false;
Console.WriteLine("Place a marker at which Y coordinate? (0-19):");
while (!isValidY) {
if (int.TryParse(Console.ReadLine(), out y)) {
if (y<0 || y>19) {
// out of range
Console.WriteLine("Out of range.");
Console.WriteLine("Place a marker at which Y coordinate? (0-19):");
} else isValidY = true;
} else {
// invalid input
Console.WriteLine("Invalid input.");
Console.WriteLine("Place a marker at which Y coordinate? (0-19):");
}
}
map[y,x] = true;
Console.WriteLine("More? (y/n)");
isMore = Console.ReadLine();
while (isMore!="y" && isMore!="n") {
// invalid value
Console.WriteLine("Please answer with y or n.");
Console.WriteLine("More? (y/n)");
isMore = Console.ReadLine();
}
}
// draw the map
for (int i=0; i<20; i++) {
for (int j=0; j< 40; j++) {
if (map[i,j]) {
Console.Write("X");
} else Console.Write(".");
}
Console.WriteLine();
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
Sample Input:
sd
4
27
-3
2
y
3
2
y
5
1
y
0
0
a
n
Sample output:
Place a marker at which X coordinate? (0-39):
Invalid input.
Place a marker at which X coordinate? (0-39):
Place a marker at which Y coordinate? (0-19):
Out of range.
Place a marker at which Y coordinate? (0-19):
Out of range.
Place a marker at which Y coordinate? (0-19):
More? (y/n)
Place a marker at which X coordinate? (0-39):
Place a marker at which Y coordinate? (0-19):
More? (y/n)
Place a marker at which X coordinate? (0-39):
Place a marker at which Y coordinate? (0-19):
More? (y/n)
Place a marker at which X coordinate? (0-39):
Place a marker at which Y coordinate? (0-19):
More? (y/n)
Please answer with y or n.
More? (y/n)
X.......................................
.....X..................................
...XX...................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
Press enter to exit.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.