From 335db01db74ee229fd4a01f1175f37488ec59aef Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Tue, 13 Oct 2015 20:02:07 +0200 Subject: [PATCH] Commented the code a bit --- average_biggest.c | 16 ++++++++++------ avg_with_function.c | 10 +++++++--- interest1.c | 6 ++++-- interest3.c | 8 ++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/average_biggest.c b/average_biggest.c index ccd4cdf..f5a9b6e 100644 --- a/average_biggest.c +++ b/average_biggest.c @@ -1,5 +1,9 @@ #include +/* A program to calculate the average of a set of numbers and the biggest + * of those numbers. From the book "Vägen till C". Translated it to english. + */ + int main() { float nr, sum, biggest; @@ -9,13 +13,13 @@ int main() ant = 0; printf("Write the numbers.\n"); - printf("Finish with EOF.\n"); - while (scanf("%f", &nr) == 1) - { + printf("Finish with EOF.\n"); // EOF is CTRL-D in Unix/Linux + while (scanf("%f", &nr) == 1) // As long as we get characters to scanf, it's + { // true, ie '1'. ant++; - sum += nr; - if (nr > biggest) - { + sum += nr; // Same as sum = sum + nr. + if (nr > biggest) // If 'nr' is bigger than 'biggest', make 'nr' + { // the new 'biggest' value. biggest = nr; } } diff --git a/avg_with_function.c b/avg_with_function.c index a9bb036..05a2b51 100644 --- a/avg_with_function.c +++ b/avg_with_function.c @@ -1,6 +1,10 @@ #include -float max(float x, float y) +/* A first example from the book "Vägen till C" where we get to use functions. + * Here is a function to get the biggest number from a set of numbers. + */ + +float max(float x, float y) // It's return value is a float. { if (x > y) { @@ -12,7 +16,7 @@ float max(float x, float y) } } -int main() +int main() // main's return value is an int (return 0 at the bottom). { float number, sum, biggest; int ant; @@ -26,7 +30,7 @@ int main() { ant++; sum += number; - biggest = max(biggest, number); + biggest = max(biggest, number); // biggest goes into x, number into y. } printf("Average: %.2f\n", sum / ant); diff --git a/interest1.c b/interest1.c index 04d120f..551bb28 100644 --- a/interest1.c +++ b/interest1.c @@ -1,5 +1,7 @@ #include -#define YEARS 10 +#define YEARS 15 + +/* Simple example of a program to calculate interest on interest */ int main() { @@ -7,7 +9,7 @@ int main() int year; printf("Invested capial: "); scanf("%f", &cap); printf("Interest? "); scanf("%f", &interest); - printf("\n Year Holding\n ==== =======\n"); + printf("\n Year Holding\n ==== =======\n"); for (year = 1; year <= YEARS; year++) { diff --git a/interest3.c b/interest3.c index 143b00b..5b026ce 100644 --- a/interest3.c +++ b/interest3.c @@ -1,5 +1,10 @@ #include +/* A simple program from the book "Vägen till C" to calculate for how long your + * money will have to sit in the bank at a given interest rate to reach the + * amount of money you'll want + */ + int main() { float cap, want, interest; @@ -11,6 +16,9 @@ int main() year = 0; + /* As long as your capital at the bank is less than you want, keep rerunning + * the loop until we reach the amout we want. Each loop is one year. + */ while (cap < want) { year = year + 1;