Hello, i am struggling with getting data from a file, using 2-dim array. I try t
ID: 3622975 • Letter: H
Question
Hello, i am struggling with getting data from a file, using 2-dim array. I try to code it may different way, but I still can't quiet figure out.There is the question.Each diver's name is 20 characters long followed by the difficulty factor of the dive and a score from each of 5 judges.
KNIFE JACK 1.3 6.0 5.1 6.3 5.9 6.5
WILLIAMSON FLIP A 1.4 3.5 4.1 4.7 7.2 3.8
SOMMER TODD 1.2 8.0 9.1 8.1 9.3 9.0
SWAN MIKE 1.1 4.3 2.1 9.9 6.2 7.0
-------------------------------------------------------------------------------------------------------There is my code it.
int getData(FILE* fpIn, int name[][MAX], float dif[], float score[][MAX])
{
int i;
int j= 0;
char ch;
i= 0;
while(i< MAX && (ch= fgetc(fpIn)) !=' ' && ch != EOF)
{
name[j][i]= ch;
i++;
}
if(i=21)
{
fscanf(fpIn, "%f", dif[i]);
i++;
}
else if(i>21)
{
fscanf(fpIn, "%f", score[j][i]);
i++;
}
j++;
return j; // Number of diver
}
Explanation / Answer
please rate - thanks
try this
#include <conio.h>
#include<stdio.h>
#define MAX 10
int getData(FILE* fpIn, char name[][20], float dif[], float score[][5])
{
int i;
int j= 0;
char ch;
ch= fgetc(fpIn);
while(ch != EOF)
{i= 0;
name[j][i]= ch;
for(i=1;i<20;i++)
name[j][i]= fgetc(fpIn);
fscanf(fpIn, "%f", &dif[j]);
for(i=0;i<5;i++)
fscanf(fpIn, "%f", &score[j][i]);
j++;
ch= fgetc(fpIn);
}
return j-1; // Number of divers
}
int main()
{int n,i,j;
char name[MAX][20];
float dif[MAX],score[MAX][5];
FILE *input;
input = fopen("input.txt","r");
if(input == NULL)
{ printf("Error opening input file! ");
return 0;
}
n=getData(input,name,dif,score);
printf("number of swimmers: %d ",n);
for(i=0;i<n;i++)
{for(j=0;j<20;j++)
printf("%c",name[i][j]);
printf(" %.1f ",dif[i]);
for(j=0;j<5;j++)
printf("%.1f ",score[i][j]);
}
fclose(input);
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.