Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The programming assignment for Chapter 8 is designed to challenge your understan

ID: 3817090 • Letter: T

Question

The programming assignment for Chapter 8 is designed to challenge your understanding of arrays, repetition, and selection. Your program should: 1. Introduce the program to the user. 2. Obtain 2 file names from the user. One contains the names of several simple geometric shapes. 3. Read the shape name file into an array of shape names. 4. In the second file, each line contains two floating point numbers associated with each shape (each line corresponds to the names in the other file): area and perimeter. These numbers are to be read into a multidimensional array. 5. Your program should create three different output files: Shapes.dat, Areas.dat, and Perimeter.dat. The data in each of these output files should be presented in a table of aligned columns. The first column is shape name, the second is area and the third is perimeter. a. Shapes.dat should contain the data sorted with shape names in alphabetical order (the associated data should appear on the same line as the shape name). b. Areas.dat should contain the same data, but sorted from the smallest area to the largest. c. Perimeter.dat should contain the same data, but sorted from the smallest perimeter to the largest. Remember to arrange the data in the appropriate order.

Explanation / Answer

#include<string.h>
#include<fstream.h>
#include<iostream>
using namespace std;
main()
{
   ifstream in;
   ofstream out;
   int j,n,i;
   char name[10][100],temp[100],x[20],y[20];
   float data[10][2],t;           //Column 0 indicates Areas and Column 1 indicates Perimeter
   cout<<"This is a program which takes two file name as input. One of the files store name of some geographic shapes. The other file contains corressponding areas and perimeter. Then 3 files are generated named Shapes.dat, Areas.dat and Perimeter.dat";
   cout<<" Enter two filename:";
   cin>>x>>y;
   in.open(x);
   j=0;
   while(!in.eof())
       in.getline(name[j++],100);   //Reading shape names
   in.close();
   n=j;                   //n contains number of Shapes present in the file
   j=0;
   in.open(y);
   while(!in.eof())
   {
       in>>data[j][0];           //Reading the area from file
       in>>data[j++][1];       //Reading the perimeters from file
   }
   in.close();

   for(i=0;i<n;i++)                   //Bubble Sort technique
       for(j=0;j<n-1;j++)
           if(strcmp(name[j],name[j+1])>0)       //Sorting the arrays in alphabetical order
           {
           //Swapping portion
           strcpy(temp,name[j]);
           strcpy(name[j],name[j+1]);
           strcpy(name[j+1],temp);

           t=data[j][0];
           data[j][0]=data[j+1][0];
           data[j+1][0]=t;

           t=data[j][1];
           data[j][1]=data[j+1][1];
           data[j+1][1]=t;
           }
   out.open("Shapes.dat");
   for(i=0;i<n;i++)
       out<<name[i]<<" "<<data[i][0]<<" "<<data[i][1]<<endl;       //Storing the data in file "Shapes.dat
   out.close();

   for(i=0;i<n;i++)                   //Bubble Sort technique
       for(j=0;j<n-1;j++)
           if(data[j][0]>data[j+1][0])       //Sorting the arrays with respect to area in ascending order
           {
           //Swapping portion
           strcpy(temp,name[j]);
           strcpy(name[j],name[j+1]);
           strcpy(name[j+1],temp);

           t=data[j][0];
           data[j][0]=data[j+1][0];
           data[j+1][0]=t;

           t=data[j][1];
           data[j][1]=data[j+1][1];
           data[j+1][1]=t;
           }
   out.open("Areas.dat");
   for(i=0;i<n;i++)
       out<<name[i]<<" "<<data[i][0]<<" "<<data[i][1]<<endl;       //Storing the data in "Areas.dat" file
   out.close();

   for(i=0;i<n;i++)                   //Bubble Sort technique
       for(j=0;j<n-1;j++)
           if(data[j][1]>data[j+1][1])       //Sorting the arrays with respect to perimeter in ascending order
           {
           //Swapping portion
           strcpy(temp,name[j]);
           strcpy(name[j],name[j+1]);
           strcpy(name[j+1],temp);

           t=data[j][0];
           data[j][0]=data[j+1][0];
           data[j+1][0]=t;

           t=data[j][1];
           data[j][1]=data[j+1][1];
           data[j+1][1]=t;
           }
   out.open("Perimeter.dat");
   for(i=0;i<n;i++)
       out<<name[i]<<" "<<data[i][0]<<" "<<data[i][1]<<endl;       //Storing the data in "Perimeter.dat" file
   out.close();

}