Is it possible that you can give me some clues on how to do this program on C++
ID: 3816559 • Letter: I
Question
Is it possible that you can give me some clues on how to do this program on C++ because I'm completely lost and frustrated.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
Hi, you can create a code like this in C++
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
string shape,area;
cout<<" Welcome to file reader";
cout<<" Enter file having shape names: ";
cin>>shape;
cout<<" Enter the file name having area and perimenter: ";
cin>>area;
ifstream fs(shape,ios::in);
ifstream fa(area,ios::in);
string temp,sar[20];
//sar is shape array
int c;
//c will hold the number of values
c=0;
float aar[20][2],a;
//a and b will be used for sorrting
//area and perimeter array
while(!fs.eof())
{
fs>>temp;
sar[c]=temp;
c++;
}
c=0;
while(!fa.eof())
{
fa>>aar[c][0]; //this will store area
fa>>aar[c][1]; //this will store perimeter
c++;
}
int i,j;
//sorting data string wise
for(i=1;i<c;++i)
{
for(j=0;j<(c-i);++j)
if(sar[j]>sar[j+1])
{
temp=sar[j];
sar[j]=sar[j+1];
sar[j+1]=temp;
//we also need to swap perimeters and area
a=aar[j][0];
aar[j][0]=aar[j+1][0];
aar[j+1][0]=a;
a=aar[j][1];
aar[j][1]=aar[j+1][1];
aar[j+1][1]=a;
}
}
//writing data to file
ofstream ofs("/Users/hitansh/Desktop/shape.dat",ios::out|ios::binary);
for(i=0;i<c;i++)
{
ofs<<sar[i]<<" "<<aar[i][0]<<" "<<aar[i][1]<<endl;
}
cout<<" Data written in shape.dat file";
return 0;
}
I have implemented sorting for shapes alphabetically
You can easily implement the rest 2
Output:
Welcome to file reader
Enter file having shape names: /Users/hitansh/Desktop/f1.txt
Enter the file name having area and perimenter: /Users/hitansh/Desktop/f2.txt
Data written in shape.dat file
f1 file:
rect
tri
square
f2 file:
12 21
12 212
44 441
shapes.dat file
rect 12 21
square 44 441
tri 12 212
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.