I'm learning basic C for an Introductory Programming class that I'm taking and I have two questions related to the code below.
First: in the //calculations
section, I want my values called cryr1
, cryr2
, cryr3
, and avgcr
, to display the results out to 2 decimal places. I'm only getting whole numbers with 2 decimals places. QUESTION: Is there something wrong in how I wrote my calculations?
Second: in the section where I have all my printf
statements, I have a lot of empty white spaces to support the spacing in the table that I want to render. However, if the inputs to this output vary in numerical length, the columns don't render properly. QUESTION: Is there some code or syntax that I should use that calculates or determines the number spaces that each value in the table show appear?
// Global Variable declarations
FILE *reportfile; // report file (for output)
FILE *inputfile; // disk file (for input)
char company[31]; // company name
int assetsyr1; // assets year 1
int assetsyr2; // assets year 2
int assetsyr3; // assets year 3
int liabilitiesyr1; // liabilties year 1
int liabilitiesyr2; // liabilties year 2
int liabilitiesyr3; // liabilities year 3
float cryr1; // current ratio yr1
float cryr2; // current ratio yr2
float cryr3; // current ratio yr3
float avgassets; // average assets
float avgliabilities; // average liabilities
float avgcr; // average current ratio
//calculations
cryr1 = (assetsyr1/liabilitiesyr1);
cryr2 = (assetsyr2/liabilitiesyr2);
cryr3 = (assetsyr3/liabilitiesyr3);
avgassets = (assetsyr1 + assetsyr2 + assetsyr3)/3;
avgliabilities = (liabilitiesyr1 + liabilitiesyr2 + liabilitiesyr3)/3;
avgcr = (cryr1 + cryr2 + cryr3)/3;
printf("%s\Current Ratio Report\n\n", company);
printf(" Current Current Current\n");
printf("Year Assets Liabilities Ratio\n");
printf("----------------------------------------------------------\n");
printf("2010 %d %d %1.2f\n", assetsyr1, liabilitiesyr1, cryr1);
printf("2011 %d %d %1.2f\n", assetsyr2, liabilitiesyr2, cryr2);
printf("2012 %d %d %1.2f\n", assetsyr3, liabilitiesyr3, cryr3);
printf("----------------------------------------------------------\n");
printf("Average %1.0f %1.0f %1.2f\n\n", avgassets, avgliabilities, avgcr);
The image is supposed to render like this
Any help you can offer is much appreciated. Thank you, David
I've placed the source code and input file here