
-----------------------------------
jafa401
Mon Aug 10, 2009 9:12 pm

Function help
-----------------------------------
How do i shorten/ make my code more efficient. Many thanks Thanks

[code]void writers (int cid [ ], double cidvalue [ ], int count)
{
	FILE* dancef = fopen ("tracks.dat", "rt");
	FILE* annf = fopen ("recp.rpt", "wt");
	int numvtracks = 0;
	int numitracks = 0;
	int numl, cidnum1, cidnum2;
	double track1, track2, tprice;
	double total = 0.0;
	int ftrack1, ftrack2;
	char sPin [16];
	while (count > 0)
	{
		numl = fscanf (dancef, "%d %d %12s\n", &cidnum1, &cidnum2, &sPin);
		if (numl != 3) break;
                fprice1 = search (cid, cidvalue, count, cidnum1, &track1);
		fprice2 = search (cid, cidvalue, count, cidnum2, &track2);
		if (ftrack1 && ftrack2 && (cidnum1 != cidnum2))
		{
			tprice =  (track2 - track1);
			total += tprice;
			numvtracks++;
			fprintf (annf, "%s   %2d   %2d   $%5.2lf\n", sPin, cidnum1, cidnum2, tprice);
		}
		else
		{
			fprintf (annf, "INVALID DATA: %2d  %2d *\n", cidnum1, cidnum2);
			numitracks++;
		}
	}
	fclose (annf);
	fclose (dancef);
	printf ("\ntracks processed: %d\n", numvtracks);
	printf ("invalid tracks processed: %d\n", numitracks);
	printf ("Track revenue: $%.2lf\n", total);[/code]

-----------------------------------
wtd
Mon Aug 10, 2009 11:22 pm

RE:Function help
-----------------------------------
First important note:  short code is not necessarily more efficient.

The first thing you could do is either comment your code, or better yet, use more meaningful variable names.  Keep in mind that there's no need to use short variable names, and no need to use prefixes or suffixes to indicate variable type.

As it is, I am not sure what your function is intended to actually do.
