Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

50%
+0 −0
Challenges Display a Progress Bar

C (gcc), 88 bytes char a[53]={91},*p=a+1,i;f(n,d){for(;i<50;)p[i++]=1.*n/d<=i/50.?45:124;p[i]=93;puts(a);} Try it online! There's two approaches to this in C that I came up with - eith...

posted 23h ago by Lundin‭

Answer
#1: Initial revision by user avatar Lundin‭ · 2024-11-22T15:01:24Z (about 23 hours ago)
# [C (gcc)], 88 bytes

<!-- language-all: lang-c -->

    char a[53]={91},*p=a+1,i;f(n,d){for(;i<50;)p[i++]=1.*n/d<=i/50.?45:124;p[i]=93;puts(a);}

[Try it online!][TIO-m3sv6mme]

[C (gcc)]: https://gcc.gnu.org/
[TIO-m3sv6mme]: https://tio.run/##NY29EoMgEIR7n4KZNKAXBZHCnEwexLFg/EkoJI6ayvHZCTGm@/Z297a9PtrW@/ZpZmJqJRu9lWKHeNImEWBxoA46tg2vmaKtFEc21TZJGi3S2GVdpW2meHov1E3kBQav0aXE6b0u1DDc/aXrB@t68vtDrOZIxn5c@pUa4KByhmGXh5ZAcozimY0i61YyGusoi7aIhLOAED8oB/knWYDk/FQK1Bd3/wE "C (gcc) – Try It Online"

There's two approaches to this in C that I came up with - either write into an array then print it, or print everything at once but use conditionals. I tried both but got about the same result so this version is the array one.

Dumping all variables at file scope makes initialization easier and we can abuse the usual C89 tricks of "implicit int" (but this makes the function "call once", so tests will have to restore all variables between multiple calls).

The actual algorithm is just floating point division then scale that from 0 to 50. `<` instead of `<=` would give incorrect rounding and that would be my on my algorithm, not the language, so I used `<=`.

Also this code has some pretty severe UB abuse with the i++... :)