-1

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

expected output

Any help you can offer is much appreciated. Thank you, David

I've placed the source code and input file here

phuclv
  • 37,963
  • 15
  • 156
  • 475
davidw
  • 45
  • 7
  • Please copy-and-paste your actual output and desired output **as text**, and ensure that your code is a [mcve] -- the **shortest possible** code someone else can run to see the problem themselves. ("Someone else can run" also means it needs to be complete enough to run -- typically, this means you might want to hardcode values rather than reading them from a file, since folks who want to test their answers typically won't have that input file at hand). – Charles Duffy Oct 20 '17 at 21:23
  • https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output This ll help you with formating your output. – marko Oct 20 '17 at 21:25
  • @davidw shaun answer ll solve your problems, you need to cast to float first. – marko Oct 20 '17 at 21:27
  • 1
    `cryr1 = (assetsyr1/liabilitiesyr1)`. Both operands on the right are ints, so theere will be integer division whose result (int) will be assigned to the float. Use `cryr1 = ((float)assetsyr1/liabilitiesyr1)` (same for others). Check [\[cplusplus\]: printf](http://www.cplusplus.com/reference/cstdio/printf) (_width_ related) for more formatting details (regarding your 2nd question). – CristiFati Oct 20 '17 at 21:27
  • source code and input file are [here](https://drive.google.com/drive/folders/0B9zdJQ7M_lvWeWxkeFEyUHpiaEE?usp=sharing) – davidw Oct 20 '17 at 21:40

1 Answers1

1

When you divide an integer by a integer and you want a float result that has its decimal part NOT truncated, you should typecast the integer to a float. That is what I understand by looking at your code

The safest way I would say would be is

//calculations
cryr1 = ((float)assetsyr1/liabilitiesyr1);
cryr2 = ((float)assetsyr2/liabilitiesyr2);
cryr3 = ((float)assetsyr3/liabilitiesyr3);
avgassets = ((float)(assetsyr1 + assetsyr2 + assetsyr3))/3;
avgliabilities = ((float)(liabilitiesyr1 + liabilitiesyr2 + liabilitiesyr3))/3;
avgcr = ((float)(cryr1 + cryr2 + cryr3))/3;

Try it out

For the second part of the question

printf statements have the ability to "set" the field for printing Like you used the %1.2f, it ensures right-alignment and prints 2 decimal spaces.

There is a similar concept for integer types as well

 printf("%7d", 12345);           //This will print   12345   (right aligned)

 printf("%7d", 123);             //This will print     123   (right aligned also notice that both numbers start from the right at same position)

You can remove a couple of spaces from the printf statements. So you can fix the tab headings and work with these

Shaun F
  • 85
  • 1
  • 2
  • 8
  • Thank you! That worked for the first of the two questions. I really appreciate that. What does "typecast" mean? I'm a total newb. – davidw Oct 20 '17 at 21:29
  • Hey, yeah, no problem at all. Typecasting is a term for changing the data-type of one variable to another data-type. What was done here is called Explicit Type Casting, which means WE forced the program to change the type. (Sometimes even the compiler does it for example when you store the division of a two floats into an integer like int a = 3.0/1.0; it will store a as 3) I've also added a suggestion for the solution of your second issue in my answer – Shaun F Oct 20 '17 at 21:48
  • Shaun, thank you so much!! Is there an equivalent left alignment that I can explore? – davidw Oct 20 '17 at 21:57
  • Yes, it would be the same except the number would have a negative sign printf("%-7d", number); – Shaun F Oct 20 '17 at 22:10