[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
C program
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 2
File: Capture.png (1 KB, 82x156) Image search: [Google]
Capture.png
1 KB, 82x156
Need help to make code in C that prints out this shape. The size is dependant to a variable at the beggining (scanf("%d",var))
Only needs to work for values that are multiples of 3.
>>
>>53357162
Did you ever consider that you're supposed to actually learn something (and/or apply shit you learned) from this task?
>>
>>53357162
We're not going to do your homework for you faggot
>>
main = putStrLn(arrow 9)

arrow n = unlines $
[[star (x >= mid-1 && x <= mid+1) | x <- w]] ++
[[star (x == mid-1 || x == mid+1) | x <- w] | y <- [2..mid]] ++
[[star (x /= mid) | x <- w]] ++
[[star (x == mid-y || x == mid+y) | x <- w] | y <- [mid-1,mid-2..0]]
where mid = n `div` 2
w = [0..n-1]
star True = '*'
star False = ' '


h-how did I do?
>>
n/3 spaces, n/3 stars, n/3 spaces
n/3 * (n/3 spaces, 1 star, n/3-2 spaces, 1 star, n/3 spaces)
n3/+1 stars,n/3-2 spaces, n/3+1 stars
etc
>>
>>53357342
Really nice, would be even cooler if the top part would scale too.
Here is my attempt for that:
main = putStrLn(arrow 23)

arrow :: Integer -> String
arrow n = unlines $
[[star (x >= mid-o && x <= mid+o) | x <- w]] ++
[[star (x == mid-o || x == mid+o) | x <- w] | y <- [2..mid]] ++
[[star (x <= mid-o || x >= mid+o) | x <- w]] ++
[[star (x == mid-y || x == mid+y) | x <- w] | y <- [mid-1,mid-2..0]]
where mid = n `div` 2
o = floor $ (fromInteger n) / 5
w = [0..n-1]
star True = '*'
star False = ' '
>>
>>53359108
nice
>>
>>53359108
>>53357342
>>53357596
thanks

>>53357291
no one made you
>>
This faggot is gonna enter the workforce someday
>>
Here you go, OP.

#include <stdlib.h>
#include <tcl.h>
#define MULTILINE(...) #__VA_ARGS__
/* Compile with
cc arrow-tcl.c -o arrow-tcl -ltcl
*/

int main(int argc, char* argv[]) {
Tcl_Interp *interp = Tcl_CreateInterp();
if (Tcl_Init(interp) != TCL_OK) {
return EXIT_FAILURE;
}
Tcl_Eval(interp, MULTILINE(
package require Tcl 8.5;

proc arrow n {
set result {};
set mid [expr {$n / 2}];
set p1 [expr {$n / 3}];
set p2 [expr {2 * $n / 3}];
for {set i 0} {$i < $n} {incr i} {
for {set j 0} {$j < $n} {incr j} {
set star? [expr {
($i == 0 && $j >= $p1 && $j <= $p2) ||
($i > 0 && $i < $mid && ($j == $p1 || $j == $p2)) ||
($i == $mid && ($j <= $p1 || $j >= $p2)) ||
($i > $mid && ($j == $i - $mid || $j == $n - $i + $mid - 1))
}];
append result [lindex {{ } *} ${star?}]
};
append result \\n;
};
return $result;
};

arrow [gets stdin];
));
printf("%s", Tcl_GetStringResult(interp));
Tcl_Finalize();
return EXIT_SUCCESS;
return 0;
}
>>
>>53359108
Just noticed that
o = n `div` 5

would be shorter. I was just trying out stuff and didn't clean it up properly.
>>
>>53359707
If you only need multiples of three it will look better with
set p2 [expr {2 * $n / 3 - 1}];
>>
>>53359411
>no one made you
That's not how it works, faggot.
>>
>>53357162
It's really shitty, lots of variable overlap, but got tired of working on ur stupid homework.

#lang racket

(provide (all-defined-out))

(define (divisible-by-3? n)
(and (exact-positive-integer? n) (zero? (modulo n 3))))

(define/contract (arrow-line size)
(divisible-by-3? . -> . string?)
(define segment (ceiling (/ size 3)))
(define leading (make-string segment #\space))
(define inner (if (>= size 9) (make-string (ceiling (/ segment 3)) #\space) ""))
(define (make-line x)
(string-append leading (if (zero? x)
(make-string segment #\*)
(string-append "*" inner "*"))))
(match size
[3 " *"]
[_ (string-join (map make-line (range (floor (/ size 2)))) "\n")]))

(define/contract (arrow-triangle size)
(divisible-by-3? . -> . string?)
(define len (ceiling (/ size 2)))
(define inner (if (>= size 9) (make-string (ceiling (/ size 3 3)) #\space) ""))
(define wing (make-string (ceiling (- (/ size 2) (/ size 3 3))) #\*))
(define (make-line x)
(define s (- size (+ (* x 2) 2)))
(string-append (make-string x #\space)
(cond
[(zero? x) (if (= size 3) "***" (string-append wing inner wing))]
[(<= s 0) (if (even? size) "**" "*")]
[else (string-append "*" (make-string s #\space) "*")])))
(string-join (map make-line (range len)) "\n"))

(define/contract (arrow size)
(divisible-by-3? . -> . string?)
(string-append (arrow-line size)
"\n"
(arrow-triangle size)))

(module+ main
(for ([size '(3 6 9 12)])
(printf "Size ~a\n\n~a\n\n" size (arrow size))))
>>
I won't try and solve it right now, but I'll tell you that usually the easiest way to solve problems like these (printing symmetrical patterns of characters) is using the backspace escape character. I might have a look at this later.
>>
>>53357162
nice arrow xD upvoted :P
Thread replies: 16
Thread images: 2

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.