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

Write a program that sorts the tori (multiple torus) by volume. The major radius

ID: 3709412 • Letter: W

Question

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 …

The function should have the following prototype: void selection_sort(struct torus array_tori[], int n);

Name your program tori.c.

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.

Use fscanf and fprintf to read and write data.

Modify the selection_sort function provided to sort an array of product struct. The tori should be sorted by volume in ascending order.

Output the sorted cylinders, including volume, in a text file called sorted.txt, in the following format.

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

* torus.c

* This program demonstrates the creation of a display list.

*/

#include <GL/glut.h>

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#define PI_ 3.14159265358979323846

GLuint theTorus;

/* Draw a torus */

static void torus(int numc, int numt)

{

int i, j, k;

double s, t, x, y, z, twopi;

twopi = 2 * PI_;

for (i = 0; i < numc; i++) {

glBegin(GL_QUAD_STRIP);

for (j = 0; j <= numt; j++) {

for (k = 1; k >= 0; k--) {

s = (i + k) % numc + 0.5;

t = j % numt;

x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);

y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);

z = .1 * sin(s * twopi / numc);

glVertex3f(x, y, z);

}

}

glEnd();

}

}

/* Create display list with Torus and initialize state */

static void init(void)

{

theTorus = glGenLists (1);

glNewList(theTorus, GL_COMPILE);

torus(8, 25);

glEndList();

glShadeModel(GL_FLAT);

glClearColor(0.0, 0.0, 0.0, 0.0);

}

/* Clear window and draw torus */

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 1.0);

glCallList(theTorus);

glFlush();

}

/* Handle window resize */

void reshape(int w, int h)

{

glViewport(0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

}

/* Rotate about x-axis when "x" typed; rotate about y-axis

when "y" typed; "i" returns torus to original view */

void keyboard(unsigned char key, int x, int y)

{

switch (key) {

case 'x':

case 'X':

glRotatef(30.,1.0,0.0,0.0);

glutPostRedisplay();

break;

case 'y':

case 'Y':

glRotatef(30.,0.0,1.0,0.0);

glutPostRedisplay();

break;

case 'i':

case 'I':

glLoadIdentity();

gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

glutPostRedisplay();

break;

case 27:

exit(0);

break;

}

}

int main(int argc, char **argv)

{

glutInitWindowSize(200, 200);

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutCreateWindow(argv[0]);

init();

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutDisplayFunc(display);

glutMainLoop();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote