Thread: Need Help Getting Started on Homework

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Need Help Getting Started on Homework

    I have a homework assignment, and I don't expect people on the board to do it for me, but I do need some advice on how to begin. Should I load all the data into one array? Also, what would be the best way to put the final output in alphabetical order?

    I am also having trouble figuring out how to assign values of 0 for letters that don't exist in the file.

    Thanks

    "Problem: There exists a series of zones labeled A through Z. A number of properties (all rectangular in shape) are assigned to a zone and have a length and width (in feet, assume integer data). You will write a program that will load data from a file named zones and display the total area (in square feet) for each zone.

    Example zones file:

    A 3 3
    B 2 2
    A 3 1
    C 2 2
    D 3 10
    Z 1 9

    Example output (to the monitor):

    A = 12 B = 4 C = 4 D = 30
    E = 0 F = 0 G = 0 H = 0
    I = 0 J = 0 K = 0 L = 0
    M = 0 N = 0 O = 0 P = 0
    Q = 0 R = 0 S = 0 T = 0
    U = 0 V = 0 W = 0 X = 0
    Y = 0 Z = 9

    Observations:

    In this example only the zone A had multiple entries, but it is possible for some zones to have multiple entries and others have none.

    You don’t need to validate any input, you can assume all data to be in the zones file to be valid.

    Format the output to print only four zones per line separated by the tab character."

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You should only need one array to store the areas.
    Just keep things in alphabetical order as you go.

    int areas[26]; //make sure you initialize all values to zero.

    Conceptually and/or programmatically you just need to know that
    areas[0] <=> areas['A']
    areas[1] <=> areas['B']
    areas[2] <=> areas['C']
    areas[3] <=> areas['D']
    .....


    As you encounter a letter in the file just tranlate it to the appropriate array index. (A function may be good for this)
    Then add the the new area to the value at areas[index]

    so...
    Code:
    if 'A' then index = 0
    if 'B' then index = 1
    areas[index] += area;
    When you're finished processing the file print the array using the same translation method to show the letter instead of the numeric index
    Last edited by spydoor; 03-23-2006 at 04:47 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Thanks for the help. I wrote the program, and I understand how I want it to work. It compiles right now, but I get a segmentation fault with a.out. I commented out the fopen part, and the program runs fine from there, but without any input.

    Code:
    fptr = fopen("zones", "r");
    
      if (fptr == 0)
      {
        printf("Open Failed!\n");
      }
      else
      {
        while (!feof(fptr))
        {
          getData(fptr, &length1, &width1, &letter1);
          data[letter1] += sumData(length1, width1);
        }
      }
    
      displayResults(data);

    Any idea what's wrong there?

    I can post the entire code if need be...
    Last edited by seawolfxix; 03-23-2006 at 08:46 PM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Please post the entire code. Also bring some soda.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Yes, your entire code will be helpful for us to help you with this.

    Well done on attempting the code yourself though. That is exactly what we want to see here.

    Just a minor aside note... it might be more appropriate to name your 'sumData' function 'getArea' or 'productData', because the area is actually length times width, and not length + width. It's a minor point, but I hope worth noting.

    Continue the good work.

    Eddie

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by seawolfxix
    Code:
        while (!feof(fptr))

    Why you shouldn't do this
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (!feof(fptr))
    Read the FAQ on why using feof() in a loop control is bad.

    > data[letter1]
    Lemme guess, this is a pointer rather than an array

    > I commented out the fopen part, and the program runs fine from there
    It's never wise to assume that the bit you commented out was the thing which was actually wrong.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    edit: will repost next week
    Last edited by seawolfxix; 03-24-2006 at 02:04 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > *num3 -= ASCIIbase;
    Why are you doing this.

    What's actually in your input file, if it's things like
    2 3 4
    Then you don't need to do anything extra apart from the fscanf

    If it's
    A B C
    Then you need something other that %d in your fscanf's
    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.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    If I scan in the character as an integer, won't an A be 65? I was then trying to subtract 65 so that I could reference the first value of my array(data[0]). Am I doing it wrong? Is that where I get my segmentation fault?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're doing something wrong.
    If you're expecting 'A', then try using char pointers, and %c as the conversion.

    Or print the value of your index just before you use it to subscript your array.
    Basically, debug it to find out yourself.
    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.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    I can scan in A as a character, but I need to convert that to an integer somehow so that I can make the correct reference in my array. Is this possible?

    For example, I need to scan in A as a character, but somehow turn that into a 0 so I can change the first value in my array. B should be 1, C should be 2, etc... for data[0], data[1], data[2], etc...
    Last edited by seawolfxix; 03-24-2006 at 01:49 PM.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Nevermind, I figured it out. Here's what I did to compensate:

    Code:
    void getData(FILE *data, int *num1, int *num2, int *num3)
    {
      char num5;
    
      fscanf(data, "%c", &num5);
      *num3 = num5;
      *num3 -= ASCIIbase;
      fscanf(data, "%d", num1);
      fscanf(data, "%d", num2);
    
      return;
    }

    I pulled down my entire code because we can get in trouble if somebody else copies it. I will repost after the assignment is due for others to reference in the future.
    Last edited by seawolfxix; 03-24-2006 at 02:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help
    By shadowctr in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2005, 07:29 PM
  2. How to get started?
    By anoopks in forum Linux Programming
    Replies: 0
    Last Post: 01-14-2003, 03:48 AM
  3. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM