Please help. I cannot get this to work. Below is what the assignment asked for.
ID: 3685364 • Letter: P
Question
Please help. I cannot get this to work. Below is what the assignment asked for.
Write a program that read the data in the file Storm_Data.txt (uploaded to laulima) and then switch the rows and columns, and write the data to a file name Rotate_Data.txt with header information. For example, your rotated data file should look something like Rotate_Data.txt.
Below is the code that I wrote. I cannot figure out how to rotate the the data text. Please help me with the solution.
DATA TEXT PROVIDED
Hazel 1954 4 144.3
Audrey 1957 4 157.0
Donna 1960 4 133.1
Carla 1961 4 157.7
Camille 1969 5 166.6
Celia 1970 3 111.1
Fred 1979 3 123.4
Allen 1980 3 128.9
Gloria 1985 3 121.2
Hugo 1989 4 130.0
Andrew 1992 5 200
Opal 1995 3 119.8
BlaBla 2017 2 100
Unknown 1901 1 75.9
Below is what the data text is supposed to look like
1 Hurricane Name -> |Hazel |Audrey |Donna |Carla |Camille |Celia |Frederic |Allen |Gloria |Hugo |Andrew |Opal |BlaBla |Unknown
2 Year -> |1954 |1957 |1960 |1961 |1969 |1970 |1979 |1980 |1985 |1989 |1992 |1995 |2017 |1901
3 Category -> |4 |4 |4 |4 |5 |3 |3 |3 |3 |4 |5 |3 |2 |1
4 Windspeed(MPH) |144. |157.00 |133.10 |157. |166.60 |111. |123.40
Below is what i have so far but Im stuck. PLease Help
/* This program reads the information for a hurricane */
/* from a data file and then prints it. */
#include <stdio.h>
#define FILENAME "storms2.txt"
/* Define structure to represent a hurricane. */
struct hurricane
{
char name[10];
int year, category;
double speed;
};
int main(void){
/* Declare variables. */
struct hurricane h1;
FILE *storms;
FILE *fptr;
/* Read and print information from the file. */
storms = fopen(FILENAME,"r");
if (storms == NULL)
printf("Error opening data file. ");
fptr=(fopen("student.txt","w"));
if(fptr==NULL){
printf("Error!");}
else{
while (fscanf(storms,"%s %d %d %lf", h1.name, &h1.year, &h1.category, &h1.speed) == 4){
printf("%s %d %d %.1lf ",h1.name, h1.year, h1.category, h1.speed);
//printf("|%s ",h1.name);
//printf("|%d ",h1.year);
//printf("|%d ",h1.category);
//printf("|%.1lf ",h1.speed);
//fprintf(fptr,"%s %d %d %.1lf ",h1.name, h1.year, h1.category, h1.speed);
}
fclose(storms);
fclose(fptr);
}
/* Exit program. */
return 0;
}
Explanation / Answer
Note:
The given code is modified to rotate the given data and stores into the text file.
Solution:
#include <stdio.h>
#define FILENAME "storms2.txt"
/* Define structure to represent a hurricane. */
struct hurricane
{
char name[10];
int year, category;
double speed;
};
int main(void)
{
/* Declare variables. */
struct hurricane h1[100];
FILE *storms;
FILE *fptr;
int n=0,i=0;
/* Read and print information from the file. */
storms = fopen(FILENAME,"r");
if (storms == NULL)
printf("Error opening data file. ");
fptr=(fopen("Rotate_Data.txt","a"));
if(fptr==NULL){
printf("Error!");}
else
{
while (fscanf(storms,"%s %d %d %lf", h1[n].name, &h1[n].year, &h1[n].category, &h1[n].speed) == 4){
printf("%s %d %d %.1lf ",h1[n].name, h1[n].year, h1[n].category, h1[n].speed);n++;
}
for(i=0;i<n;i++)
{
fprintf(fptr,"%s ",h1[i].name);
}
fprintf(fptr," ");
for(i=0;i<n;i++)
{
fprintf(fptr,"%d ",h1[i].year);
}
fprintf(fptr," ");
for(i=0;i<n;i++)
{
fprintf(fptr,"%d ",h1[i].category);
}
fprintf(fptr," ");
for(i=0;i<n;i++)
{
fprintf(fptr,"%.1lf ",h1[i].speed);
}
fprintf(fptr," ");
fclose(storms);
fclose(fptr);
}
/* Exit program. */
return 0;
}