Thread: Code problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Code problem

    Hi! I've got a problem with my code. Everytime I run my program, I get an error message that I think has to do with array indexing, but I can't figure it out. This is the code that causing trouble along with the necessary info about the arrays:

    Code:
    vector<int> tempArray(s);
    vector<int> cowloc(c);
    vector<int> gaps(m-1);
    
    
        for(int i=0; i<s; i++)
            tempArray[i]=0;
        
        for(int i=0; i<c; i++)
        {
            cin >> temp;
            tempArray[temp-1]=1;
        }
        high=0;
        for(int i=0; i<s; i++)
        {
            if(tempArray[i]==1)
            {
                cowloc[high]=i+1;
                high++;
            }
        }
        min=cowloc[0];
        max=cowloc[high-1];
        
        //Rhis part is where the trouble starts
        for(int i=min; i<max-1; i++)
        {
            gap=0;
            
            for(int k=cowloc[i]; tempArray[k+1]==0; k++)
            {
                gap++;
                l=k;
            }
            for(int j=m-2; j>-1; j--)
            {
                if(gap>gaps[j])
                {
                    gaps[j]=gap;
                    break;
                }
            }
        }
    If anyone could help it would be greatly appreciated. Thanks!!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    vector<int> tempArray(s);
    s is undefined.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    s is undefined?

    The user inputs the values of m, s, c.
    cin >> m >> s >> c;

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    for(int i=0; i<s; i++)
            tempArray[i]=0;
    They are already initialized to 0.

    Code:
    cin >> temp;
            tempArray[temp-1]=1;
    If temp is greater than s, you are out of bounds.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    C (1 <= C <= S)

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    first, give your variables meaningful names. s, m and c are about as helpful and sensible as "finegans wake". Second, use a meaningful thread title. "code problem" is not very specific.

    finally post small, complete and compileable code samples.

    This won't solve your current problem but it'll certainly get more people to help you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    ok
    this is my whole code
    This is a relatively famous problem: Barn repair
    It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.

    The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.

    Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.

    Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.

    Print your answer as the total number of stalls blocked.



    Code:
    /*
    ID: mhayter1
    PROG: barn1
    LANG: C++
    */
    
    #include<iostream>
    #include<fstream>
    #include<vector>
    using namespace std;
    
    int main()
    {
        int m=0,s=0,c=0, temp=0, gap=0, min=0, max=0,high=0, sum=0,l=0;
    /*
        ifstream fin("barn1.in", ios::in);
        ofstream fout("barn1.out", ios::out);
        
        if(!fin)
        {
            cerr << "File could not be opened\n";
            exit(1);
        }
        if(!fout)
        {
            cerr << "File could not be opened\n";
            exit(1);
        }
        */
        cin >> m >> s >> c;
        
        if(m>=c)
        {
            cout << c << "\n";
            return 0;
        }
        vector<int> tempArray(s);
        vector<int> cowloc(c);
        vector<int> gaps(m-1);
        
        for(int i=0; i<s; i++)
            tempArray[i]=0;
        
        for(int i=0; i<c; i++)
        {
            cin >> temp;
            tempArray[temp-1]=1;
        }
        high=0;
        for(int i=0; i<s; i++)
        {
            if(tempArray[i]==1)
            {
                cowloc[high]=i+1;
                high++;
            }
        }
        min=cowloc[0];
        max=cowloc[high-1];
        
        //Error in this block of code
        for(int i=min; i<max-1; i++)
        {
            gap=0;
            
            for(int k=cowloc[i]; tempArray[k+1]==0; k++)
            {
                gap++;
                l=k;
            }
            for(int j=m-2; j>-1; j--)
            {
                if(gap>gaps[j])
                {
                    gaps[j]=gap;
                    break;
                }
            }
        }
        for(int j=m-2; j>-1; j--)
        {
            cout << "G= " << gaps[j] << "\n";
            sum+=gaps[j];
        }
        cout << "max= " << max << "\n";
        cout << "min= " << min << "\n";
        cout <<"sum= " << sum << "\n";
        
        cout << max-min+1-sum << "\n";
        
        int y;
        cin >>y;
              
        return 0;
    }
    does that help at all?

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    the sample is:

    4 50 18
    3
    4
    6
    8
    14
    15
    16
    17
    21
    25
    26
    27
    30
    31
    40
    41
    42
    43

    OUTPUT FORMAT
    A single line with one integer that represents the total number of stalls blocked.
    SAMPLE OUTPUT (file barn1.out)
    25

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> does that help at all?
    Yes.

    >> vector<int> cowloc(c);
    At that point, c is 18, so cowloc has 18 entries.

    >> //Error in this block of code
    >> for(int i=min; i<max-1; i++)
    When you run this loop, min is 3 and max is 43.

    >> for(int k=cowloc[i]; tempArray[k+1]==0; k++)
    On that line, you access cowloc[i]. Since i runs from min to less than max-1, it is attempting to access elements at location 3 to 41, but cowloc only has 18 elements. Once it attempts an index higher than 17, it fails.

    Better variable names on your index variables (instead of i) might have made it easier to spot that you were using the wrong variable.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    Thanks a bunch Daved. I didn't see that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM