Commented the code a bit
This commit is contained in:
parent
99ef530cae
commit
335db01db7
@ -1,5 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* 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()
|
int main()
|
||||||
{
|
{
|
||||||
float nr, sum, biggest;
|
float nr, sum, biggest;
|
||||||
@ -9,13 +13,13 @@ int main()
|
|||||||
ant = 0;
|
ant = 0;
|
||||||
|
|
||||||
printf("Write the numbers.\n");
|
printf("Write the numbers.\n");
|
||||||
printf("Finish with EOF.\n");
|
printf("Finish with EOF.\n"); // EOF is CTRL-D in Unix/Linux
|
||||||
while (scanf("%f", &nr) == 1)
|
while (scanf("%f", &nr) == 1) // As long as we get characters to scanf, it's
|
||||||
{
|
{ // true, ie '1'.
|
||||||
ant++;
|
ant++;
|
||||||
sum += nr;
|
sum += nr; // Same as sum = sum + nr.
|
||||||
if (nr > biggest)
|
if (nr > biggest) // If 'nr' is bigger than 'biggest', make 'nr'
|
||||||
{
|
{ // the new 'biggest' value.
|
||||||
biggest = nr;
|
biggest = nr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
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)
|
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;
|
float number, sum, biggest;
|
||||||
int ant;
|
int ant;
|
||||||
@ -26,7 +30,7 @@ int main()
|
|||||||
{
|
{
|
||||||
ant++;
|
ant++;
|
||||||
sum += number;
|
sum += number;
|
||||||
biggest = max(biggest, number);
|
biggest = max(biggest, number); // biggest goes into x, number into y.
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Average: %.2f\n", sum / ant);
|
printf("Average: %.2f\n", sum / ant);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define YEARS 10
|
#define YEARS 15
|
||||||
|
|
||||||
|
/* Simple example of a program to calculate interest on interest */
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -7,7 +9,7 @@ int main()
|
|||||||
int year;
|
int year;
|
||||||
printf("Invested capial: "); scanf("%f", &cap);
|
printf("Invested capial: "); scanf("%f", &cap);
|
||||||
printf("Interest? "); scanf("%f", &interest);
|
printf("Interest? "); scanf("%f", &interest);
|
||||||
printf("\n Year Holding\n ==== =======\n");
|
printf("\n Year Holding\n ==== =======\n");
|
||||||
|
|
||||||
for (year = 1; year <= YEARS; year++)
|
for (year = 1; year <= YEARS; year++)
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* 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()
|
int main()
|
||||||
{
|
{
|
||||||
float cap, want, interest;
|
float cap, want, interest;
|
||||||
@ -11,6 +16,9 @@ int main()
|
|||||||
|
|
||||||
year = 0;
|
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)
|
while (cap < want)
|
||||||
{
|
{
|
||||||
year = year + 1;
|
year = year + 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user