[Courses] [C] Beginner's Lesson 4A: Arrays, Qualifiers, and Reading Numbers

Anca M. Holban anca at lsv.cl.edu.ro
Fri Oct 11 17:23:40 EST 2002


Umm, programming in C is fun :)
Instead of writing so many lines, (again:>) i'd do smth like this:

#include "stdio.h"
int main(void)
     {
     fload data[5]={34.0,27.0,45.0,82.0,22.0};
     double total;
     total=data[0]+data[1]+data[2]+data[3]+data[4];
     printf("total: %.2f, average: %.2f.\n",total,total/5.0);
     return 0;
     }

   I used %.2f to display the results with 2 decimals.
	peace ;) Anca.

> 	/* exmpl4-1.c -- computes the total and average of five numbers */
> 	/*            -- program shows use of a single dimension array  */
> 	#include <stdio.h>
> 	int main(void)
> 	{
> 		float data[5];  /* array holds data to average and total */
> 		float total;    /* the total of the data items */
> 		float average;  /* average of the items */
> 
> 		data[0] = 34.0;
> 		data[1] = 27.0;
> 		data[2] = 45.0;
> 		data[3] = 82.0;
> 		data[4] = 22.0;
> 	
> 		total = data[0] + data[1] + data[2] + data[3] + data[4];
> 		average = total /5.0;
> 		printf("Total %f Average %f\n", total, average);
> 
> 		return 0;
> 	}




More information about the Courses mailing list