Question

In: Computer Science

In the C programming language: Pretend you are an astrophysicist making a report on the distances...

In the C programming language:

Pretend you are an astrophysicist making a report on the distances to the 50 brightest stars.
You need to print out a nice table sorted by distance to include in your report.

Using data in input.txt, write a program that can read in the included file input.txt.

Use scanf or fscanf (or any other function you prefer) to read the comma separated values (CSV) into an array of structs representing each record.

Once all records are loaded, sort the list by the distance field. Alternatively, you can keep the array
sorted by its distance during reading. Use any sorting function you want.

Save the data sorted by the distance field (lowest to highest) in another file called "distance.txt".
Save only the following fields in this order:

   Distance
   Name
   Bayer Name
   right ascension
   declination

Make sure that the columns line up nicely and that the header is printed before the data.

input.txt contains:

Rank,Star Name, Bayer Name, Visual Mag., Abs. Mag., Dist. (ly), Spectral Type, Lum. (Sol), Mass (Sol), Diam. (Sol), RA (hr_min), Dec (deg)
1, Sirius, Alp CMa, -1.44, 1.45, 8.6, A1V, 25, 2.0, 1.7, 06h 45m, -16.7
2, Canopus, Alp Car, -0.62, -5.53, 310, F0Ib, 13600, 8.5, 65, 06h 24m, -52.7
3, Rigil Kent., Alp Cen, -0.28c, 4.34, 4.4, G2V+K1V, 1.5, 1.1, 1.2, 14h 40m, -60.8
4, Arcturus, Alp Boo, -0.05v, -0.31, 36.7, K2III, 170, 1.1, 26, 14h 16m, +19.2
5, Vega, Alp Lyr, 0.03v, 0.58, 25.3, A0V, 37, 2.1, 2.3, 18h 37m, +38.8
6, Capella, Alp Aur, 0.08v, -0.48, 42.2, G5III+G0II, 79, 2.7, 12, 05h 17m, +46.0
7, Rigel, Bet Ori, 0.18v, -6.69, 770, B8Ia, 66000, 17, 78, 05h 15m, -8.2
8, Procyon, Alp CMi, 0.40, 2.68, 11.4, F5IV-V, 7.7, 1.5, 2.0, 07h 39m, +5.2
9, Betelgeuse, Alp Ori, 0.45v, -5.14, 430, M2Ib, 105000, 18, 936, 05h 55m, +7.4
10, Achernar, Alp Eri, 0.45v, -2.77, 144, B3V, 3300, 6-8, 10, 01h 38m, -57.2
11, Hadar, Bet Cen, 0.61v, -5.42, 525, B1III, 16000, 10.7, 8, 14h 04m, -60.4
12, Altair, Alp Aql, 0.76v, 2.20, 16.8, A7V, 10.6, 1.8, 1.8, 19h 51m, +8.9
13, Acrux, Alp Cru, 0.77c, -4.19, 320, B0.5IV+B1V, 25000, 14, ?, 12h 27m, -63.1
14, Aldebaran, Alp Tau, 0.87, -0.63, 65.1, K5III, 425, 1.7, 44.2, 04h 36m, +16.5
15, Spica, Alp Vir, 0.98v, -3.55, 260, B1V+B2V, 13400, 11, 7.8, 13h 25m, -11.2
16, Antares, Alp Sco, 1.06v, -5.28, 605, M1Ib+B4V, 65000, 15.5, 800, 16h 29m, -26.4
17, Pollux, Bet Gem, 1.16, 1.09, 33.7, K0III, 32, 1.9, 8, 07h 45m, +28.0
18, Fomalhaut, Alp PsA, 1.17, 1.74, 25.1, A3V, 17.7, 2.1, 1.8, 22h 58m, -29.6
19, Deneb, Alp Cyg, 1.25v, -8.73, 3200, A2Ia, 54000, 20, 110, 20h 41m, +45.3
20, Mimosa, Bet Cru, 1.25v, -3.92, 350, B0.5III, 34000, 14, 8, 12h 48m, -59.7
21, Regulus, Alp Leo, 1.36, -0.52, 77.5, B7V, 150, 3.5, 3.2, 10h 08m, +12.0
22, Adhara, Eps CMa, 1.50, -4.10, 430, B2II, 20000, 10, ?, 06h 59m, -29.0
23, Castor, Alp Gem, 1.58c, 0.59, 51.5, A1V+A2V, 30/14, 2.2/1.7, 2.3/1.6, 07h 35m, +31.9
24, Gacrux, Gam Cru, 1.59v, -0.56, 87.9, M3.5III, 1500, 3, 113, 12h 31m, -57.1
25, Shaula, Lam Sco, 1.62v, -5.05, 700, B2IV, ?, 10.4, 6.2, 17h 34m, -37.1
26, Bellatrix, Gam Ori, 1.64, -2.72, 240, B2III, 21500, 8, 5.7, 05h 25m, +6.3
27, Elnath, Bet Tau, 1.65, -1.37, 131, B7III, 70, 4.5, 5.5, 05h 26m, +28.6
28, Miaplacidus, Bet Car, 1.67, -0.99, 111, A2III, 210, 3, 5.7, 09h 13m, -69.7
29, Alnilam, Eps Ori, 1.69v, -6.38, 1300, B0Ia, 375000, 40, 26, 05h 36m, -1.2
30, Alnair, Alp Gru, 1.73, -0.73, 101, B7IV, 380, 4, 3.6, 22h 08m, -47.0
31, Alnitak, Zet Ori, 1.74c, -5.26, 820, O9.5Ib+B0I, 100000, 28, 20, 05h 41m, -1.9
32, Regor, Gam Vel, 1.75v, -5.31, 840, WC8+O9Ib, 100000, 30, 13, 08h 10m, -47.3
33, Alioth, Eps UMa, 1.76v, -0.21, 80.9, A0IV, 108, 3, 3.7, 12h 54m, +56.0
34, Kaus Aust., Eps Sgr, 1.79, -1.44, 145, B9.5III, 375, 5, 7, 18h 24m, -34.4
35, Mirfak, Alp Per, 1.79, -4.50, 590, F5Ib, 5400, 11, 56, 03h 24m, +49.9
36, Dubhe, Alp UMa, 1.81, -1.08, 124, K0III+F0V, 300, 4, 30, 11h 04m, +61.8
37, Wezen, Del CMa, 1.83, -6.87, 1800, F8Ia, 50000, 17, 200, 07h 08m, -26.4
38, Alkaid, Eta UMa, 1.85, -0.60, 101, B3V, 700, 6, 1.8, 13h 48m, +49.3
39, Sargas, The Sco, 1.86c, -2.75, 270, F1II, 960, 3.7, 20, 17h 37m, -43.0
40, Avior, Eps Car, 1.86v, -4.58, 630, K3II+B2V, 6/11 K, 4.6/16, 153/6, 08h 23m, -59.5
41, Menkalinan, Bet Aur, 1.90v, -0.10, 82.1, A2IV, 48, 2.4, 2.8, 06h 00m, +44.9
42, Atria, Alp TrA, 1.91, -3.62, 415, K2Ib-II, 5500, 7, ?, 16h 49m, -69.0
43, Koo She, Del Vel, 1.93, -0.01, 79.7, A0V, ?, ?, ?, 08h 45m, -54.7
44, Alhena, Gam_Gem, 1.93, -0.60, 105, A0IV, 160, 2.8, 4.4, 06h 38m, +16.4
45, Peacock, Alp Pav, 1.94, -1.81, 180, B0.5V+B2V, 2100, 5, 4.4, 20h 26m, -56.7
46, Polaris, Alp UMi, 1.97v, -3.64, 430, F7Ib-II, 2200, 7.5, 30, 02h 32m, +89.3
47, Mirzam, Bet CMa, 1.98v, -3.95, 500, B1III, 19000, ?, ?, 06h 23m, -18.0
48, Alphard, Alp Hya, 1.99, -1.69, 180, K3II, ?, 3.0, 50.5, 09h 28m, -8.7
49, Algieba, Gam Leo, 2.01, -0.92, 126, K0III+G7II, 320/50, 1.23, 32, 10h 20m, +19.8
50, Hamal, Alp Ari, 2.01, 0.48, 65.9, K2III, 90, 2, 15, 02h 07m, +23.5

