Quick, make a program that outputs an "ASCII art" cross of asterisks in your favourite language!

Quick, make a program that outputs an "ASCII art" cross of asterisks in your favourite language!

Other urls found in this thread:

github.com/nim-lang/Nim/blob/devel/todo.txt#L1-L10
amazon.com/Programming-Artificial-Intelligence-International-Computer/dp/0321417461
twitter.com/SFWRedditGifs

---
org 0x7c00
bits 16

jmp 0x0000:fix_cs
fix_cs:
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7bf0
sti

mov si, cross
call print

jmp $

print:
push ax
push si
mov ah, 0x0e
.loop:
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
pop si
pop ax
ret

cross:
db '* *', 0x0d, 0x0a
db ' * * ', 0x0d, 0x0a
db ' * ', 0x0d, 0x0a
db ' * * ', 0x0d, 0x0a
db '* *', 0x0d, 0x0a, 0x00

times 510-($-$$) db 0
dw 0xaa55
---

Eh I have already done this months ago...
"workplace.stackexchange.com/questions/39004/which-weak-algorithmic-strategies-i-have-that-lead-me-to-failing-coding-this-sim"
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

v2

---
org 0x7c00
bits 16

jmp 0x0000:fix_cs
fix_cs:
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7bf0
sti

mov si, cross
call print

jmp $

print:
push ax
push si
mov ah, 0x0e
.loop:
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
pop si
pop ax
ret

cross:
db '*', 0x20, 0x20, 0x20, '*', 0x0d, 0x0a
db 0x20, '*', 0x20, '*', 0x20, 0x0d, 0x0a
db 0x20, 0x20, '*', 0x20, 0x20, 0x0d, 0x0a
db 0x20, '*', 0x20, '*', 0x20, 0x0d, 0x0a
db '*', 0x20, 0x20, 0x20, '*', 0x0d, 0x0a

times 510-($-$$) db 0
dw 0xaa55
---

#include
#include

#define spc(x, xs, c) \
((x) % (xs) == 0 ? (c) : ' ')

#define ch(m, i, c) \
(*((m) + (i)) != 0 ? (c) : ' ')

#define pc(c) putchar(c)


void character(size_t xw, size_t xl, size_t space, char c, const int* matrix)
{
size_t x;

for (x = 0; x < xw; ++x)
pc(spc(x, space, ch(matrix, 0, c)));

for (; x < xl; ++x)
pc(spc(x, space, ch(matrix, 1, c)));

for (; x < xl + xw; ++x)
pc(spc(x, space, ch(matrix, 2, c)));

for (; x < xl * 2; ++x)
pc(spc(x, space, ch(matrix, 3, c)));

for (; x < xl * 2 + xw; ++x)
pc(spc(x, space, ch(matrix, 4, c)));
}


void line(size_t xw, size_t xl, size_t space, const char* str, const int* matrix)
{
size_t x;

while (1) {
character(xw, xl, space, *str++, matrix);

if (*str == '\0')
break;

for (x = 0; x < space; ++x)
pc(' ');
}

pc('\n');
}


void draw(size_t size, size_t thick, size_t space, const char* str)
{
int matrix[] = {
1, 0, 1, 1, 1,
1, 0, 1, 0, 0,
1, 1, 1, 1, 1,
0, 0, 1, 0, 1,
1, 1, 1, 0, 1
};

size_t xw = thick * space;
size_t xl = size * space;
size_t yw = thick;
size_t yl = size;
size_t y;

for (y = 0; y < yw; ++y)
line(xw, xl, space, str, matrix);

for (; y < yl; ++y)
line(xw, xl, space, str, matrix + 5);

for (; y < yl + yw; ++y)
line(xw, xl, space, str, matrix + 10);

for (; y < yl * 2; ++y)
line(xw, xl, space, str, matrix + 15);

for (; y < yl * 2 + yw; ++y)
line(xw, xl, space, str, matrix + 20);
}


int main()
{
draw(3, 2, 2, "*");
return 0;
}

Only works properly for odd inputs but idgaf

First attempt
#include
#include
#include

using namespace std;


