Homework Help (array)

This is a discussion on Homework Help (array) within the C Programming forums, part of the General Programming Boards category; I have to write a program that reads in from a sample schedule.txt file that looks like so: 2 10 ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Homework Help (array)

    I have to write a program that reads in from a sample schedule.txt file that looks like so:
    2
    10
    0 8 12
    1 8 12
    2 8 12
    3 8 12
    4 8 12
    5 8 12
    6 8 12
    3 12 15
    4 20 22
    5 18 20
    3
    1 13 18
    2 8 17
    1 17 20

    The first number 2 indicates the number of schedules. The next integer 10 indicates how many bookings are in the week. The following 10 lines state when the appointments are books, 0 being sunday to 6 being saturday followed by the hours of the appointment. So, 0 8 12 is an appointment on sunday from 8-12. What i have to do is read in this information and output to the user the number of hours booked for each week disregarding the double booked hours. For example the 2nd schedule for the second week has a double booking on 1 (monday) one for 13-18 and another at 17-20 so the 17-20 is voided and the total hours for that week is 14. Any help would be greatly appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... first start by reading our Homework Policy

    Then you need to sit down and do 4 simple steps that every good programmer does...

    1) Analyse the problem (or task, if you prefer) until you understand it well enough to describe it in small detail.
    2) Plan out a step by step way to solve the problem, in words. (the smaller the steps the better)
    3) Write your code based upon the plan you created.
    4) Test your code ... first in small steps as you write, then the whole program. (Compiles does not mean Works)

    If you run into trouble with the code... post it here, using code tags and we'll see what we can do.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    const int HoursPerWeek = 168;
    const int HoursPerDay = 24;
    int main() {
        int schedule[HoursPerWeek];
        int num_schedule, events, day, start, end, day_hours, i, count = 0;
        FILE* ifp;
        ifp = fopen("schedule.txt", "r");
        fscanf(ifp, "%d", &num_schedule);
        while (count < num_schedule) {
            for (i=0; i<HoursPerWeek; i++)
                schedule[i] = 0;
            fscanf(ifp, "%d", &events);
            for (i=0; i<events; i++) {
                fscanf(ifp, "%d%d%d", &day, &start, &end);
                day_hours = 0;
                day_hours = day * HoursPerDay;
                int h = start + day_hours;
                int t = 0;
                if(schedule[h] < 1 || schedule[end+day_hours] < 1)
                    while (h < end + day_hours) {
                        schedule[h] ++;
                        if((schedule[h]) > 1) {
                            while(t >= 0){
                                schedule[h-t]--;
                                t--;
                            }
                        break;
                        }
                        h++;
                        t++;
                    }
            }
            count++;
            int sum = 0;
            for (i=0; i<HoursPerWeek; i++)
                sum += schedule[i];
            printf("Schedule %d contains %d hours of scheduled activity\n", count, sum);
        }
    
        return 0;
    }
    A friend helped but can somebody please comment on the code so i can understand what its doing.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've got a better idea. How about you go line by line adding comments until you get stuck. When you do get stuck, make a comment saying what you think it's doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    the first for loop initializes the array to all zeroes. Im confused on the second for loop as to what is actually going on after the information is scanned in from the file.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why does no one ever listen to me? Oh I know, because I don't just and out answers, I make them actually think.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Do you think id be asking if i havent already thought about it myself? i got the code its written i dont need the answer just need help comprehending the code.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Thinking about it, and doing it are two entirely different things. For example, if you had actually done what I suggested, I would be able to see what you were stuck on specifically, instead of "I'm confused on the second part".

    clack clack clackyclack... (I seem to be doing a lot of this lately...)


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    part of the second for loop:

    While i < number events that week, i++
    (for example the first week there are 10 events)

    Use an example from your text file to try out the next part:

    (The first line is 0 8 12)

    day_hrs = 0*24 = 0
    h = 8 + 0
    (This is the index (8) that accounts for Sunday 8 am)

    end + day_hours = 12
    (This is the index (12) that accounts for sunday 12 pm)

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by IamTheCarter View Post
    Do you think id be asking if i havent already thought about it myself? i got the code its written i dont need the answer just need help comprehending the code.
    Here's your problem in a nutshell... You "got the code its written" ... BUT *you* didn't write it. I can tell just be looking at it that it wasn't written by a beginner... and so will your teacher.

    There are 4 steps to writing a program... Analyse the requirment until you understand it. Plan a step by step solution to solve the problem. Write your code based on your plan. Test your code in small steps as you work and then in total when done.... Every programmer, follows these 4 steps for every program, no matter how simple or complex, for the simple reason that you cannot solve a problem that you do not understand.

    Tell me... how much real thought did you give this? Did you stare at the assignment and go "Oh crap!" and head off to find someone to do it for you... or did you actually sit down and write out exactly what had to be done and at least try to get it going on your own? 2/3 of programming understanding what has to be done... the rest is writing and testing code.

    Seriously... Quzah was giving you some really sound advice... he enjoins you to *think*...

    Now, do yourself a favour... scrap your friend's code, sit down and figure this out for yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-21-2010, 08:46 AM
  2. homework help wanted: removing multiple spaces from array
    By DHart07 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2010, 09:34 PM
  3. array homework
    By kokujampo in forum C Programming
    Replies: 4
    Last Post: 04-05-2010, 12:47 PM
  4. Homework help Array Arguments
    By VanillaSparkles in forum C Programming
    Replies: 3
    Last Post: 08-24-2007, 01:27 AM
  5. Replies: 4
    Last Post: 03-02-2003, 08:12 AM

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