C Programming: A torus is defined by its major radius (R) and minor radius (r).
ID: 3713385 • Letter: C
Question
C Programming:
A torus is defined by its major radius (R) and minor radius (r). Major radius is the distance from the center of the tube to the center of the torus. Minor radius is the radius of the tube. The volume of a torus is computed as V = 2(pi)^2 Rr^2.
Write a program that sorts the tori (multiple torus) by volume. The major radius and minor radius will be read from file tori.txt. Write the sorted torus, including volumes to a text file called sorted.txt. Assume tori.txt has the format of minor radius (double) followed by major radius (double) with each torus on a separate line:
12.2 23.4
1. Name your program tori.c.
2. The program should be built around an array of structures, with each structure containing information of a torus’s major radius, minor radius, and volume. Assume that there are no more than 1000 tori in the file.
3. Use fscanf and fprintf to read and write data.
4. Modify the selection_sort function provided to sort an array of product struct. The tori should be sorted by volume in ascending order. The function should have the following prototype:
void selection_sort(struct torus array_tori[], int n);
5. Output the sorted cylinders, including volume, in a text file called sorted.txt, in the following format.
# minor_rad major_rad Volume
1 1.400000 10.400000 402.364032
Suggestions:
1. Set up torus struct.
2. Use fscanf function to read the input file (note that fscanf returns number of entries filled).
3. Initially output unsorted array to screen to make sure the file input and array setup are working.
4. Modify the selection_sort function for processing tori.
5. Initially output sorted array to the screen.
6. When output is correct, write to the output file.
Expected in sorted.txt:
# minor_rad major_rad Volume
1 1.400000 10.400000 402.364032
2 3.000000 6.400000 1136.978427
3 2.400000 12.000000 1364.374112
4 3.500000 6.500000 1571.734501
5 2.800000 10.600000 1640.407208
6 4.100000 5.600000 1858.170160
7 3.400000 8.200000 1871.119081
8 3.500000 12.400000 2998.385817
9 3.400000 14.200000 3240.230603
10 4.500000 12.300000 4916.543432
11 7.800000 14.200000 17053.255182
12 9.100000 10.500000 17163.340750
13 9.800000 12.500000 23696.920167
14 9.400000 15.600000 27208.841240
15 10.200000 14.200000 29162.075430
16 12.400000 14.400000 43705.450734
17 13.400000 18.200000 64507.576452
18 12.200000 23.400000 68748.821812
Explanation / Answer
Below is your code
//c program for calculating volume of torus and sorting by volume
//header files including
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//tori structure having min and max radius and volume
struct tori
{
float minradius,maxradius,volume;
};
//main method implementation
int main()
{
//file descriptors and variables declarations and initializations
FILE *fp,*fq;
int i=0,len=0,j;
float pi=22.0/7.0;
struct tori torus[1000],temp;
//opening tori.txt in read mode and sort.txt in write mode
fp = fopen("tori.txt","r");
fq = fopen("sort.txt","w");
//if fp and fq are null then cant open files
if(fp == NULL)
{
printf(" Can't open file or file doesn't exist.");
exit(0);
}
if(fq == NULL)
{
printf(" Can't open file or file doesn't exist.");
exit(0);
}
//reading tori.txt file using fscanf
//calculating volume and displaying data in the tori.txt file
printf(" Data in tori.txt file... and corresponding volume ");
while(((fscanf(fp,"%f%f",&torus[i].minradius,&torus[i].maxradius))!=EOF)&&(i<1000))
{
torus[i].volume=pi*torus[i].minradius*torus[i].minradius*2*pi*torus[i].maxradius;
printf(" %f %f %f ",torus[i].minradius,torus[i].maxradius,torus[i].volume);
i++;
}
len=i;
//sorting data in ascending order by volume
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(torus[i].volume>torus[j].volume)
{
temp=torus[i];
torus[i]=torus[j];
torus[j]=temp;
}
}
}
//displaying sorted data and writing sorted data into sort.txt file using fprintf
printf(" Sorted Data in tori.txt file.in ascending order by volume and the same writing into file sort.txt ");
printf(" # minor_rad major_rad Volume");
fprintf(fq," # minor_rad major_rad Volume");
for(i=0;i<len;i++)
{
printf(" %d %f %f %f",i+1,torus[i].minradius,torus[i].maxradius,torus[i].volume);
fprintf(fq," %d %f %f %f",i+1,torus[i].minradius,torus[i].maxradius,torus[i].volume);
}
fclose(fp);
fclose(fq);
//closing file descriptors
return 0;
}//end of main method
Output
Data in tori.txt file... and corresponding volume
1.000000 1.400000 27.657141
10.400000 402.364044 859735.875000
2.000000 3.000000 237.061203
6.400000 1136.978394 920007.625000
3.000000 2.400000 426.710205
12.000000 1364.374146 3881282.250000
4.000000 3.500000 1106.285645
6.500000 1571.734497 1311853.000000
5.000000 2.800000 1382.857056
10.600000 1640.407227 3641184.500000
6.000000 4.100000 2915.853027
5.600000 1858.170166 1151173.375000
7.000000 3.400000 3291.200195
8.200000 1871.119141 2485469.000000
8.000000 3.500000 4425.142578
12.400000 2998.385742 9107730.000000
9.000000 3.400000 5440.554688
14.200000 3240.230713 12907196.000000
10.000000 4.500000 8889.795898
12.300000 4916.543457 14694316.000000
11.000000 7.800000 18644.865234
14.200000 17053.255859 67930256.000000
12.000000 9.100000 25887.085938
10.500000 17163.339844 37381756.000000
13.000000 9.800000 32718.400391
12.500000 23696.919922 73146104.000000
14.000000 9.400000 36396.796875
15.600000 27208.841797 130809280.000000
15.000000 10.200000 45337.953125
14.200000 29162.076172 116164760.000000
16.000000 12.400000 62710.589844
14.400000 43705.449219 179035760.000000
17.000000 13.400000 76503.601563
18.200001 64507.578125 422116960.000000
18.000000 12.200000 78087.960938
23.400000 68748.820313 743663040.000000
Sorted Data in tori.txt file.in ascending order by volume and the same writing i
nto file sort.txt
# minor_rad major_rad Volume
1 1.000000 1.400000 27.657141
2 2.000000 3.000000 237.061203
3 3.000000 2.400000 426.710205
4 4.000000 3.500000 1106.285645
5 5.000000 2.800000 1382.857056
6 6.000000 4.100000 2915.853027
7 7.000000 3.400000 3291.200195
8 8.000000 3.500000 4425.142578
9 9.000000 3.400000 5440.554688
10 10.000000 4.500000 8889.795898
11 11.000000 7.800000 18644.865234
12 12.000000 9.100000 25887.085938
13 13.000000 9.800000 32718.400391
14 14.000000 9.400000 36396.796875
15 15.000000 10.200000 45337.953125
16 16.000000 12.400000 62710.589844
17 17.000000 13.400000 76503.601563
18 18.000000 12.200000 78087.960938
19 10.400000 402.364044 859735.875000
20 6.400000 1136.978394 920007.625000
21 5.600000 1858.170166 1151173.375000
22 6.500000 1571.734497 1311853.000000
23 8.200000 1871.119141 2485469.000000
24 10.600000 1640.407227 3641184.500000
25 12.000000 1364.374146 3881282.250000
26 12.400000 2998.385742 9107730.000000
27 14.200000 3240.230713 12907196.000000
28 12.300000 4916.543457 14694316.000000
29 10.500000 17163.339844 37381756.000000
30 14.200000 17053.255859 67930256.000000
31 12.500000 23696.919922 73146104.000000
32 14.200000 29162.076172 116164760.000000
33 15.600000 27208.841797 130809280.000000
34 14.400000 43705.449219 179035760.000000
35 18.200001 64507.578125 422116960.000000
36 23.400000 68748.820313 743663040.000000
--------------------------------
Process exited after 0.3748 seconds with return value 0
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.