Solutions

Expert Solution

C Program

#include<stdio.h>
#include<stdlib.h>

typedef struct starnode //details of star only distance is given float and every other info is stored as string
{
        int rank;
        char star_name[50];
        char bayer_name[50];
        char visual_mag[20];
        char abs_mag[20];
        float distance;
        char spectral_type[20];
        char lum[20];
        char mass[20];
        char diam[20];
        char right_ascension[20];
        char declination[20];

} STAR; //renaming "sturct starnode" delcaration to "STAR" using typedef

int comparator(const void *p, const void *q)    //this comparator operation should be passed as parameter to the qsort function (quick sort)
{
        return ( ((STAR*)p)->distance - ((STAR*)q)->distance );
}

int main()
{
        FILE* fp = fopen("input.txt", "r");     //opening input.txt in read mode
        STAR stars[50]; //creating stucture array to read and store from csv formatted input.txt
        char headings[1000];    
        fgets(headings, 1000, fp);      //this is to just strip away the headers in input.txt
        for(int i=0;i<50;i++)        //looping until 50 lines of input.txt 
                /*
                        scanning all fields from each line of input.txt at a time using fscanf
                        -> the notation %50[^,] reads characters until next , if there is no comma then it reads upto 50 characters
                        -> it is always good practice to specify the size as the program may get vulnerable to buffer overflow attacks
                */
                fscanf(fp, "%d,%50[^,],%50[^,],%20[^,],%20[^,],%f,%20[^,],%20[^,],%20[^,],%20[^,],%20[^,],%s",
                                 &stars[i].rank,
                                 stars[i].star_name,
                                 stars[i].bayer_name,
                                 stars[i].visual_mag,
                                 stars[i].abs_mag,
                                 &stars[i].distance,
                                 stars[i].spectral_type,
                                 stars[i].lum,
                                 stars[i].mass,
                                 stars[i].diam,
                                 stars[i].right_ascension,
                                 stars[i].declination   );
        fclose(fp);

        //opening distance.txt in write mode
        fp = fopen("distance.txt", "w");

        /*
                The in-built function qsort takes in array of structures as a void pointer ,the size of array, sizeof each structure and finally
                a pointer to comparator function
                Behaviour of comparator function should be 
                        The user should compare structures based on their fields in custom way of comparison
                        and return a negative value if first structure is less than the second in some way else if greater must return positive value 
                        and 0 if both are equal 
        */
        qsort((void*)stars, 50, sizeof(STAR), comparator);      


        //printing the sorted results in a tabular format

        printf("| Distance | Name       | Bayer Name    | Right Ascension\t| Declination\t|\n");
        for(int i=0;i<50;i++)
                printf("| %9.2f|%12s| %s\t| %s\t\t| %s\t\t|\n",
                                 stars[i].distance,
                                 stars[i].star_name,
                                 stars[i].bayer_name,
                                 stars[i].right_ascension,
                                 stars[i].declination   );

        //writing the results to distance.txt in a csv format

        fprintf(fp, "Distance,Name,Bayer Name,Right Ascension,Declination\n");

        for(int i=0;i<50;i++)
                fprintf(fp, "%.2f,%s,%s,%s,%s\n",
                                 stars[i].distance,
                                 stars[i].star_name,
                                 stars[i].bayer_name,
                                 stars[i].right_ascension,
                                 stars[i].declination   );
        return 0;
}

