Write a program that takes an input N, and for the input, print out an ascii X that is N by N represented by asterisks. Good Luck!
Examples:printX(3);
* *
*
* *printX(7);
* *
* *
* *
*
* *
* *
* *
>>59241990
Sorry boys -- Ive made a mistake.printX(7);
* *
* *
* *
*
* *
* *
* *
I'm not doing it unless a bird threatens to stab me
>>59242318
Not going to bother writing code but the process is trivial
Looping i over j, Asterisks go in the j = i mod N and it's symmetrical position(N-1 - i mod N).
Everything else is space.
#include stdio.h
int a = 0;
int b = 0;
function printX(a, b){
printf("%s", "Input the value of A");
scanf("%s", a);
printf("%s", "Input the value of B");
scanf("%s", b);
gotoxy(x);
}
void main(){
printX(7);
getch()
return 0;
}
let printX = n => console.log(Array(n).fill().map((a,b)=>Array(n).fill().map((x,y)=>([b,n-b-1].includes(y))?'*':' ').join('')).join("\n"))> printX(7)
* *
* *
* *
*
* *
* *
* *
>>59241990
what about users putting 1 or 2 in?
>>59242914
doesnt compile
>>59242941
ugly as shit kys
>>59242914
What programming language is this?
>>59241990
what should print(even_number) print?
>>59242871
well here it is in Dlangimport std.stdio;
import std.conv;
void asciiX(int size)
{
for(int y = 0; y < size; y++)
{
for(int x = 0; x < size; x++)
{
if(x == y % size || x == size - 1 - y % size)
{
write("*");
}
else
{
write(" ");
}
}
writeln();
}
}
void main(string[] args)
{
if(args.length < 2)
{
writeln("Not enough arguments");
return;
}
asciiX(to!int(args[1]));
}
Sadly I don't know enough Dlang yet to simplify it.
Please rate, I'm just a haskell beginner, took me approx 10 minutes because I do not code in haskell that often...
Also, it feels very hacky.setValue :: Int -> (Int, Int) -> String
setValue n (a, b)
| a == b = "X"
| a == (n - (b + 1)) = "X"
| otherwise = " "
genArray :: Int -> Int -> [(Int, Int)]
genArray 0 _ = [(0, 0)]
genArray i n = genArray (i - 1) n ++ [(i `mod` n, i `div` n)]
breakLines :: Int -> [String] -> String
breakLines _ [] = ""
breakLines n xs = unwords (take n xs) ++ "\n" ++ breakLines n (drop n xs)
main = do
n <- readLn :: IO Int
putStr $ breakLines n $ map (setValue n) $ genArray (n * n - 1) n
>>59241990def drawX(n):
output = ""
for x in range(0,n):
line = list(n*' ')
line[x] = '*'
line[n-1-x] = '*'
output+="".join(line)
if x!=n-1:
output+= "\n"
return output
#include <iostream>
#include <string>
std::string printX(unsigned w) {
if (w == 1) return "x\n";
std::string ret = "";
for (unsigned i = 0; i <= w; ++i) {
for (unsigned j = 0; j <= w; ++j) {
if (i == j || i + j == w) {
ret += "x";
} else {
ret += " ";
}
if (j == w) {
ret += '\n';
}
}
}
return ret;
}
int main() {
unsigned w;
while (true) {
std::cout << "Enter the width of the x you would like to print: ";
std::cin >> w;
if (std::cin.fail() || w == 0) {
std::cin.clear();
while (std::cin.get() != '\n') {}
std::cout << "Invalid number." << std::endl;
} else {
break;
}
}
std::cout << printX(w);
return 0;
}
private static void Main(string[] args)
{
var a = 0;
var b = 0;
Console.Write("Input a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input b: ");
b = Convert.ToInt32(Console.ReadLine());
for (int x = 0; x < a; x = x + 1)
{
for (int y = 0; y < b; y = y + 1)
{
if (x == y % a || x == a - 1 - y % b)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
function printX(n){
var s='';
for(var i=0,st=0,n2=n*2-1;i++<n;st+=2,s+='\n')
for(var j=0;j<=n2;j++)
s+=(j==st||j==(n2-1-st))?'*':(j!=n2)?' ':'';
return console.log(s);
}