These are design decisions that a flowchart does not show.
Should the variables raw
and score
be integer or floating point?
Let use use integer values for these.
Should the variables base
and scale
be integer or floating point?
These should be floating point.
Here is the complete program. Note how the comments follow the flowchart.
#include <stdio.h> #include <math.h> int main() { double base, scale; int raw, score, count=0; /* Initialize */ printf("Enter base -->"); scanf("%lf", &base); printf("Enter scale -->"); scanf("%lf", &scale); /* Get first raw score */ printf("Score 1 -->"); scanf("%d", &raw); /* Loop while raw score is not negative */ while (raw >= 0) { if (raw <= 300) { /* Convert and print the raw score */ count++; score = (int)round(base + scale*raw); printf("Scaled Score: %d\n", score); } else printf("That value is out of range\n"); /* Get next raw score */ printf("Score %2d-->", count + 1); scanf("%d", &raw); } /* Summarize */ printf("%3d scores processed.", count); }
If you try to compile and run this program on a recent version of Microsoft VC++ it will ask you to replace
scan
with scan_s
for increased security.
Go ahead and do that.
double round( double val )
rounds its double precision argument to the nearest double precision integer.
A value like 3.4 is rounded to 3.0. A value like 3.6 is rounded to 4.0.
Not all C environments have it. If they do, you need to include math.h
.
Why is a type cast used in score = (int)round(base + scale*raw);
?