[SOLVED] CSC60 Lab 4-On File Handling and Loops

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon Lab-4-a7mdkn.zip (753.5 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (1 vote)

 

PROBLEM:
Write a program to compute the perimeter and area of a polygon using an input data file and an output file. A file lab4.c is provided with some of the print statements in it.

 It is your responsibility to read the Power Point files:
o C-3 Characters ControlStructures.pptx, #20-57 on the various Ifs and Switch o C-4 Loops.pptx, #1-24
o Ask any needed questions in class

To get the files you need, first move to your csc60 directory: cd csc60
The following command will create a directory named lab4 and put all the needed files into it below your csc60 directory.

Type: cp -R /gaia/home/faculty/bielr/files_csc60/lab4 .
Spaces needed: (1) After the cp ↑ space & dot

(2) After the -R
(3) After the directory name at the end & before the dot.

After the files are in your account and you are still in csc60, you need to type: chmod 755 lab4 This will give permissions to the directory.
Next move into lab4 directory [cd lab4], and type: chmod 644 lab4*.*
This will give permissions to the files.

Your new lab4 directory should now contain: lab4.c, lab4.dat, lab4sample.dat

INPUT/OUTPUT DESCRIPTION:

  •   The test data will be a file called lab4sample.dat. Use it to verify the correctness of your

    program. It has 3 sets of data.

  •   The final data will be a file called lab4.dat. It has 6 sets of data.
  •   Each line or record of the file will consist of two numbers: the radius and the number of sides

    of the polygon.

  •   Print your name and assignment (use fprintf)
  •   Use an fscanf statement in a while loop to repeatedly get each set of values. It would be a

    good idea to make these variable type double.

  •   The output will be a file, lab4.out. The output of the sample data will follow.

    FORMULAS

 Remember to translate the algebra of the two formulas into the C language. Perimeter of the polygon = 2n R sin PI

n Areaofthepolygon=1⁄2nR2 sin2PI

n

DATA FILES:
There are two data files:

 lab4sample.dat – Use it to verify the correctness of your program  lab4.dat

CSC 60. Lab 4. On File Handling and Loops.

Page 2 of 3

ALGORITHM DEVELOPMENT:
Open the data file lab4sample.dat or lab4.dat Do the appropriate error checking

Open the output file lab4.out
Do the appropriate error checking

Print your name and assignment. Then print the column header lines needed. (use fprintf)

while ((fscanf(…, &radius, &nsides)) == 2)
| Compute the perimeter and area of the polygon.
|_ fprintf the radius, nsides, perimeter, and area as in the Defined Output Appearance.

Close the two files

VIEWING OUTPUT
When you run the program, the whole thing is going to lab4.out. Open that file to see your output. Use either “cat” or “vim”.

REMINDERS:

  •   Include your name and lab4 in your comment block, and in your output.
  •   All numeric variables are to be type double.
  •   Most of the print and fprintf statements are included in lab4.c for you. You need to write the

    fprintf in the loop.

  •   The input file name, which will be changed, ought to be in a #define statement. The file will

    come with two #define statements, for the test file and the final file. Just move the // from in front of one #define statement to the other #define statement.

    #define INFILE lab4sample.dat

    // #define INFILE lab4.dat

  •   FOR THE VALUE OF PI, use M_PI from math.h (which we already have included).
  •   To compile, you will need to add –lm so math.h can be found. Type: gcc –lm lab4.c

    DEFINED OUTPUT APPEARANCE (using lab4sample.out): Your Name. Lab 4.

 Radius
--------
  12.60
   5.60
   7.85
 Number
Of Sides
--------
  24.00
   8.00
  12.00
  Perimeter
 Of Polygon
------------
    78.9422
    34.2884
    48.7615

Area Of

  Polygon
-----------
  493.0813
   88.6995
  184.8675
  • Lab-4-a7mdkn.zip