Thread: Arrays and sums of contents.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    Arrays and sums of contents.

    You've got a double array named payments[M], and a char array named l_plates[M][N], both of which have been sorted.

    For the contents of l_plates that are THE SAME, you want the corresponding SUM of contents of payments stored on the first of equal slots, while turning the remaining equal slots into '-1' every time. How do you go about this?

    Example: (why isn't this working?)

    Code:
    for (beg = 0, end = 0; beg < M-1; beg++) {
            for (end = beg + 1; (end < M) && (strcmp(l_plates[beg], l_plates[end]) == 0); end++) {}
            if (end > beg + 1) {
            payments[beg] = sum_pay(payments, beg, end);
            } 
        }
    
    
    double sum_pay (double payments[M], int start, int finish) {
        int i, sum;
        
        for (i = start, sum = 0; (i < M) && (i < finish); i++) {
            sum = sum + payments[i];
        }
        for (i = start + 1; i < finish; i++) {
            payments[i] = -1;
        }
        return (sum);
    What changes have to be made so that this code would work? I've been trying to figure this out for hours now...

    Edit: this is just part of the code, of course. The whole thing does compile, and things seem to be working great until I get to this part (at least according to gdb).
    Last edited by Xpl0ReRChR; 11-24-2011 at 10:28 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int i, sum;
    Is there a reason you declared sum as an int instead as a double?

    Code:
    for (end = beg + 1; (end < M) && (strcmp(l_plates[beg], l_plates[end]) == 0); end++) {}
    Is there something the above loop is supposed to do? It does nothing right now.
    Edit: Maybe, it is just setting end to a valid value; but, I think the value is 1 more than wanted.

    Tim S.
    Last edited by stahta01; 11-24-2011 at 11:16 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    You're right, I declared sum as int by accident.
    I also fixed the thing about the end.

    But the whole thing is still not working right.

    eg. The price for buses is 16.50, but when the SAME bus goes two times and pays 0.20 each time, payments[i] for that bus should be (16.50-0.20)*2, but instead I'm getting 16.40.

    Oddly enough, it works correctly when no license plate is given more than once.

    Can someone help?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Just got it working, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me with Evaluating Sums?
    By matthayzon89 in forum Tech Board
    Replies: 1
    Last Post: 09-26-2010, 04:28 PM
  2. Arrays & Sums
    By mrlooneytoon in forum C Programming
    Replies: 9
    Last Post: 04-05-2007, 06:35 PM
  3. Sums...
    By karb0noxyde in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2004, 06:39 PM
  4. Structures, Arrays, Sums...
    By Inept Pig in forum C Programming
    Replies: 19
    Last Post: 04-19-2002, 04:07 PM
  5. transfering contents of 3 arrays to 1
    By myth in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 08:35 PM