Thread: stupid c question re arrays

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    stupid c question re arrays

    Okay, this is a basic stupid easy question on a homework problem that I have not been able to figure out, and I would really appreciate a hint.

    Overview, I am creating a traffic report where the user enters the traffic volume recorded for each hour for several hours. Then various information is displayed/calculated using this information (average, median, etc).

    I have two single dimensional arrays, one with the hours and one with the user input for the traffic volumes.

    I need to show a volume and the hour it was recorded for various data manipulations (smallest number, largest, sort in ascending order, etc) How do I return both the hour and the volume in my function?

    The following code finds the smallest volume, but I can't get it to show the lowest hour. The code below will only give the last hour, for the last x, because of the for loop structure.

    I tried originally to return these as int values, but I ended up with gibberish, so I switched to a void returning a printf statement. Do I need to use pointers? A nested for loop? Go back to returning int values for hour and volume from the function?

    void smallest(int hour[], int volume[], int time, int cars)
    {
    int x, y, lowhour, lowestvol = 1000;

    for (x = 0; x < cars; x++)
    {if (volume[x] < lowestvol)
    lowestvol = volume[x];
    lowhour = hour[x]
    }
    }
    printf("The lowest traffic volume was %d recorded at %d.\n", lowestvol, lowhour);

    }

    Thanks,
    C.Unger

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I need to show a volume and the hour it was recorded for various data manipulations (smallest number, largest, sort in ascending order, etc) How do I return both the hour and the volume in my function?
    You could make a structure:
    Code:
    struct Data { int hour; int volume; };
    
    Data smallest( ... )
    {
        struct Data d = { -1, -1 };
    
        ... do stuff ...
    
        d.hour = hour;
        d.volume = volume;
        return d;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  2. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  3. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  4. Question on Arrays
    By shin in forum C Programming
    Replies: 10
    Last Post: 06-06-2004, 01:11 PM
  5. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM