[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/dpc/ - Daily programming challenge

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 18
Thread images: 4

File: oldai1.jpg (128KB, 533x538px) Image search: [Google]
oldai1.jpg
128KB, 533x538px
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.
>>
File: 86478469.jpg (429KB, 1589x646px) Image search: [Google]
86478469.jpg
429KB, 1589x646px
#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?
>>
>>59244865
F
>>
>>59242871
well here it is in Dlang

import 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
>>
>>59241990
def 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;
}
>>
File: file.png (7KB, 305x263px) Image search: [Google]
file.png
7KB, 305x263px
        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);
}
Thread posts: 18
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.