C Program Screenshots

Distance.txt

Distance,Name,Bayer Name,Right Ascension,Declination
4.40,RigilKent.,AlpCen,14h40m,-60.8
8.60,Sirius,AlpCMa,06h45m,-16.7
11.40,Procyon,AlpCMi,07h39m,+5.2
16.80,Altair,AlpAql,19h51m,+8.9
25.30,Vega,AlpLyr,18h37m,+38.8
25.10,Fomalhaut,AlpPsA,22h58m,-29.6
33.70,Pollux,BetGem,07h45m,+28.0
36.70,Arcturus,AlpBoo,14h16m,+19.2
42.20,Capella,AlpAur,05h17m,+46.0
51.50,Castor,AlpGem,07h35m,+31.9
65.10,Aldebaran,AlpTau,04h36m,+16.5
65.90,Hamal,AlpAri,02h07m,+23.5
77.50,Regulus,AlpLeo,10h08m,+12.0
79.70,KooShe,DelVel,08h45m,-54.7
80.90,Alioth,EpsUMa,12h54m,+56.0
82.10,Menkalinan,BetAur,06h00m,+44.9
87.90,Gacrux,GamCru,12h31m,-57.1
101.00,Alnair,AlpGru,22h08m,-47.0
101.00,Alkaid,EtaUMa,13h48m,+49.3
105.00,Alhena,Gam_Gem,06h38m,+16.4
111.00,Miaplacidus,BetCar,09h13m,-69.7
124.00,Dubhe,AlpUMa,11h04m,+61.8
126.00,Algieba,GamLeo,10h20m,+19.8
131.00,Elnath,BetTau,05h26m,+28.6
144.00,Achernar,AlpEri,01h38m,-57.2
145.00,KausAust.,EpsSgr,18h24m,-34.4
180.00,Peacock,AlpPav,20h26m,-56.7
180.00,Alphard,AlpHya,09h28m,-8.7
240.00,Bellatrix,GamOri,05h25m,+6.3
260.00,Spica,AlpVir,13h25m,-11.2
270.00,Sargas,TheSco,17h37m,-43.0
310.00,Canopus,AlpCar,06h24m,-52.7
320.00,Acrux,AlpCru,12h27m,-63.1
350.00,Mimosa,BetCru,12h48m,-59.7
415.00,Atria,AlpTrA,16h49m,-69.0
430.00,Betelgeuse,AlpOri,05h55m,+7.4
430.00,Adhara,EpsCMa,06h59m,-29.0
430.00,Polaris,AlpUMi,02h32m,+89.3
500.00,Mirzam,BetCMa,06h23m,-18.0
525.00,Hadar,BetCen,14h04m,-60.4
590.00,Mirfak,AlpPer,03h24m,+49.9
605.00,Antares,AlpSco,16h29m,-26.4
630.00,Avior,EpsCar,08h23m,-59.5
700.00,Shaula,LamSco,17h34m,-37.1
770.00,Rigel,BetOri,05h15m,-8.2
820.00,Alnitak,ZetOri,05h41m,-1.9
840.00,Regor,GamVel,08h10m,-47.3
1300.00,Alnilam,EpsOri,05h36m,-1.2
1800.00,Wezen,DelCMa,07h08m,-26.4
3200.00,Deneb,AlpCyg,20h41m,+45.3

Distance.txt Screenshot

Output

Each and everything is explained in the c program comments.

Thank you! If you like my work, hit like.


Related Solutions

C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is a disturbance of one of more fields such that the field values oscillate repeatedly about a stable equilibrium value. Waves are usually represented using mathematical functions of the form F (x, t), where x = position and t = time. Your task is to write a program that will visualize a given wave for exactly N seconds. You do not need to worry about...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is a sub class is derived from the Account class and implements the ITransaction interface. There are two class variables i.e. variables that are shared but all the objects of this class. A short description of the class members is given below: CheckingAccount Class Fields $- COST_PER_TRANSACTION = 0.05 : double $- INTEREST_RATE = 0.005 : double - hasOverdraft: bool Methods + «Constructor» CheckingAccount(balance =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT