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

CODE IN C PLEASE You are to write a program to read this data from the txt file

ID: 3864143 • Letter: C

Question

CODE IN C PLEASE

You are to write a program to read this data from the txt file planets.txt which is attached to the assignment. The blanks in the left label column are entered as _ in the txt file for ease of input. They should be converted to blanks for the output by your program. Note that you should put the file planets.txt in your project folder for this program.

Do not put it in the src or debug subfolders. Your program is to read the data and print the table with the planets and all associated data ordered from left to right by descending order of density. Do not print the input data as it is read, i.e. your program produces one table.

Your table should look similar to the one above but of course the planets will be in a different order. Your program is to also store the sorted table in a new csv file called sortedPlanets.csv which should be created in the project folder for this project. Note that csv is a text file format where the data fields are separated by commas. You should study the format of the csv file before starting (see for example https://en.wikipedia.org/wiki/Comma-separated_values ). Make sure your csv file will open in Excel.

Problem 4 Consider the information in the following table (note Pluto is included even though it has been downgraded from planetary status) MERCURY VENUS EARTH MARS JUPITER SATURN URANUS NEPTUNE PLUTO Mass (1024kg) 0.642 1898 0.0146 Diameter (km) 4879 12104 12756 6792 142984 120536 51118 49528 2370 Density (kg/m 3) 5427 5243 5514 3933 1326 687 1271 1638 2095 Gravity (m/sA 2) 3.7 8.9 9.8 3.7 23.1 9 8.7 11 0.7 43 10.4 11.2 5 59.5 35.5 21.3 23.5 1.3 Escape Velocity (km/s) 1407.6 -5832.5 23.9 24.6 9.9 10.7 -17.2 16.1 -153.3 Rotation Period(hours) Length of Day (hours) 4222.6 2802 24 24.7 9.9 10.7 17.2 16.1 153.3 Distance from Sun(106km) 57.9 108.2 149.6 227.9 778.6 1433.5 2872.5 4495.1 5906.4 46 107.5 147.1 206.6 7405 1352.6 2741.3 4444.5 4436.8 Perihelion (106km). Aphelion (106km) 69.8 108.9 152.1 249.2 816.6 1514.5 3003.6 4545.7 7375.9 88 224.7 365.2 687 4331 10747 30589 59800 90560 Orbital Period(days) orbital velocity (km/s) orbital Inclination (degrees) 7 3.4 o 1.9 1.3 2.5 0.8 1.8 17.2 orbital Eccentricity 0.205 0.007 0.017 0.094 0.049 0.057 0.046 0.011 0.244 obliquity to orbit degrees) 0.01 177.4 23.4 25.2 3.1 26.7 97.8 28.3 122.5 464 15 -65 110 -140 -195 -200 -225 167 Mean Temperature(C) o 92 1 0.01 Unknown Unknown Unknown Unknown o Surface Pressure (bars) o 0 1 2 67 62 27 14 5 Number of Moons No No No No Yes Yes Yes Yes No Ring System? Yes No Yes No Yes Yes Yes Yes Unknown Global Magnetic Field? which is taken from http://nssdc.gsfc.nasa.gov/planetary/factsheet/ The symbol A denotes to the power of 24 e.g. 10A24 means 10

Explanation / Answer

The first step is to write a program which reads the file from the planets.txt factsheet, I have gone through the factsheet, it contains all the data of it.

Step-1: To demonstrates how a file should be stored on the disk is to read.

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

void main()
{
FILE *f;
char fName[15];
char ch;

printf("Enter the file to be opened: ");
scanf("%s", fName);
f = fopen(fName, "r");
  
if (f == NULL)
{
printf("Sorry.!!! Can't open file ");
exit(0);
}
  
ch = fgetc(f);
  
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(f);
}
  
fclose(f);
}

Step2: To read the data from a file,

int main()
   {
       FILE *f;
       char buff[700];

       f =fopen("planets.txt","r");
       if (!f)
       return 1;

       while (fgets(buff,700, f)!= NULL)
       printf("%s",buff);

       fclose(f);
       return 0;
   }

Step3: To store the sortedPlanets.csv into the program:-

int main()
{   
   int i=0,j=0;
   char buf[1024] ;   
char *rec,*line;
int m[100][100];   
FILE *fstream = fopen("sortedPlanets.csv","r");   
if(fstream == NULL) {
printf(" The file you are trying to read is failed.!!!");
return -1 ;   
}   
while((line=fgets(buf,sizeof(buf),fstream))!=NULL)   
{
       rec = strtok(line,";");   
       while(rec != NULL)   
           {   
           printf("rec : %s",rec) ;   
           m[i][j++] = atoi(rec) ;   
           rec = strtok(NULL,";");   
           }   
       ++i ;   
   }   
   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