UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help Search Notes:

# A Combined Example

This program will work with right triangles. We have a data file that gives the lengths of the two sides of right triangles and we are to calculate the hypotenuse of each triangle. The first item in the data file is the number of pairs of sides that exist in the file.

## The Data File

```linux1[520] cat triangles.dat
5
3.000   4.000
3.250   5.750
4.500   7.250
5.000   8.000
6.000   8.000
linux1[521]
```
Our program will open the file, read the number of pairs and then allocate enough memory to hold an array of that many TRIANGLE structures. We can then read the lengths of the sides into each of the structures, calculate the hypotenuse for each, and later print it all out to a file.

## The Program

```
/*********************************
** File file_malloc.c
** Author: S. Bogar
** Date: 04/03/02
** Section: 101
** E-Mail: bogar@cs.umbc.edu
** Modified: 09/25/05
**
** This file demonstrates file handling, dynamic
** memory allocation and working with an array of
** structures.
*******************************/

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

typedef struct triangle
{
float sideA;
float sideB;
float hypotenuse;
} TRIANGLE;

int main()
{
int i, items ;
TRIANGLE *triangles;
FILE *ifp, *ofp;

/* open the data file */
ifp = fopen("triangles.dat", "r");
if (ifp == NULL)
{
printf("Couldn't open triangles.dat\n");
exit(-1);
}

/* read the number of items from the file */
fscanf(ifp, "%d", &items);

/* Get a block of memory big enough to hold that many TRIANGLES */
triangles = (TRIANGLE *) malloc(items * sizeof(TRIANGLE)) ;
if(triangles == NULL)
{
printf("Not enough memory\n");
exit(-2);
}

/* Lets use it as an array of TRIANGLES and fill the values
of all the sideAs and sideBs by reading them from the file */
for(i = 0; i < items; i++)
{
fscanf(ifp, "%f", &triangles[i].sideA);
fscanf(ifp, "%f", &triangles[i].sideB);
}

/* Close the file since we're finished reading from it */
fclose(ifp);

/* Calculate the hypotenuse */
for (i = 0 ; i < items ; i++)
{
triangles[i].hypotenuse = sqrt(triangles[i].sideA *
triangles[i].sideA +
triangles[i].sideB *
triangles[i].sideB );
}

/* open the output file */
ofp = fopen("triangles.out", "w");
if (ofp == NULL)
{
printf("Couldn't open triangles.out\n");
exit(-3);
}

/* Print the results formatted nicely to the output file */
fprintf(ofp, "\n\tSide A\t\tSide B\t\tHypotenuse\n\n");

for (i = 0 ; i < items ; i++)
{
fprintf(ofp, "\t%6.3f\t\t%6.3f\t\t%6.3f\n", triangles[i].sideA,
triangles[i].sideB, triangles[i].hypotenuse);
}
fprintf(ofp, "\n");

/* Close the output file since we're finished writing to it */
fclose(ofp);

/* Give up use of the memory block */
free(triangles) ;

return 0;
}

```

## The Sample Run

```
linux1[519] a.out
linux1[520] cat triangles.out

Side A          Side B          Hypotenuse

3.000           4.000           5.000
3.250           5.750           6.605
4.500           7.250           8.533
5.000           8.000           9.434
6.000           8.000          10.000

linux1[521]

```

CSEE | 201 | 201 F'06 | lectures | news | help