Thread: Question about arrays?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    Question about arrays?

    Example 7.9 Calculate freight charges

    Design an algorithm that will read an input weight for a item to be shipped., search an array of shipping weights and retrieve a corresponding freight charge. In this algorithm, two paired arrays, each containing six elements, have been established and initialized. The array 'shipping_weights' contains a range of shipping weights in grams, and the array 'freight_charges' contains the corresponding array of freight charges in dollars, as follows:

    My question is this for shipping weights it has values

    1-100
    101-500
    501-1000
    1001-3000
    3001-5000
    5001-9999

    How am I suppose to store these numbers in an array?
    Or what am I suppose to store in the shipping weights array?
    I just can't figure out what to do?
    This problem isn't for class I just working on my own I have been in college in the past but I'm not now. this problem is from simple program design book

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    You know I got to thinking maybe a two dimensional array could work!

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I think that maybe using two arrays as per the question would be better than a 2-dimensional array.

    For the first array you'd have an array of structs (the struct would represent the range and therefore have two values: the lower and upper values for the range).

    The second array would be an array of floating point numbers which correspond with the freight charge in dollars.

    I'm a bit confused because two arrays are not required. You could just have the range and freight charge all in one struct and have a single array with elements of that struct. Weird. If that question is verbatim from the book then it's certainly ambiguous enough to make me wonder what it's really asking...

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Yeah it's exactly from the book
    I don't know a thing about structures
    but you would suggest using a structure. I guess Thanks I guess I got some reading to do I don't want anyone to do the problem for me I'm hoping this gives me enough insight to work on the problem I'll post later if I solved the problem or not I don't think its hard at all so I mean I have a little bit experience I wonder how I could even use two arrays how do you store 2 values in one array I mean that's why I suggested a 2 dimensional array I mean the chapter is on arrays not structures so that might be cheating Plus its suggesting a paired or aka parallel array not a 2 dimensional array like I suggested

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    do you think it means actually use three arrays one for the low value and one for the high value and one for the freight charges

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by artistunknown
    do you think it means actually use three arrays one for the low value and one for the high value and one for the freight charges
    According to what you quoted, the requirements clearly state: "In this algorithm, two paired arrays, each containing six elements, have been established and initialized."

    Considering that the shipping weights appear to be integers covering a contiguous range, it possible that the array only stores the start of each subrange, or the end of each subrange.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well, that makes sense I guess it would have to be the end value because I don't think the beginning value would work because I couldn't test for 9999 and have it be six elements

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    Considering that the shipping weights appear to be integers covering a contiguous range, it possible that the array only stores the start of each subrange, or the end of each subrange.
    Yes, if that's what the question is asking (i.e. do this without structs) then two arrays (e.g. one of int and one of double/float) would be a reasonable approach.

    Thinking about it, that's the only reasonable way to interpret the question given the information supplied.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by artistunknown View Post
    Well, that makes sense I guess it would have to be the end value because I don't think the beginning value would work because I couldn't test for 9999 and have it be six elements
    I guess the only "trick" there is that you'll have to make an assumption on the minimum value of the first range (and that's probably what I'd do)

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Yeah I guess its not only the end value that would be a problem but the minimum value too I guess I just didn't think of it because from 1-100 its 3.00 dollars freight charge but what if I put in a negative number or 0

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Logically, an item cannot have no weight or a negative weight, so you can check for that separately first before searching the array for the subrange that it falls into.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    You know the more I think about it I'm still stuck because if I use unsigned int array it includes 0 which I can't include 0 in the problem
    Is there some kind of logic I'm missing here I'm back to where I started how do you put two values in one array slot or index I still wonder if Paired arrays can mean more than just two arrays but 2 or more arrays another name for paired arrays is parallel and from what I understand about parallel arrays you can have as many as you like

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    oh sorry I posted before I saw your post laserlight so I will try and code it the way you said makes sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays
    By Houhie in forum C Programming
    Replies: 12
    Last Post: 06-13-2012, 10:15 AM
  2. Question about 2D arrays?
    By agentxx04 in forum C Programming
    Replies: 4
    Last Post: 12-16-2004, 12:31 PM
  3. Question on 2D Arrays
    By Zildjian in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2004, 08:34 PM
  4. help me out with this question on arrays
    By datainjector in forum C Programming
    Replies: 10
    Last Post: 08-12-2002, 09:13 AM
  5. question about arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-24-2002, 06:27 PM