Thread: Array help

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    8

    Array help

    Hi! I'm new to these boards, and new to programming in general. I'm working on a project, and I've been stuck on this part for hours. The basics of the project are that I have to read values from an input file that correspond to ENABLE and ON states of 5 heaters. Some of them should be DISABLED while others are ENABLE and some should be OFF while others are ON.

    I can't get my output to display the ENABLE and ON values like it should. All of my values come back ENABLED and ON, when I know that some of them should be DISABLED and/or OFF. My code looks something like this:

    Code:
    enum on_enable {ENABLE=0, ON=1};
    
    main()
    {
    int lower_state[5][2];
    int n;
    char on_state_labels[2][4]={{"ON "},
                                                    {OFF"};
    char enable_state_labels[2][9]={{"ENABLED "}
                                                         {"DISABLED"};
    
    for (n=0; n<5; n++)
    {
       scanf("%d %d", &lower_state[n][ENABLE], &lower_state[n][ON];
       printf("%s", on_state_labels[lower_state[n][ON]]);
       printf("%s", enable_state_labels[lower_state[n][ENABLE]]);
    }
    }
    Where am I going wrong with this? I've tried everything I can think of and I always get back ON and ENABLED values. I don't know if something major or minor is wrong, but I am at a standstill with this project until I get it figured out. Please help!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well for a start you are missing a quote mark and a brace:
    Code:
    char on_state_labels[2][4]={{"ON "},{"OFF"}};
    Edit: and another closing brace after "DISABLED"
    Edit2: and a comma between "ENABLED" and "DISABLED"

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    And a comma
    Code:
    char enable_state_labels[2][9]={{"ENABLED "},
                                                         {"DISABLED"};
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Yeah, thanks, but I guess I should have stated that this is an abridged version of my program, but I think everything relevant to my problem is there. In my actual program the quote and brace are there, sorry. And the comma is there too.
    Last edited by Oklaskull; 03-11-2008 at 02:04 PM.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Does your file look like:
    1 0 0 0 1 1 0 1 1 1
    or:
    1 0
    0 0
    0 1
    1 0
    1 1
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Honestly, I don't know what the file looks like. The instructor for the class just told us what order the values were read in, and that the scanf statements should take two values at a time.

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    1. Group the array declarations a bit better and fix the missing closing brace.
    2. Your first scanf doesn't have a closing parenthesis.
    3. main should return int

    Your code works, for me, so you are close.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Thank you for your help, but my problem still hasn't been solved. With those corrections the program compiles and runs, but it doesn't run properly. My program now looks like this:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <time.h>
    
    enum on_enable {ENABLE=0, ON=1};
    
    main()
    {
    int lower_state[5][2];
    int n;
    char on_state_labels[2][4]={{"ON "},
                                {"OFF"}};
    char enable_state_labels[2][9]={{"ENABLED "},
                                    {"DISABLED"}};
    
    for (n=0; n<5; n++)
    {
    scanf("&#37;d %d", &lower_state[n][ENABLE], &lower_state[n][ON]);
    printf("%s", on_state_labels[lower_state[n][ON]]);
    printf("%s\n", enable_state_labels[lower_state[n][ENABLE]]);
    }
    }
    and the output looks like this:

    ON ENABLED
    ON ENABLED
    ON ENABLED
    ON ENABLED
    ON ENABLED


    The problem is that some of those ONs and ENABLEDS should be OFFs and DISABLEDs (I don't know which ones because I can't see the input file). I don't know why they always show up as ON and ENABLED. Thanks again for any help, it's greatly appreciated.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well it looks as if its a problem with the way you are reading from the file. Your current posted code only gets input from the user, and it works now.

  10. #10
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Yes you don't have an input file....
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Where are your "OFF" and "DISABLED" values declared? You have one 'enum' --- where's the other? All the code knows is '0' and '1', so how does your program know the value of "OFF" and "DISABLED"?

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    I have an input file to use when I run the program. To run the program with the input file I type:

    telem.x < ~phil4985/cs1313student/soonerglitch_telem.dat

    telem.x is the name of my program, and everything from the ~ on is the input file. It then returns what I gave as my output. I don't know if this helps or not, I'm so confused...

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    are you on a unix system?
    If yes then issue:
    > cat ~phil4985/cs1313student/soonerglitch_telem.dat
    and within code tags paste us the output...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Quote Originally Posted by kcpilot View Post
    Where are your "OFF" and "DISABLED" values declared? You have one 'enum' --- where's the other? All the code knows is '0' and '1', so how does your program know the value of "OFF" and "DISABLED"?
    I am under the impression that "OFF" and "DISABLED" are printed out through the use the char arrays that I declared. Is that not the case? For example, depending on the value my program reads from the input file, either ON or OFF is read from the char array and that is what should be printed. Clarification of this would be really helpful to me. Thanks again!

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Here is the output as requested (that was pretty nifty) I should also state at this point that I am only posting part of my code here, and there are more values to be read in in a similar fashion to what I have posted, hence the length of the input file.

    Code:
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    -10. -9.  -5. -8. -9.
    10. 11. 5. 11. 11.
    5. 0. 3.
    2. -1. 3.
    1 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 1 1 0 0
    -10. -9.  -5. -8. -9.
    10. 11. 5. 11. 11.
    5. 0. 3.
    2. -1. 3.
    1 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 1 1 0 0
    -7. -10.  -5. -8. -10.
    10. 11. 5. 11. 11.
    3. -1. 2.
    2. 0. 3.
    1 1 1 1 0 0 0 0 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 1 0 0
    -3. -10.  -5. -8. -10.
    10. 11. 5. 11.  9.
    3. -1. 2.
    2. 0. 3.
    1 1 1 1 0 0 0 0 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 1 0 0
    -1. -7.  -7. -10. -8.
    9. 8. 5. 9.  7.
    3. 1. 2.
    2. 3. 3.
    1 1 1 1 0 0 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 1 0 0
    2. -5.  -9. -10. -6.
    7. 7. 3. 7.  6.
    3. 3. 2.
    2. 5. 3.
    1 1 1 1 0 0 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 1 0 0
    3. -2.  -10. -8. -3.
    5. 5. 1. 6.  4.
    2. 4. 1.
    1. 6. 2.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 0 0 0
    5. -0.  -10. -6. -1.
    3. 4. 0. 3.  2.
    1. 5. 0.
    1. 6. 1.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 0 0 0
    7. 2. -9. -4. 1.
    2. 2. -1. 1.  0.
    0. 7. -1.
    0. 6. 1.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 0 1 1
    0 0 1 0 0 0
    10. 4. -5. -1. 4.
    0. 1. -3. 0.  -2.
    -1. 7. -1.
    -2. 5. 1.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    1 1 1 0 1 1
    1 1 1 0 0 0
    12. 7. -2. 2. 7.
    -3. -1. -5. -2.  -5.
    -3. 6. 1.
    -4. 5. 1.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    1 1 1 0 1 1
    1 1 1 0 0 0
    15. 11. 2. 5. 9.
    -5. -4. -8. -4.  -8.
    -1. 5. 3.
    -2. 5. 0.
    1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0
    1 1 1 0 1 1
    1 1 1 0 0 0
    17. 13. 5. 6. 12.
    -7. -7. -10. -6.  -9.
    1. 4. 5.
    0. 4. -1.
    1 0 1 1 1 1 1 1 1 1
    0 0 0 0 1 1 0 0 0 0
    1 1 1 1 1 1
    1 1 1 1 1 1
    17. 13. 5. 6. 12.
    -10. -9. -10. -8.  -10.
    3. 4. 6.
    1. 4. -1.
    1 0 1 1 1 1 1 1 1 1
    1 1 0 0 1 1 0 0 1 1
    1 1 1 1 1 0
    1 1 1 1 1 1
    17. 15. 8. 9. 15.
    -10. -10. -9. -9.  -10.
    5. 7. 6.
    3. 6. 1.
    1 0 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 0 0 1 1
    1 1 1 0 1 0
    1 1 1 0 1 1
    16. 16. 9. 11. 17.
    -9. -10. -7. -10.  -8.
    5. 7. 6.
    5. 6. 2.
    1 0 1 0 1 1 1 1 1 0
    1 1 1 1 1 1 1 1 1 1
    1 1 1 0 1 0
    1 1 1 0 1 1
    16. 16. 12. 13. 17.
    -7. -8. -4. -9.  -6.
    6. 7. 6.
    7. 6. 3.
    1 0 1 0 1 1 1 1 1 0
    1 1 1 1 1 1 1 1 1 1
    1 0 1 0 1 0
    1 0 1 0 1 1
    16. 16. 15. 16. 17.
    -5. -5. -2. -6.  -5.
    6. 7. 6.
    7. 6. 5.
    1 0 1 0 1 1 1 0 1 0
    1 1 1 1 1 1 1 1 1 1
    1 0 1 0 1 0
    1 0 1 0 1 1
    16. 16. 16. 16. 17.
    -2. -1. 0. -3.  -3.
    6. 6. 5.
    5. 6. 7.
    1 0 1 0 1 0 1 0 1 0
    1 1 1 1 1 1 1 1 1 1
    1 0 1 0 1 0
    1 0 1 0 1 0
    16. 16. 16. 16. 17.
    -0. -0. 3. -1.  -0.
    5. 4. 5.
    5. 5. 7.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM