Problem Description:
Steve is suspicious that the pen drive he just bought for his computer said 1EB on the box, but when he plugged it into his computer the OS says it only has 931PB of space.
Meena says that's because hard drive marketing uses base-10 to calculate space, but computer science (and OS) use base-2 (and always have). So, using base-10, 1 Exabyte (EB) would be 10^18 (1,000,000,000,000,000,000) bytes.
But in base-2 it would be 2^60 (1,152,921,504,606,846,976) bytes.
Most humans use base-10 when counting, so there is confusion. (Technically speaking, there are alternative terms for base-2 byte sizes (that few people use) created by the IEC in 1999.)
Help Meena explain it to steve by writing a program that will take storage space given in base-10, and convert it to base-2 using the tables below for reference.
Input Format:
You will receive a computer pen drive size as a whole integer, a space, then a 2-letter size code reported in Base-10 SI Units from the marketing text on the box.
Your program should then convert to the base-2 Binary size the hard drive will show as available space in the OS rounded to the nearest 2 decimal places in the largest binary size you can express a whole number in (e.g. do not write 1030 MiB, write 1.01 GiB)
#include<stdio.h>
#include<string.h>
#define PREFIXES 7
int main(){
double siq[PREFIXES],b2q[PREFIXES];
double n,bytes=0;
char *si[]={"KB","MB","GB","TB","PB","EB","ZB"};
char *b2[]={"KiB","MiB","GiB","TiB","PiB","EiB","ZiB"},u[3];
siq[0]=1e3;b2q[0]=1024;int i;
for(i=1;i<PREFIXES;i++){siq[i]=siq[i-1]*1000;b2q[i]=b2q[i-1]*1024;}
scanf("%lf %s",&n,u);
for(i=0;i<PREFIXES;i++)if(strcmp(u,si[i])==0)bytes=n*siq[i];
for(i=PREFIXES-1;i>=0;i--)if(bytes>=b2q[i]){printf("%.2lf %s",bytes/b2q[i],b2[i]);break;}
}
0 Comments