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

This is a discussion on Homework help (again)! To array or not to array? That IS the question.. within the C Programming forums, part of the General Programming Boards category; Hello again! More homework help, again I feel like I just need a push in the right direction. Here are ...

  1. #1
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18

    Question 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!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    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!

  4. #4
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24
    What the fck! Lol

  5. #5
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    Seriously l2krence! I tried to write this stuff down, I am just so lost!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    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.
    Last edited by Adak; 11-18-2010 at 08:25 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    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!
    Last edited by Aslan14; 11-19-2010 at 02:32 PM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    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. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    So replace fp with stdin, and you're reading the keyboard.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    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.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    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.
    Last edited by agxphoto; 11-21-2010 at 08:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21