C ++: Data structure / Queue

0

I made a data structure algorithm. I put all my knowledge into it. I can not find the error. I leave the exercise statement below:

  

Make a program that creates two F1 and F2 queues, each one of size 10.   Read 20 integers, and if the number read is even, insert it in the   F1 and, if it is odd, in row F2. Then, if the number read is   multiple of 3, remove an element from row F1, storing it in a   auxiliary variable, and then write it in the video and insert it into the F2 queue.   If the read number is multiple of 5, remove an element from row F2,   by storing it in an auxiliary variable, and then write it in the video and   insert it in the F1 queue. A number can be either multiple of 3 or   5, and in this case, post a message on the video and do nothing in the   reading the next number. Stop reading when   have already been read or when overflow or   underflow in some queue.

int main()
{
    int f=-1, r=-1, f1[10], f2[10], cont=0, val=0, aux=0, tam=9;

do{
    cout << "Informe um valor: " << endl;
        cin >> val;
    cout << " " << endl;

    if(val%2==0){
            if(r==tam){
                cout << "OVERFLOW!" << endl << endl;
                    return 0;
            }else{
                f1[++r]=val;
            }
    }else{
        if(r==tam){
            cout << "OVERFLOW!" << endl << endl;
                return 0;
        }else{
                f2[++r]=val;
        }
    }

    if(val%3==0){
            if(r==f){
                cout << "UNDERFLOW!" << endl << endl;
                    return 0;
            }else{
            aux=f1[f+1];
            f++;
            if(f==r){
                f=r=-1;
            cout << "Valor retirado: " << aux << endl << endl;
            f2[++r]=aux;
            }
            }
    }

    if(val%5==0){
            if(r==f){
                cout << "UNDERFLOW!" << endl << endl;
                    return 0;
            }else{
            aux=f2[f+1];
            f++;
            if(f==r){
                f=r=-1;
            cout << "Valor retirado: " << aux << endl << endl;
            f1[++r]=aux;
            }
            }
    }

    if(val%3==0 && val%5==0){
        cout << "Numero mulitplo de 3 e 5!" << endl << endl;
    }

cont++;
}while(cont < 20);
return 0;
}
    
asked by anonymous 09.08.2017 / 03:38

1 answer

1

To begin with, if it is an exercise in data structures then you are expected to create a queue structure type. For example,

// Tipo estrutura de fila
struct Queue {
    int iStt ;        // Índice de início
    int iEnd ;        // Índice de fim
    int elems[10] ;   // Elementos na fila
} ;

is enough to structure. With this, you can locally define the variables queues Fila F1; and Fila F2; to represent the two queues. In C ++, you can also create methods for the structure, allowing easy initialization, storage, access, withdrawal, etc. If it is not allowed, you should at least have the right to create functions that initialize, access, and change queues.

From what I saw in your code, the index used to indicate row position F1 is used to indicate row F2 as well, which is wrong. Each row must have its start index and its end index as well. Just to create a queue structure type, this problem is already solved.

I also recommend using more intuitive nomenclatures (for example, it's good to use "i", counters use the letter "c", temporary numbers use "t", and so on) and proper indentation (maintain the scope level ), both measures for readability. Many teachers charge heavily for this kind of thing.

Any questions?

    
09.08.2017 / 08:37