Runtime error - don't know why

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
inigrow
New poster
Posts: 1
Joined: Fri Oct 17, 2008 7:19 pm

Runtime error - don't know why

Post by inigrow » Fri Oct 17, 2008 7:24 pm

Hi!
I found this online judge program today and I thought I would test it.
But when I tested it with a program that runs perfectly in Dev-c++ I got Runtime error. Why isn't there
some kind of error reporting on this stuff. I have no idea what is causing this error.

Please take a look at the source code and help me find it. It's so confusing and irritating and I really want to know
why. I use c++ on windows.

Thanks

Code: Select all

#include <iostream>

using namespace std;

enum Type {
    EXPRESS,
    ORDINARY
};

int addTrain (int queue[], int &pos, int leave)
{
    pos++;
    queue[pos-1] = leave;
    
    return 0;
}

int main()
{    
    int ae, ao, le, lo, t;
    int selType, selNum;
    int i, j;

    const int SIZE = 20;
    
    int expQueue[SIZE], ordQueue[SIZE];
    int expWait[SIZE], ordWait[SIZE];
    int nExpWaits = 0, nOrdWaits = 0;

    int expLen=0, ordLen=0;
    
    int expTrain, ordTrain;
    
   
    for (i=0; i<SIZE; i++) {
        expWait[i] = 0;
        ordWait[i] = 0;
        expQueue[i] = 0;
        ordQueue[i] = 0;
    }
    
   
    cin >> ae >> ao >> le >> lo >> t;
    
    
    addTrain(expQueue, expLen, le);
    selType = EXPRESS; selNum = expLen-1;
    
    cout << "0 0 0" << endl;
    
    
    for (i=1; i<=t; i++) { 
    
    	
    	if (i % ae == 0) {
            addTrain (expQueue, expLen, le);
        } 
        if (i % ao == 0) {
            addTrain (ordQueue, ordLen, lo);
        }
        
        if (selType == EXPRESS && expQueue[selNum] > 1) {	
            expQueue[selNum]--;
            
        } 
        else if (selType == ORDINARY && ordQueue[selNum] > 1) {
            ordQueue[selNum]--;  
            
        } else {
            expTrain = -1;
            
            for (j=0; j<expLen; j++) {
                if (expQueue[j] > 1 && j != selNum) {
                    expTrain = j;
                    break;
                }
            }
            ordTrain = -1;
            for (j=0; j<ordLen; j++) {
                if (ordQueue[j] > 0 && j != selNum) {
                    ordTrain = j;
                    break;
                }
            }
            
            if (expTrain >= 0) {
                selType = EXPRESS;
                selNum = expTrain;
            }
            else if (ordTrain >= 0) {
                selType = ORDINARY;
                selNum = ordTrain;
            }
                
            
        }
    		
    	nExpWaits = 0;
    	nOrdWaits = 0;
       
    	for (j=0; j<expLen; j++) {
            if (!(j == selNum && selType == EXPRESS) && expQueue[j] > 1) {
                if (i != t) expWait[j]++;
                
                nExpWaits++;
            }
        }
        for (j=0; j<ordLen; j++) {
            if (!(j == selNum && selType == ORDINARY) && ordQueue[j] > 1) {
                if (i != t) ordWait[j]++;
                
                nOrdWaits++;
            }
        }
    
    	if (i % ae == 0 || i % ao == 0) {
    	   cout << i << " " << nExpWaits << " " << nOrdWaits << endl;
    	   
        }
    }
    cout << "0" << endl;
    
    int avgExp=0, avgOrd=0, avgAll=0;
    for (i=0; i<expLen; i++) {
        avgExp += expWait[i];
    }
    for (i=0; i<ordLen; i++) {
        avgOrd += ordWait[i];
    }
    
    avgAll = (avgOrd + avgExp) / (ordLen + expLen);
    avgOrd /= ordLen;
    avgExp /= expLen;
    cout << avgExp << " " << avgOrd << " " << avgAll << endl << "0" << endl;
    
    for (i=0; i<expLen; i++) {
        cout << "E" << (i+1) << " " << expWait[i] << endl;
    }
    for (i=0; i<ordLen; i++) {
        cout << "O" << (i+1) << " " << ordWait[i] << endl;
    }
    
    return 0;
}

Fluffymoo
New poster
Posts: 7
Joined: Wed Oct 15, 2008 5:05 pm

Re: Runtime error - don't know why

Post by Fluffymoo » Wed Nov 19, 2008 12:13 pm

check if there can be cases causing your algo to divide by zero, for a start...in your case...
ordLen != 0
expLen != 0
ordLen != -expLen
Also your fucntion "int addTrain(int queue[], int &pos, int leave)"...first parameter does not refer to any global array of ints, nor does it work with any array outside of function, simply, it has no effect. I suggest one of the following:
declare expQueue[SIZE] as a global array, right under "using namespace std;" and rewrite addTrain to this

Code: Select all

void addTrain (int pos, int leave)
{
    expQueue[pos] = leave;
}
or just erase that function completely and everytime you would have called it via

Code: Select all

addTrain (expQueue, expLen, le);
just type

Code: Select all

expQueue[expLen]=le;
instead...
Same goes for

Code: Select all

addTrain (ordQueue, ordLen, lo);
->

Code: Select all

ordQueue[ordLen]=lo;
Finally, i have no idea what problem solution you described, so my notes are usefull only to make your code do what you wanted it to do.

Post Reply

Return to “C++”