# Homework help (again)! To array or not to array? That IS the question..

• 11-18-2010
Aslan14
Homework help (again)! To array or not to array? That IS the question..
Hello again! More homework help, again I feel like I just need a push in the right direction. Here are the homework instructions;

HW 5 reads three columns of numbers, column 2 and 3 are separated by a colon. The first two are integers and the third is floating point. Ignore data in the row if there is a zero in column 1. If first number is 1, take the average of column 3. If there column 1 and 2 both have a 1, find the max of column 3. If there is a 1 in column 1 and a 0 in column 2, find minimum of column 3. Max, min, and average are printed with a %7.2f. If there is a -1 in column 1, the set of numbers have ended.

My questions are;
(1) How does the user enter the numbers in a column? Every time I hit enter to move to the next line, it commands the program to run.
(2) Should I just enter the numbers separated by a space or comma?
(3) If I use an array(s), can I use multiple arrays in one program? Meaning, can I use two int arrays or do I have to make it a multidimensional array?
(4) If I use arrays, how would I write the code so the user inputs the numbers?
(5) And finally, for either way I go, how in the heck do I get the program to check the first and second numbers of the columns? (I think this is where my biggest hang up comes from, I am totally blank here!)

I really don't have much code written yet, as I don't really know which direction to go. Thanks for any help you can give!
• 11-18-2010
CommonTater
Who makes up these assignments anyway????

Aslan14 your best bet is to sit down with paper and pencil and figure out exactly what is needed to accomplish the task. From there reduce it to pseudo-code, a kind of step by step procedure. From the second pass, try to reduce each step to code... Once you have something that compiles (even with errors) post it up and we'll see what we can do.
• 11-18-2010
Aslan14
My professor who is cold war era! (seriously, he tells us stories about the Russians. Entertaining to say the least, but these programs are kicking my butt!)

Working on writing another program to an external file now (THAT is actually not too bad), will write down something shortly and post it.
Thanks Tater!
• 11-18-2010
l2krence
What the fck! Lol
• 11-18-2010
Aslan14
Seriously l2krence! I tried to write this stuff down, I am just so lost!
• 11-18-2010
When the problem description gets involved, post up a sample of the input your program needs to handle, and the output you need to create.

The assignment isn't that bad, but reading it, and then reading your questions, and trying to figure out what the relationship is between the two - it gives me a headache. :p :p

Take each constraint in the assignment, and solve it one at a time. Concentrate on just one problem at a time, and using pseudo code can really help in this first stage.

It's a big mistake to just sit down and try to write code, without thinking (or writing) down the pseudo code that will form the backbone of the program. You don't want or need all the pseudo code for this first step, but you *need* the basic logic blocks that you can build your program, around.
• 11-19-2010
Salem
Start by preparing a small text file you can test with
Code:

```0 : 42 : 1324 1 : 0 : 42```
Use fgets() to read a whole line.

Use sscanf() to extract 3 colon separated numbers.
Say "%d : %d : %d"
• 11-19-2010
Aslan14
Okay, so I am going to start with the "user input" looking like this

(c1 c2 : c3)
0 1 : 7.2 (line 1)
1 1 : -3.3 (line 2)
1 0 : 9.92 (line 3)
0 0 : -3.4 (line 4)
1 1 : 5.5 (line 5)
-1

If I understand the directions correctly, since the first column (c1) of the first line contains a 0, I am to ignore that whole line and move to the next.

Next, if the first number is 1, I should take the average of the 3rd column, not including the first number in c3 that I previously ignored.

Then, if there is a 1 in c1 and c2, find the max (which should be 5.5 in this case) and print with a .2 format.

Last, if 1 in c1 and 0 in c2, find the minimum (should be -3.4).

-1 denotes end of numbers.

Make better sense? I think I might need to just go get the professors help because I don't understand!
• 11-19-2010
Salem
> Then, if there is a 1 in c1 and c2, find the max (which should be 5.5 in this case) and print with a .2 format.
There are two example of this in your file.
The first time you see it, the max (so far) is 7.2
But the second time you see it, the max has increased to 9.92.

The first step is to get the file reading sorted out.
Code:

```char buff[100]; FILE *fp = fopen("file.txt","r"); while ( fgets( buff, sizeof(buff), fp ) != NULL ) {   printf("Read line %s", buff ); }```

Then add to that the basics of input conversion.
Code:

```char buff[100]; FILE *fp = fopen("file.txt","r"); while ( fgets( buff, sizeof(buff), fp ) != NULL ) {   int c1, c2;   double value;   if ( sscanf( buff, "%d %d : %f", &c1, &c2, &value ) == 3 ) {     printf("c1=%d c2=%d\n", c1, c2 );   } else {     printf("UNKNOWN line %s", buff );   } }```
Can you figure out what to do with c1 and c2 now?
Try JUST ONE of the cases (say the max) and see how you get on.
• 11-20-2010
Aslan14
I totally can see where you are going Salem! The only problem is I am not reading from an external file, the numbers are to be inputted by the user when prompted.

It would be SO much easier I think if I could write the program to read from a file, then maybe I wouldn't have such a mental block.
• 11-20-2010
Salem
So replace fp with stdin, and you're reading the keyboard.
• 11-21-2010
agxphoto
One way you could have the stdin to be split up into columns is to prompt for a tab between numbers. You don't have to hit enter before the end of a line.

Sometimes when I had some odd behaviors in stdin with programs that should have been simple, it was related to doing the data entry in a word processor. Are you CAT'ting in the program?

So your workflow on the data entry is CAT, then GCC, then test the program, right? If so, in a lot of student problems, it'd be common for the program to keep on accepting input until the program was halted with a CNTRL-D or some similar hard stop.

[For example, some K&R C examples will work great with CAT, but malfunction when encoded with a word processor. It sounds like it shouldn't, but it often does.]

This would be a very basic, elementary kind of cosmetic problem a student might have. Just checking.
• 11-21-2010
agxphoto
One way you could have the stdin to be split up into columns is to prompt for a tab between numbers. You don't have to hit enter before the end of a line.

Sometimes when I had some odd behaviors in stdin with programs that should have been simple, it was related to doing the data entry in a word processor. Are you CAT'ting in the program?

So your workflow on the data entry is CAT, then GCC, then test the program, right? If so, in a lot of student problems, it'd be common for the program to keep on accepting input until the program was halted with a CNTRL-D or some similar hard stop. Then, after that hard stop, the program would PRINTF its report and then stop. That kind of behavior should be typical. Do you see that in your programs, normally? Or, do you type them in and some of them do nothing, even though the code looks right on the screen?

[For example, some K&R C examples will work great with CAT, but malfunction when encoded with a word processor. It sounds like it shouldn't, but it often does. I accept the ridicule I am about to receive for saying this; but, I have observed it repeatedly.]

Try a workflow of: CAT to GCC to testing, and then going back to use a word processor for simple editing on the program's .c file.

This would be a very basic, elementary kind of cosmetic problem a student might have. Just checking.