Parts Catalog Accessories Catalog How To Articles Tech Forums
Call Pelican Parts at 888-280-7799
Shopping Cart Cart | Project List | Order Status | Help



Go Back   PeachParts Mercedes-Benz Forum > General Discussions > Off-Topic Discussion

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-09-2010, 12:41 AM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
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++

Reply With Quote
  #2  
Old 03-09-2010, 01:09 AM
300SD81's Avatar
1981 Mercedes-Benz 300SD
 
Join Date: Aug 2006
Location: University of Georgia
Posts: 1,082
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!
Reply With Quote
  #3  
Old 03-09-2010, 01:15 AM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
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]);

?
Reply With Quote
  #4  
Old 03-09-2010, 01:30 AM
300SD81's Avatar
1981 Mercedes-Benz 300SD
 
Join Date: Aug 2006
Location: University of Georgia
Posts: 1,082
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
		}

	}
}
I'm not as good with strings as with bytes in C... basically all my experience with C the past couple years has been with microcontrollers.
__________________
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!
Reply With Quote
  #5  
Old 03-09-2010, 12:38 PM
OldPokey's Avatar
0-60 in 10 minutes flat
 
Join Date: Sep 2006
Location: Middletown MD
Posts: 527
Lightbulb

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 );
strsep() lets you tokenize a string in loop fashion. For more info on that google "man strsep". There are tons of helpful library routines - if you can think of something basic you need to do, it's probably in the standard C library.

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

Reply With Quote
  #6  
Old 03-09-2010, 08:42 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
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
Reply With Quote
  #7  
Old 03-09-2010, 08:46 PM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
What system are you using? If it's Linux or UNIX, you have manual pages. If it's Windows, you have MSDN.
Reply With Quote
  #8  
Old 03-09-2010, 08:48 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
uhm.. its on a winders machine compiling in DEV C++
Reply With Quote
  #9  
Old 03-09-2010, 11:23 PM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
http://www.google.com/search?q=msdn+fgets&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-USfficial&client=firefox-a
Reply With Quote
  #10  
Old 03-10-2010, 04:14 PM
OldPokey's Avatar
0-60 in 10 minutes flat
 
Join Date: Sep 2006
Location: Middletown MD
Posts: 527
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

Reply With Quote
  #11  
Old 03-10-2010, 06:56 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
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?
Reply With Quote
  #12  
Old 03-10-2010, 07:05 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
i dont know what the hell to do.. really.. I dont
Reply With Quote
  #13  
Old 03-10-2010, 08:40 PM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
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.
Reply With Quote
  #14  
Old 03-10-2010, 09:11 PM
TheDon's Avatar
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
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
Reply With Quote
  #15  
Old 03-10-2010, 09:54 PM
Registered User
 
Join Date: Oct 2005
Posts: 4,263
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.

Reply With Quote
Reply

Bookmarks


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:35 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0
Copyright 2024 Pelican Parts, LLC - Posts may be archived for display on the Peach Parts or Pelican Parts Website -    DMCA Registered Agent Contact Page