Array help

This is a discussion on Array help within the C Programming forums, part of the General Programming Boards category; Well it would work fine if the input was 0 and 1 only. I guarantee that, but it looks like ...

  1. #16
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Well it would work fine if the input was 0 and 1 only. I guarantee that, but it looks like the input is not so, and scanf just puts the value 0 for it is not reading anything appropriate.
    If you post the content of the input, and i suspect that you should type:
    > cat /home/phil4985/cs1313student/soonerglitch_telem.dat
    to get that printed, we could guide you to read it appropriately.
    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.

  2. #17
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Your input file is much more complicated than what you describe. With the initial values in it, your program works correctly, and prints the right thing.
    It appears to me that either your assignment has a much different specification, or you are reading in a wrong file, with the first being more probable in my view.
    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.

  3. #18
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    There are with a quick view, 20 blocks of: 2 series of 5 pairs of 1 or 0, followed by two series of 3 pairs of 1 or 0 followed by 2 series of 5 values followed by 2 series of 3 values negative or positive, with the values delimited by a '.', and i really can understand a connection between the data but not enough to be useful.
    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. #19
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    yes you are correct. the assignment is to read enable and on values for a set of 5 heaters, another set of 5 heaters, then a set of 3 heaters and another set of 3 heaters. Then temperature readings for those heaters are to be read. This is done until the data input stops (so everything needs to be in a while loop).
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <time.h>
    
    enum on_enable {ENABLE=0, ON=1};
    enum time_constants {ZERO=0, FOUR_SECONDS=4};
    enum number_contants {HEATERS=5, STATES=2, BATT=3, IMU=3};
    main()
    {
    
    int lower_state[HEATERS][STATES];
    int upper_state[HEATERS][STATES];
    int batt_state[BATT][STATES];
    int imu_state[IMU][STATES];
    int n;
    int m;
    int i;
    int j;
    int last_read;
    double lower_temp[HEATERS];
    double upper_temp[HEATERS];
    double batt[BATT];
    double imu[IMU];
    char on_state_labels[2][4]={{"ON "},
                                {"OFF"}};
    char enable_state_labels[2][9]={{"ENABLED "},
                                    {"DISABLED"}};
    time_t mytime;
    
    while(1==1)
    {
    for (n=0; n<5; n++)
    {
            scanf("&#37;d %d", &lower_state[n][ENABLE], &lower_state[n][ON]);
            printf("                          LOWER_%d  %s "
    ,n,on_state_labels[lower_state[n][ON]]);
            printf("%s\n", enable_state_labels[lower_state[n][ENABLE]]);
    }
    printf("\x1b[5A");
    for (n=0; n<5; n++)
    {
            scanf("%d %d", &upper_state[n][ENABLE], &upper_state[n][ON]);
            printf("UPPER_%d %s ",n,on_state_labels[upper_state[n][ON]]);
            printf("%s\n", enable_state_labels[upper_state[n][ENABLE]]);
    }
    printf("\n");
    for (m=0; m<3; m++)
    {
            scanf("%d %d", &batt_state[m][ENABLE], &batt_state[m][ON]);
            printf("                          BATT_%d %s "
    ,m,on_state_labels[batt_state[m][ON]]);
            printf("%s\n", enable_state_labels[batt_state[m][ENABLE]]);
    }
    printf("\x1b[3A");
    for (m=0; m<3; m++)
    {
            scanf("%d %d", &imu_state[m][ENABLE], &imu_state[m][ON]);
            printf("IMU_%d %s ",m,on_state_labels[imu_state[m][ON]]);
            printf("%s\n", enable_state_labels[imu_state[m][ON]]);
    }
    for (n=0; n<5; n++)
    {
            scanf("%lf", &lower_temp[HEATERS]);
            printf("%s", lower_temp[HEATERS]);
    }
    
    mytime = time(ZERO);
    printf("%s",ctime(&mytime));
    mytime = time(ZERO);
    printf("%s",ctime(&mytime));
    printf("\x1b[0A");
    sleep(FOUR_SECONDS);
    }
    }
    This is the entire code that I have so far. It's obviously still a huge work in progress, but if you feel up to it you can see if I'm on the right track. I have to go to work now unfortunately, but thank you guys for all your help so far, it's much appreciated. This is so frustrating!

  5. #20
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    for (n=0; n<5; n++)
    {
            scanf("&#37;lf", &lower_temp[HEATERS]);
            printf("%s", lower_temp[HEATERS]);
    }
    HEATERS = 5, and you go out of bounds, try n instead.
    You want to print a double, use %f
    Same as the before.
    Ok apart from some typical mistakes your program works fine once you complete reading the rest of the floating point data, it runs.

    Hints to go on.
    correct main to return int.
    Drop the infinite loop and instead do a test for the end of data. (hint: don't use feof, read the forum FAQ for the correct way to test for eof)
    And finally fix your indentation a bit, so that the blocks of code are clear.
    Good luck.
    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.

Page 2 of 2 FirstFirst 12
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, 02: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

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