|
|
|
|
|
|
#1
|
||||
|
||||
|
need some help with C programming
again.. lol
So I need to use file I/O to take in some data and use it to output something... taking in golf scores and the number of golfers.. example input 2 (number of golfers 1 2 3 4 5 x x x x x ( par) 2 3 4 4 4 (golfer 1 score) 3 4 4 2 1 (golfers 2 score) number of golfers allowed for the program is golfers <=100. I already did a program that takes in all that info and out puts the golfers over/under score. so now I need to do the same thing but output it for each hole. ex Golfer 1 score : -1 0 1 2 -2 1 0 0 etc etc etc so, I am thinking I can use a majority of the the code from the previous program but use arrays in some fashion to get the desired output but cannot think how to do it. take note this is in C not C++ |
|
#2
|
||||
|
||||
|
excuse my lack of golf knowledge.. is the number of holes fixed? If it is, I would malloc a 2-dimensional array, have a seperate array for par, and loop through doing the math. If not, then calculate the size of the arrays first from the number of holes...
__________________
Ich liebe meine Autos! 1991 Mercedes-Benz 560SEL | Megasquirt MS3-Pro | 722.6 transmission w/ AMG paddles | Feind Motorsports Sway Bar | Stinger VIP Radar | AntiLaser Priority | PLX Wideband O2 | 150A Alternator | Cat Delete 1981 Mercedes-Benz 300SD | Blown engine, rebuilding someday... 1981 Mercedes-Benz 300SD | Rear ended, retired in garage. 2009 Yamaha AR230HO | Das Boot Excessive speeding? It ain't excessive till I redline! |
|
#3
|
||||
|
||||
|
well here is the sample input file I have to use to test my programs
2 3 4 4 4 4 5 5 3 3 4 4 4 4 4 5 3 3 4 4 4 5 4 6 4 5 6 3 4 4 4 5 4 3 3 5 4 2 3 4 4 4 5 5 3 3 4 4 4 4 4 4 3 3 4 just an example.. first line of scores is par and the consecutive lines are the scores of golfers. Of course the number of holes are fixed.. lol this is for an round of golf with 18 holes. how would I take in the array?... fscanf(Fin,"%d",&par[18]); ? |
|
#4
|
||||
|
||||
|
I'd do something like this, codes a mess and does no error checking, but you get the idea.
Code:
int main (...) {
...
int numgolfer = (fgetc(stdin) - '0'); //convert ascii to int
fgetc(stdin); // ignore newline
int* par = malloc(18*sizeof(int));
int* scores = malloc(numgolfer);
for (int i = 0; i < numgolfer; i++) {
scores[i] = malloc(18);
for (j = 0; j<18; j++) {
//read scores in
}
}
}
__________________
Ich liebe meine Autos! 1991 Mercedes-Benz 560SEL | Megasquirt MS3-Pro | 722.6 transmission w/ AMG paddles | Feind Motorsports Sway Bar | Stinger VIP Radar | AntiLaser Priority | PLX Wideband O2 | 150A Alternator | Cat Delete 1981 Mercedes-Benz 300SD | Blown engine, rebuilding someday... 1981 Mercedes-Benz 300SD | Rear ended, retired in garage. 2009 Yamaha AR230HO | Das Boot Excessive speeding? It ain't excessive till I redline! |
|
#5
|
||||
|
||||
|
Code:
FILE *fp;
char line[128], *pTok, *pStr;
int golfers, par[18], scores[100][18];
int ii, golferIndex;
fp = fopen ( "foo", "r" );
/* Read in number of golfers */
if ( fgets ( line, sizeof(line), file ) != NULL )
{
sscanf ( line, "%d", &golfers );
}
else
{
golfers = 0;
}
/* read in par line */
if ( fgets ( line, sizeof(line), file ) != NULL )
{
pStr = line;
for ( ii = 0; ii < 18; ii++ )
{
/* Grab them one token at a time */
pTok = strsep ( &pStr, " " );
if ( pTok == NULL || !sscanf ( pTok, "%d", &par[ii] ) )
{
break;
}
}
}
/* Read in scores */
golferIndex = 0;
while ( golferIndex < golfers && fgets ( line, sizeof(line), file ) != NULL )
{
pStr = line;
for ( ii = 0; ii < 18; ii++ )
{
/* Grab them one token at a time */
pTok = strsep ( &pStr, " " );
if ( pTok == NULL || !sscanf ( pTok, "%d", &scores[golferIndex][ii] ) )
{
break;
}
}
golferIndex++;
}
fclose ( file );
Now you have the scores in a two-dimensional array. Go through it a golfer at a time (first dimension) and do the math for each hole (second dimension).
__________________
1984 300TD |
|
#6
|
||||
|
||||
|
interesting..
fgets.. what is that? if its what It hink it is we were told to never use "gets" in any form or fashion.. its just bad juju |
|
#7
|
|||
|
|||
|
What system are you using? If it's Linux or UNIX, you have manual pages. If it's Windows, you have MSDN.
|
|
#8
|
||||
|
||||
|
uhm.. its on a winders machine compiling in DEV C++
|
|
#9
|
|||
|
|||
|
#10
|
||||
|
||||
|
Using gets() is bad juju because there is no limit to how much data it can suck in. This can lead to overflow errors. The fgets() function has a limit, which is the second argument - the sizeof(line) statement. It's perfectly safe to use, and in fact should be used instead of gets().
__________________
1984 300TD |
|
#11
|
||||
|
||||
|
so I'd use the
"char *fgets( char *string, int n, FILE *stream ); wchar_t *fgetws( wchar_t *string, int n, FILE *stream );" in each arguement to take in the par score then the succesive scores for each golfer n? |
|
#12
|
||||
|
||||
|
i dont know what the hell to do.. really.. I dont
|
|
#13
|
|||
|
|||
|
I suspect that you should be using fscanf to read the numbers.
Too bad you're not learning FORTRAN. It has language constructs to do this very thing. |
|
#14
|
||||
|
||||
|
yes.. I know i am using fscanf.. but I need it to output with the over/under score for each hole... which is where I think I will need arrays but i dont know what to do in regards to that
|
|
#15
|
|||
|
|||
|
Yes, some arrays would be appropriate here. Have you tried searching Google for tutorials? Lots of stuff is out there for the finding.
You'll want to use fscanf in a loop to read each number into the appropriate array. |
![]() |
| Bookmarks |
|
|