Page 1 of 1

Stack of Arrays.

Posted: Sun Aug 22, 2004 6:12 am
by _.B._
Greetings!
I've spent over 2 hours trying to find out how to do this, but still NADA :o
So, once again, I need your help.
Is it possible to create a "stack" of "arrays of numbers"?
I'm trying to do this:

[cpp]#include <iostream.h>
#include <stack.h>

typedef unsigned int Mobiles[4];

stack <Mobiles> Pila;
Mobiles Mobil;
unsigned int Casos,C1,C2;

int main()
{ cin >> Casos;
for (C1 = 0;C1 < Casos;C1++)
{ while (!Pila.empty())
Pila.pop();
for (C2 = 0;C2 < 4;C2++)
cin >> Mobil[C2];
if (Mobil[0]*Mobil[1]*Mobil[2]*Mobil[3] == 0)
Pila.push(Mobil);

}
return 0;
}[/cpp]
But it'll give me an error when I'm trying to push Mobil into Pila :o

Thanks in advance!

Posted: Sun Aug 22, 2004 6:51 am
by shamim
From What I understand you are trying to create a stack whose element type is an array of integer, size-4.

Perhaps it is better to use a struct to hold the array elements.

Here is how you can change your code.

[cpp]
#include <iostream.h>
#include <stack>

using namespace std;


struct Mobiles
{
int ar[4];
};



stack <Mobiles> Pila;

Mobiles Mobil;

unsigned int Casos,C1,C2;

int main()
{ cin >> Casos;
for (C1 = 0;C1 < Casos;C1++)
{ while (!Pila.empty())
Pila.pop();
for (C2 = 0;C2 < 4;C2++)
cin >> Mobil.ar[C2];
if (Mobil.ar[0]*Mobil.ar[1]*Mobil.ar[2]*Mobil.ar[3] == 0)
Pila.push(Mobil);

}
return 0;
}
[/cpp]

Good Luck. :wink:

Thanks!

Posted: Sun Aug 22, 2004 7:40 am
by _.B._
Greetings Shamim!
Now you are helping me with C++ too? :wink:
Thanks for the prompt answer, and for the useful information! :D
It seems that it'll work just fine 8)
I'm learning C++, and I'm trying to solve "Not so Mobile" (http://acm.uva.es/p/v8/839.html) with Stacks to learn how to use them. Well, now with Structs too :wink:

Thanks!, and keep posting!

EDITED: Accepted - 0:00.492 - 468 :D
A bit slow, but ACed at first try 8)