void starCross(int n) {
float half = (float)n * 0.5;

for (int i = 1; i

Not what was asked

plus it's not even angled correctly

[CODE]
PROGRAM DRAWTHING
IMPLICIT NONE
CHARACTER(LEN=1), PARAMETER :: SYMBOL="#"
CHARACTER(LEN=1), PARAMETER :: PADDING=" "
CHARACTER(LEN=1), DIMENSION(:,:), ALLOCATABLE :: THINGY
CHARACTER(LEN=30) :: FMT
INTEGER :: XSIZE,I
WRITE(*,"(A15)") "SIZE OF THINGY:"
READ(*,*) XSIZE
ALLOCATE(THINGY(XSIZE,XSIZE))
THINGY=PADDING
FORALL(I=1:XSIZE); THINGY(I,I)=SYMBOL; THINGY(XSIZE-I+1,I)=SYMBOL; ENDFORALL
WRITE(FMT,"('('I0'('I0,'A/))')") XSIZE,XSIZE
WRITE(*,FMT) THINGY
DEALLOCATE(THINGY)
ENDPROGRAM DRAWTHING
[/CODE]

oh ok
PROGRAM DRAWTHING
IMPLICIT NONE
CHARACTER(LEN=1), PARAMETER :: SYMBOL="#"
CHARACTER(LEN=1), PARAMETER :: PADDING=" "
CHARACTER(LEN=1), DIMENSION(:,:), ALLOCATABLE :: THINGY
CHARACTER(LEN=30) :: FMT
INTEGER :: XSIZE,I
WRITE(*,"(A15)") "SIZE OF THINGY:"
READ(*,*) XSIZE
ALLOCATE(THINGY(XSIZE,XSIZE))
THINGY=PADDING
FORALL(I=1:XSIZE); THINGY(I,I)=SYMBOL; THINGY(XSIZE-I+1,I)=SYMBOL; ENDFORALL
WRITE(FMT,"('('I0'('I0,'A/))')") XSIZE,XSIZE
WRITE(*,FMT) THINGY
DEALLOCATE(THINGY)
ENDPROGRAM DRAWTHING

...

what lang is dis

fortran

i had one but i fucked up

what do you mean

genx = function(xx){
for(v=0;v x x
> x x
> x x
> x x
> x
> x x
> x x
> x x
> x x

Somethings wrong but whatever

tsk tsk

This is really basic geometry. You've got a line that goes up linearly with a slop of one and a normal line to it that we want to intersect at the [(n-1)/2,(n-1)/2] point.

So y - (n-1)/2 = -(x-(n-1)/2)

Solving for y we get the other equation as y = n-1-x

#include

int main() {
int size;
scanf("%d",&size);

for(int y = .5;y

(defn print-cross [a]
(let [x (range a) y (dec a)]
(doseq [row x col x]
(print (if (or (= col row) (= col (- y row))) "*" " ")
(if (= y col) "\n" "")))))
(print-cross 10)
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
=> nil
(print-cross 9)
* *
* *
* *
* *
*
* *
* *
* *
* *

For brainlets: This is a list comprehension in clojure. May be even shorter, but fuck it, I'm supposed to work now

lol whats an asskey xD

You're going to continue to post in threads without using code tags?

My code is awful :

Quick and dirty.
#!/bin/sh

char='*'
frst=1
scnd=$1

for line in `seq $1`; do
for col in `seq $1`; do
if [ $col -eq $frst ]; then
echo -n "$char"
elif [ $col -eq $scnd ]; then
echo -n "$char"
else
echo -n ' '
fi
done
echo
(( frst += 1 ))
(( scnd -= 1 ))
done

import strutils

let size = stdin.readLine.parseInt

for i in 1..size:
for j in 1..size:
if i == j or i - 1 == size - j: stdout.write '*'
else: stdout.write ' '
echo ""

Yes.

18/88

print '* *'
print ' * * '
print ' * * '
print ' * '
print ' * * '
print ' * * '
print '* *'

v1.0 when?

C
#include

#define N 5
#define WALL '*'
#define HOLE ' '

int main(void) {
for (int y = N - 1; y >= 0; --y) {
for (int x = 0; x < N; ++x) {
putchar(x == y || x == (N - y - 1) ? WALL : HOLE);
}
putchar('\n');
}
}

#!/usr/bin/env lua5.3

local N = 5
local WALL = '*'
local HOLE = ' '

local s = {}
for y = N, 1, -1 do
for x = 1, N do
if x == y or x == N - y + 1 then
s[#s + 1] = WALL
else
s[#s + 1] = HOLE
end
end
s[#s + 1] = '\n'
end
print(table.concat(s, "", 1, #s - 1))

Lua

Python
#!/usr/bin/env python3

N = 5
WALL = '*'
HOLE = ' '

for y in range(N - 1, -1, -1):
for x in range(N):
print(WALL if x == y or x == (N - y - 1) else HOLE, end='')
print()

Also, for stuff like this please use my thread I can do stuff in C, Python and Lua.

printf " * \n * \n*****\n * \n * \n" | cowsay -n

I can't understand the reference, what does Wikipedia founder has to do with FORTRAN?
Are you a scientist user?

Forgot I uninstalled that useless shit.
The program 'cowsay' is currently not installed. You can install it by typing:
sudo apt install cowsay

noo
at least not yet

...

absolute madman

Autism

Use the ISE lol

fixed

github.com/nim-lang/Nim/blob/devel/todo.txt#L1-L10

if(Integer.parseInt(args[0])==5){
System.out.println("* *");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println(" * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println("* *");
}
else {
System.out.println("size is not within parameter restrictions");
}


Stay sharp!

m=ARGV[0].to_i-1;for n in 0..m;o=" "*m;o[n]='*';o[m-n]='*';puts o;end

C
#include
#include
#define z (h+1)
#define a [
#define ka ]
#define x(x) x,x
int main(int b, char **y) {if(b) {int c a 43 ka = {3,4,2,0,5,x(x(x(x(0)))),x(x(x(6))),x(0),0,6}; c a 42 ka = 1;int k a] = {x(x(4)),x(b),0}; for (int j=0;j=h);) {u a i*z+i ka = k a k[ k a 5]ka];u[i*z+h ka = 6 + k a 5 ka;k a 5 ka;u a i*z+h-i-k [ k [5 ka]ka = k a k [ k a 5 ]] ka;}for (i=-1;++i

Why have you done this

getting ready for ioccc this year.

too much readable
try harder.

Code:
#!/usr/bin/env bash

a='1'
b=${1}
c=${a}
d=${b}

until [[ "${a}" -gt "${b}" ]]; do
pos='1'
until [[ "${pos}" -gt "${b}" ]]; do
if [[ ( "${pos}" == "${c}" ) || ( "${pos}" == "${d}" ) ]]; then
echo -n '*'
else
echo -n ' '
fi
((pos++))
done
((a++))
((c++))
((d--))
echo ''
done


Output
$ xshape 5
* *
* *
*
* *
* *
$ xshape 15
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *

defmodule X do
def x(n) do
blank = List.duplicate(' ', n)
0..n - 1
|> Enum.map(fn x ->
blank
|> List.replace_at(x, '*')
|> List.replace_at(n - 1 - x, '*')
end)
|> Enum.join("\n")
end
end

Enum.each(3..10, fn x ->
x
|> X.x()
|> (& &1 "\n").()
|> IO.puts()
end)

use std::io;
use std::io::Write;

fn main() {
print!("Enter the size: ");
io::stdout().flush().unwrap();

let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();

match buf.trim().parse::() {
Ok(n) if n > 2 => {
println!();
draw_cross(n);
}
Ok(_) => println!("Size must be larger than 2"),
Err(_) => println!("Size must be a number"),
}
}

fn draw_cross(n: i32) {
for i in 0..n {
for j in 0..n {
print!("{}", if j == i || j == n - 1 - i { '*' } else { ' ' })
}

println!();
}
}

> io::stdin().read_line(&mut buf).unwrap();

disgusting

#include
#include

void draw_even_x(int size)
{
for (int i = 0; i < size / 2; i++) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2 + (i * 2))); j++) putchar(' ');
printf("x\n");
}

for (int i = (size / 2) - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2 + (i * 2))); j++) putchar(' ');
printf("x\n");
}
}

void draw_odd_x(int size)
{
for (int i = 0; i < size / 2; i++) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2+ (i * 2))); j++) putchar(' ');
printf("x\n");
}

for (int i = 0; i < size / 2; i++) putchar(' ');
printf("x\n");

for (int i = (size / 2) - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2+ (i * 2))); j++) putchar(' ');
printf("x\n");
}
}

