# ASCII Progress Bar

This is a companion discussion topic for the original entry at https://toph.co/p/ascii-progress-bar

/*
Why getting WA in the last test case. But I find everything OK. My code:
*/

``````#include <stdio.h>
#include <math.h>

int main(void)
{
float rate;
int near;

scanf ("%f", &rate);
near = (int) floor(rate);
int tm = (int) (rate / 10.0);

printf ("[");
for (int i=0; i < tm; ++i){
printf ("+");
}
for (int k=10; k > tm; --k)
printf (".");
printf ("] %d%c\n", near, '%');

return 0;
}
``````

@md_jakariya Try an input like `88.999999`.

1 Like

Whst should be the output

The output for 55% should equal to 50%or 60% ?

floor it down to the nearest 10

55% will be treated as 50%.

1 Like
``````#include<iostream>
using namespace std;

int main()
{
float f;
int n;
cin >> f;
n = (int)f;

int floor = n / 10;

if(floor <= 10)
{
cout << "[";
for(int i=1; i<=10; i++)
{
if(i <= floor)
cout << "+";
else
cout << ".";
}
cout << "]" << " " << n << "%" << endl;
}

return 0;
}
``````

Why I am getting wrong answer?

Try using `double f` instead of `float`.

It worked. Thanks!   1 Like

#include<stdio.h>
int main()
{
printf(“Should I solve it like this?”);
int a;
scanf("%d",&a);
int c=a;
printf("[");
a=a/10;
int b=10-a;
while(a–){
printf("+");
}
while(b–){
printf(".");
}
printf("] %d",c);
char z=’%’;
printf("%c",z);
return 0;
}

@Burn_Fireblaze looks like you have already solved it.

I’m surprised. Why people are solving it in the complex way?

Many man. many ideas. Thats why.

@hjr265 bro.
as the problem statement says that-- floor it down to the nearest 10 (62 becomes 60, 65 becomes 60, 68 becomes 60 and so on.).

but in input 62.3% have output of 62%.

why?

use double insted of float

Floor it down to nearest 10 to draw the progress bar.

Floor it down to the nearest integer to print the percentage.

``````x=float(input())
x=int(x)
y=x//10
z=10-y
print('[',end='')
for i in range(10):
if 1<=y<=10:
print('+',end='')
y-=1
elif 1<=z<=10:
print('.',end='')
z-=1
print(']',end='')
print(x)
``````

What is the problem here

1 Like
``````#include<stdio.h>
void main(){
float n;
scanf("%f",&n);
int a,b,c;
a=n/1;
b=a/10;
c=10-b;
printf("[");
for(int i=1;i<=b;i++){
if(i<=a){
printf("%c",43);
}
}
for(int i=1;i<=c;i++){
if(i<=c){
printf("%c",46);
}
}
printf("] ");
printf("%d",a);
printf("%c",37);
}

``````

what’s wrong with my code???

1.
`void main()`
` ^~~~ change the return type to int` [This line’s inspired by @hjr265 ]

2. Check the quotes. Change all “ to ".

3. Your code’s giving incorrect output for cases like 52.999999.
Change

to `double n`

# ────────────────────

Explanation: `double` has 2x the precision of `float`, as the name says. In general, `double` has 15 decimal digits of precision, while `float` has 7.

# ────────────────────

Calculations:

`double` has 52 mantissa bits + 1 hidden bit: log(253)/log(10) = 15.95 digits

`float` has 23 mantissa bits + 1 hidden bit: log(224)/log(10) = 7.22 digits

This precision loss could lead to greater truncation errors in cases.

1 Like

You can use static_cast(n) to cut down the decimal portion without rounding the double number

2 Likes