C++: Data structure / Queue

Asked

Viewed 497 times

0

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 year below:

Make a program that creates two rows F1 and F2, size 10, each. Read 20 whole numbers, and if the number read is even, enter it in the queue F1 and, if odd, row F2. In sequence, if the number read is multiple of 3, remove an item from row F1 by storing it in a auxiliary variable and then write it in the video and insert it in row F2. If the number read is a multiple of 5, remove an item from row F2, storing it in an auxiliary variable and then writing it in the video and enter it in row F1. A number can be either a multiple of 3 or 5 and, in this case, present a message in the video and do nothing at queues, moving on to reading the next issue. Stop reading when the 20 numbers have already been read or when overflow occurs 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;
}
  • 1

    What error does it present ?

  • I’m not able to write the lines. And the result is not expected

  • 1

    That statement is a little strange at least. Because if you enter the number 3 theoretically you are right in 2 cases, the odd being is multiple of 3, which would remove an element from the F1 to the F2 and inserted another to F2. This also implies that you cannot start with an odd number otherwise you have nothing to remove from F1

  • Yeah. I couldn’t quite figure it out either!

  • 1

    And the same thing happens for a multiple of 5 that has to be odd if it ends in 5 or even if it ends in 10. I advise you to try to review the statement with the teacher to clarify the objective of the work and how it is supposed to work.

  • I’ll talk to him. Thank you very much!

Show 1 more comment

1 answer

1


To begin with, if it is an exercise in data structures then one is expected to create a sort of queue structure. 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 both rows. In C++, you can also create methods for the structure, enabling easy initialization, storage, access, withdrawal, etc. If not allowed, at least you should have the right to create functions that initialize, access and change the queues.

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

I also recommend using more intuitive nouns (for example, it’s good for indexes to use the letter "i", counters to use the letter "c", temporary numbers to use "t", things like that) and proper indentation (maintain the scope level), both measures aiming at readability. Many teachers strongly charge this kind of thing.

Any doubt?

  • Thank you very much! You helped me a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.