Arrays and Beers - Lone Leth Thomsen

Solution index                   Textual C program


#include <stdio.h>
#include <stdlib.h>

#define  N 3

int i=0, beer_per_hour=0, beer[N];

int main(void)
{
   beer[0] = 2; //Lone's beer/h capability
   beer[1] = 5; //Kim's beer/h capability
   beer[2] = 8; //Thorbjoern's beer/h capability

   for(i=0; i<N; i++){
            beer_per_hour = beer_per_hour + beer[i];
   }

   for(i=0; i<N; i++){
            printf("\n Members' capabilities: %d", beer[i]);
   }

   printf("\n Team's total amount of beer per hour: %d \n", beer_per_hour);

   system("PAUSE");
   return 0;
}

Summary of exercise: Write a program that contains an int-array called "beer" with the same size as the number of group members in your group. In each field of the array, write how many beers each group member can drink per hour. With a for-loop, calculate how many beers the group can drink per hour, all together.
 

#include <stdio.h>
#include <stdlib.h>
The header files of Standard Input/Output (stdio.h) and the Standard Library (stdlib.h) are included.
 

#define  N 3
As always the size of the array should be defined as a symbolic constant
 

int i=0, beer_per_hour=0, beer[N];
The integer for iterating through the array (i) and the integer for summing up the number of beers (beer_per_hour) are initiliazed to 0. Furthermore the array (beer) is created with space allocated for N elements.
 

   beer[0] = 2; //Lone's beer/h capability
   beer[1] = 5; //Kim's beer/h capability
   beer[2] = 8; //Thorbjoern's beer/h capability
The values of the array are set to the capability of beer per hour each group member can consume.
 

   for(i=0; i<N; i++){
            beer_per_hour = beer_per_hour + beer[i];
   }
This for-loop calculates the amount of beers the group can consume in one hour. The counter i is used to access each of the indexes in the beer array.
 

   for(i=0; i<N; i++){
            printf("\n Members' capabilities: %d", beer[i]);
   }
This for-loop prints the amount of beers each of the group members can consume in one hour. Again the counter i is used to access the indexes in the beer array.
 

   printf("\n Team's total amount of beer per hour: %d \n", beer_per_hour);
This prints the total amount of beers the group can consume.
 


Generated: Wednesday, March 29, 2006, 12:33:34
This program dissection page is generated from an XML-in-LAML source file