Starting from:

$35

CSC60- Lab 4: On File Handling and Loops Solved

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                                                            

             Area of the polygon = ½ n R2 sin 2 PI

                                                                         n

DATA FILES:

There are two data files:

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

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. 

 

            Number      Perimeter      Area Of 

 Radius    Of Sides    Of Polygon      Polygon 

--------   --------   ------------   ----------- 

  12.60      24.00        78.9422      493.0813 

   5.60       8.00        34.2884       88.6995 

   7.85      12.00        48.7615      184.8675

 

 

PREPARE YOUR FILE FOR GRADING: 

Make sure your program has been corrected from:

using lab4sample.dat to using lab4.dat  and has been re-complied.

 

When all is well and correct,  

at the prompt, type:  script StudentName_lab4.txt       [Script will keep a log of your session.]

At the prompt, type:  gcc -lm lab4.c  
to compile the code
At the prompt, type:  a.out                 
to run the program
At the prompt, type:  cat lab4.out      After the program run is complete,  
to show contents of the output file
                            type: exit                   
to leave the script session
 

Turn in your completed session:   

Go to Canvas and turn in two files:

1.      lab4.c

2.      StudentName_lab4.txt

 

More products