int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "error: no argument provided\n");
exit(EXIT_FAILURE);
}

int size = atoi(argv[1]);
if (size < 1) {
fprintf(stderr, "error: argument must be a positive integer\n");
exit(EXIT_FAILURE);
}

if (size % 2) draw_odd_x(size);
else draw_even_x(size);

return EXIT_SUCCESS;
}

Does not draw additional unnecessary spaces at the end of the second asterisk. Behavior for even and odd sized cross must therefore be slightly different. General strategy for either is: draw top half, then draw bottom half with reverse process.

Shorter:
#include
#include

int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "error: no argument provided\n");
exit(EXIT_FAILURE);
}

int size = atoi(argv[1]);
if (size < 1) {
fprintf(stderr, "error: argument must be a positive integer\n");
exit(EXIT_FAILURE);
}

/* Draw top half */
for (int i = 0; i < size / 2; i++) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2+ (i * 2))); j++) putchar(' ');
printf("x\n");
}

/* Draw middle (for odd sized crosses) */
if (size % 2) {
for (int i = 0; i < size / 2; i++) putchar(' ');
printf("x\n");
}

/* Draw bottom half */
for (int i = (size / 2) - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) putchar(' ');
putchar('x');
for (int j = 0; j < (size - (2+ (i * 2))); j++) putchar(' ');
printf("x\n");
}

