C++: Data Structure / queue

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

Make a program that creates two rows F1 and F2, of size 10, each. Read 20 integers, and if the Read number is even, enter it in the queue F1 and, if odd, in row F2. In the sequence, if the number Read is multiple of 3, remove an element from the F1 queue, storing it in a auxiliary variable and then write it in the video and enter it in the F2 queue. If the Read number is multiple of 5, remove an element from queue F2, storing it in an auxiliary variable and then writing it to the video and enter it in the F1 queue. A number can be both multiple of 3 and multiple of 5 and in this case, present a message in the video and do nothing in the queues, moving on to the reading of the next number. Stop reading when have already read the 20 numbers or when overflow occurs or underflow in some line.

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;
}
Author: Bruno Peres, 2017-08-09

1 answers

For starters, if it is a data structure exercise 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 rows Fila F1; and Fila F2; to represent the two rows. In C++, you can also create methods for the framework, allowing easy startup, storage, access, withdrawal, etc. If 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 queue position F1 is used to indicate that of queue F2 as well, which is wrong. Each queue has to have its start Index and also its end index. Just by creating a queue structure type, this problem is already solved.

I also recommend using more intuitive nomenclatures (for example, it is good for indexes to use the letter "i", counters to use the letter "c", temporary numbers to use "t", things thus) and adequate indentation (maintain the level of the scope), both measures aimed at readability. Many teachers strongly charge this kind of thing.

Any questions?

 1
Author: RHER WOLF, 2017-08-09 06:42:43