Re: another C problem
FWLIW, I'd put only the initial M | F and final enumeration into IF statements. And then I'd put the largest IF ... THEN ... ELSE into a SWITCH ... CASE instead: it's easier to read. while(!EOF) { /* read file into array */ if (M ){ switch(etc) do whatever break; [...] switch(etc) count++ else if switch(etc) else switch(etc) count++ } Just my 0.02c worth - don't spend it all at once - we _do_ have a financial ongoing crisis. ;) Wesley Parish Quoting [EMAIL PROTECTED]: I hope people don't mind me posting these here, let me know if you do and I'll find a more appropriate geek list to post them. The C program I'm working on at the moment is taking a list from a data file and populating an array to either be outputted either to screen, file or printer. the foo.dat file is like this: F 26 5 F 64 4 F 29 2 M 12 3 M 40 1 ...snip... So I need to populate (using count++) a two-dimensional array based on whether the first column is M or F the second column is within a range and whether the thrid column is either 1,2,3,4or5 This is the part that's doing my head in, I could do it using a very convoluted if-else-if loop which would end up giving me a massive and hard to maintain piece of code ie: if line == (F 25) 1) then count++ to array[0][0] else if line == (F 25) 2) then count++ to array[1][0] etc, etc, etc, (code won't compile - made it more human readable for mostly my sake...) doing it this way I would end up with 25 if-else-if statements. is there a way to take the 1) section read the number from the third coloumn from the foo.dat file and minus one from it? This would give me the x axis of the array - or am I barking completly up the wrong tree? Or is there a better way to do this? any pointers would be appreciated. Kerry ps, I hope I'm making some kind of sense with all of this. Sharpened hands are happy hands. Brim the tinfall with mirthful bands - A Deepness in the Sky, Vernor Vinge I me. Shape middled me. I would come out into hot! I from the spicy that day was overcasked mockingly - it's a symbol of the other horizon. - emacs : meta x dissociated-press
another C problem
I hope people don't mind me posting these here, let me know if you do and I'll find a more appropriate geek list to post them. The C program I'm working on at the moment is taking a list from a data file and populating an array to either be outputted either to screen, file or printer. the foo.dat file is like this: F 26 5 F 64 4 F 29 2 M 12 3 M 40 1 ...snip... So I need to populate (using count++) a two-dimensional array based on whether the first column is M or F the second column is within a range and whether the thrid column is either 1,2,3,4or5 This is the part that's doing my head in, I could do it using a very convoluted if-else-if loop which would end up giving me a massive and hard to maintain piece of code ie: if line == (F 25) 1) then count++ to array[0][0] else if line == (F 25) 2) then count++ to array[1][0] etc, etc, etc, (code won't compile - made it more human readable for mostly my sake...) doing it this way I would end up with 25 if-else-if statements. is there a way to take the 1) section read the number from the third coloumn from the foo.dat file and minus one from it? This would give me the x axis of the array - or am I barking completly up the wrong tree? Or is there a better way to do this? any pointers would be appreciated. Kerry ps, I hope I'm making some kind of sense with all of this.
Re: another C problem
On Fri, Nov 28, 2008 at 11:23 AM, [EMAIL PROTECTED] wrote: I hope people don't mind me posting these here, let me know if you do and I'll find a more appropriate geek list to post them. The C program I'm working on at the moment is taking a list from a data file and populating an array to either be outputted either to screen, file or printer. the foo.dat file is like this: F 26 5 F 64 4 F 29 2 M 12 3 M 40 1 ...snip... So I need to populate (using count++) a two-dimensional array based on whether the first column is M or F the second column is within a range and whether the thrid column is either 1,2,3,4or5 This is the part that's doing my head in, I could do it using a very convoluted if-else-if loop which would end up giving me a massive and hard to maintain piece of code ie: if line == (F 25) 1) then count++ to array[0][0] else if line == (F 25) 2) then count++ to array[1][0] etc, etc, etc, (code won't compile - made it more human readable for mostly my sake...) doing it this way I would end up with 25 if-else-if statements. is there a way to take the 1) section read the number from the third coloumn from the foo.dat file and minus one from it? This would give me the x axis of the array - or am I barking completly up the wrong tree? Or is there a better way to do this? any pointers would be appreciated. Kerry ps, I hope I'm making some kind of sense with all of this. actually sounds like a python or perl problem :-)
Re: another C problem
On Fri, 28 Nov 2008 11:23:15 +1300 (NZDT) [EMAIL PROTECTED] wrote: I hope people don't mind me posting these here, let me know if you do and I'll find a more appropriate geek list to post them. The C program I'm working on at the moment is taking a list from a data file and populating an array to either be outputted either to screen, file or printer. the foo.dat file is like this: F 26 5 F 64 4 F 29 2 M 12 3 M 40 1 ...snip... So I need to populate (using count++) a two-dimensional array based on whether the first column is M or F the second column is within a range and whether the thrid column is either 1,2,3,4or5 This is the part that's doing my head in, I could do it using a very convoluted if-else-if loop which would end up giving me a massive and hard to maintain piece of code ie: if line == (F 25) 1) then count++ to array[0][0] else if line == (F 25) 2) then count++ to array[1][0] etc, etc, etc, (code won't compile - made it more human readable for mostly my sake...) doing it this way I would end up with 25 if-else-if statements. is there a way to take the 1) section read the number from the third coloumn from the foo.dat file and minus one from it? This would give me the x axis of the array - or am I barking completly up the wrong tree? Or is there a better way to do this? any pointers would be appreciated. Kerry ps, I hope I'm making some kind of sense with all of this. Need input! What do you mean! Lessons I've learnt the hard way. 1. If you've got data to input, store it as accurately as you can. The customer will always say 'can you just do this...' and you'll probably have to start from scratch if you've summarised it. 2. A complex problem is lots of simple problems stuck together. Part 1. Get the data. Read into a ( 3d array ), M = 0, F =1 [ ain't that true! ] = array [sex] [age] [cat]++; ( or is it = array [sex] [age] [--cat]++; ) Part 2. Summarise as you wish to display == male [ageband][category], etc ?? Part 3. Display. hth, Steve -- Steve Holdoway [EMAIL PROTECTED]
Re: another C problem
Kerry, 1. Loop round reading a line at a time and interpreting the columns. You want to store the 3 columns into variables - sex, age and tattoo_count. fscanf is good for this - do a man fscanf 2. Check the sex for values other than M or F. What about lower case m or f ? 3. Storing the answers in an array. Not sure if this is a great idea - how many lines could the file contain ? How do you know ahead of time how big the array needs to be ? 4. I would just copy the answers straight to their destination without storing them. HTH, John