return EXIT_SUCCESS;
}

fun n x y = if x == y || x == (n - y) then 'X' else ' '
cross n = unlines [ [ fun n x y | x = putStr . cross

#!/bin/sh
#!/bin/sh
x() {
n=$1
odd=$(expr $n % 2)
half=$(expr $n / 2)
for i in $(seq 0 $n); do
a=$i
if [ $i -gt $half ]; then
a=$(expr $n - $i)
fi
b=$(expr $n - $a \* 2 - 1)
printf "%${a}s*" ''
if [ $i -ne $half -o $odd -eq 1 ]; then
printf "%${b}s*" ''
fi
printf "\n"
done
}

for i in $(seq 2 10); do
x $i
done

prettified edition
fun n x y =
if x == y || x == (n - y)
then 'X'
else ' '

cross n = unlines
[ [ fun n x y |
x = putStr . cross

x(N) :- xdraw(1,N,N,N), !.

xdraw(_,_,0,_).
xdraw(L,H,C,N) :- row(L,H,N),
L1 is L+1,
H1 is H-1,
C1 is C-1,
xdraw(L1,H1,C1,N).

row(_,_,0) :- nl.
row(L,H,C) :- C = H,
write('*'),
C1 is C-1,
row(L,H,C1).
row(L,H,C) :- C = L,
write('*'),
C1 is C-1,
row(L,H,C1).
row(L,H,C) :- write(' '),
C1 is C-1,
row(L,H,C1).

...

Nice

>side effects

I don't see any asserts. Do you?

Thanks :)

gee must think I am homosexual because there is no way I am doing that bullshit

#!/usr/bin/env python3

import sys

num = int(sys.argv[1])

for i in range(num):
lst = [" "] * num
lst[i] = lst[num-i-1] = "*"
print("".join(lst))

Well done user, very clean.
As someone going through Bratko's book it's nice to know there's at least one other person still holding the torch for prolog.

Thanks! I actually learned it in uni, believe it or not, in a course that covered prolog and scheme. Glad I had to take it, it really blew my mind. I've been looking for something fun to do with it.

Is this the book?
amazon.com/Programming-Artificial-Intelligence-International-Computer/dp/0321417461

Do you need any existing AI knowledge to read it?

Hahaha

#include
#include

void print_cross(int scale) {
for (int i=0; i

Now just wrap it up in an XFactory and it's enterprise ready!

#!/usr/bin/env ruby

def draw_half_cross size, iter
iter.each do |i|
puts "#{' ' * i}x#{' ' * (size - (2 + i * 2))}x"
end
end

abort "error: no argument provided" if ARGV.empty?

size = ARGV[0].to_i
half = size / 2
abort "error: argument must be a positive integer" if size < 1

draw_half_cross size, 0.upto(half - 1)
puts "#{' ' * half}x" if size.odd?
draw_half_cross size, (half - 1).downto(0)

What a disgusting language

What part do you find disgusting?

Which parts DON'T I find disgusting?

Haskell:
main = putStr "Enter size: " >> getLine >>= star
star s = putStr $ unlines $ map line [0..n]
where n = read s - 1
line k = [if elem i [k,n-k] then '*' else ' ' | i

not really a programmer, how did I do?
#include
int main(){
int size, loops = 1;
printf("? ");
scanf("%d", &size);
for(int i = 1; i

Looks mostly good. People will probably shit on your code but they basically make it into a code golf competition here. Only real issue imo is you could just use i instead of loops. Haven't checked for off by one errors though.

I can't do it, am I a brainlet? Do you guys start with a template or lookup the first step?

I'm talking about from scratch.

That's true. I'd 'optimize' it but it would just end up looking like

def x n
(0...n).map do |i|
(0...n).map do |j|
if i == j || n - i - 1 == j
"*"
else
" "
end
end.join
end.join "\n"
end

Your second comparison is fucked. See this post

int y = .5 I stopped reading there

lmao

That's cause I was trying to fuck around with a different method of doing it using floats and forgot to change my initializer back to zero.

Guess what? This is C and it automatically turned them back to zero because that's how integers work.

Yeah, I see what you mean.
What do you mean by template/lookup?
That's Ruby, right? How does the precedence work here? As in, how does it know not do bind the join to that do block... thing? Not sure if it's a real lambda or not.
Yeah, he's fucking with you user, 0.5 just truncates to 0 for ints. I just checked your code, and it renders the second diagonal wrong. The easiest solution is to just replace the second conditional with 1+size-loops. If you counted from 0 to n-1 like he did, your offset would be slightly different since j is lower but size and loops cancel out.

These programming problems are like math problems you did in school. First they taught you how to solve for x, then they gave you problems like x - 7 = 8. Except the tool you use to solve the problem are the programming language. So, first, it helps to know a programming language. Not saying you need to be a master. As an example, you should know the syntax for a "for" loop (or a given language's equivalent) without looking it up. The next step I usually take is thinking how I would "manually" code the solution. For this problem, it would be like this: Whenever there's repetitions, you can use a loop of some kind. You can also try looking at it like this: Another good trick to use is to look for a pattern and figure out the pattern. In this problem, the number of spaces between the * decreases by two each time, and then increases by 2 starting from 1 after the center *. Another one that applies more to problems with sets of data is to sort the data in some meaningful way. That's all I can think of for now, but basically you just need to learn a programming language and then things start to become obvious.


t. learning programming through solving challenges

no types yet more type safe than all the other solutions in this thread

Haskell niggers go home.

why

In beautiful Javascript:


function makeStar(size){

var x, y, z, str;

for(x = 0, y = size; x < size; x++, y--){
str = [];

for(z = 0; z < size; z++){
str.push(' ');
}
str[x] = '*';
str[y] = '*';
document.write(str.join(' ') + '');
}
}

makeStar(10);

That's HTML

If you're going to omit the head tag, you can just go all the way in modern html and just put a script tag.

Need to do that in order to run in my browser.

>claim to have done it in javascript
>but post html solution
lying is rude

ORG $1000

MOVE.B #$04,D0 ;get number
TRAP #$0F
MOVE.L D1,D2

LOOP_R:
MOVE.L #0,D4 ;reset column
MOVE.L D2,D5 ;scale-row-1
SUB.L D3,D5
SUB.L #1,D5

LOOP_C:
CMP.L D3,D4 ;column==row
SEQ D0
CMP.L D4,D5 ;column==scale-row-1
SEQ D1
OR.B D0, D1
CMPI.B #$FF, D1
BNE ELSE

MOVE.B #$2A,D1 ;print '*'
MOVE.B #$06,D0
TRAP #$0F
BRA IF_END
ELSE:
MOVE.B #$20,D1 ;print ' '
MOVE.B #$06,D0
TRAP #$0F
IF_END:

ADD.L #1,D4
CMP.L D2,D4
BLT LOOP_C

MOVE.B #$0A,D1 ;print CR, LF
MOVE.B #$06,D0
TRAP #$0F
MOVE.B #$0D,D1
MOVE.B #$06,D0
TRAP #$0F

ADD.L #1,D3
CMP.L D2,D3
BLT LOOP_R
STOP #$2700

END $1000

It's shit and I didn't bother optimizing it, but it works.

>ST-TOS
>not the TV show
Absolute madman.

Still haven't solved it yet but I would start by doing it small (3x3) and by hand first.

$ cat cross.coffee
size = +process.argv[2]
for y in [1..size]
for x in [1..size]
process.stdout.write (if x in [y, (1 + size - y)] then '*' else ' ')
process.stdout.write '\n'

$ coffee cross.coffee 7
* *
* *
* *
*
* *
* *
* *

Your loop terminates when x hits the size but your y initializes on size.