Solved

Make a stack with float and int values

Posted on 2006-11-30
7
553 Views
Last Modified: 2008-01-09
I have this program that I have to do where I must create a message with 2 random  numbers with a int and float data type.
message int =12  message float=12.2
Then I have to put the numbers onto a stack.  How do  I create a float random number for the stack and how do I get them to show at the same time or put them on at the same time when creating my stack?
0
Comment
Question by:MissProgrammer
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
score_under earned 250 total points
ID: 18048222
The stack could be just a struct specially designed: This code will have functions to simeltaneously push and pop from a stack (struct, actually). Since I don't have my C compiler to hand, I can't test this, but you should be able to correct it if there is anything wrong.

(inside struct)
int point=0;
int random_num[256];
float random_dec[256];
(after struct)
void Push_That( YOUR STRUCT TYPE *struct_ptr, int the_int, float the_dec)
{
struct_ptr->random_num[struct_ptr->point] = the_int;
struct_ptr->random_dec[struct_ptr->point] = the_dec;
struct_ptr->point++;
return;
}
void Pop_That( YOUR STRUCT TYPE *struct_ptr, int* the_int, float* the_dec)
{
*the_int=struct_ptr->random_num[struct_ptr->point];
*the_dec=struct_ptr->random_dec[struct_ptr->point];
struct_ptr->point--;
return;
}
0
 
LVL 2

Expert Comment

by:score_under
ID: 18048270
Oops... On Pop_That, put struct_ptr->point--; above the other 2 lines of code, but DO NOT DO IT ON THE PUSH function ok?
0
 

Author Comment

by:MissProgrammer
ID: 18048375
When I put this in:

#ifndef TEST_H
#define TEST_H

using namespace std;

struct Test{


int point=0;
int random_num[256];
float random_dec[256];
void Push_That( unsigned *struct_ptr, int the_int, float the_dec);
void Pop_That( unsigned *struct_ptr, int* the_int, float* the_dec);
};
void Test::Push_That( unsigned *struct_ptr, int the_int, float the_dec)
{
struct_ptr->random_num[struct_ptr->point] = the_int;
struct_ptr->random_dec[struct_ptr->point] = the_dec;
struct_ptr->point++;
return;
}
void Test::Pop_That( unsigned *struct_ptr, int* the_int, float* the_dec)
{
*the_int=struct_ptr->random_num[struct_ptr->point];
*the_dec=struct_ptr->random_dec[struct_ptr->point];
struct_ptr->point--;
return;
}
#endif

I get these errors:

\program files\microsoft visual studio\myprojects\tttt\test.h(9) : error C2252: 'point' : pure specifier can only be specified for functions
c:\program files\microsoft visual studio\myprojects\tttt\test.h(17) : error C2227: left of '->random_num' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(17) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(18) : error C2227: left of '->random_dec' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(18) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(19) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(24) : error C2227: left of '->random_num' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(24) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(25) : error C2227: left of '->random_dec' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(25) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(26) : error C2227: left of '->point' must point to class/struct/union
Error executing cl.exe.
0
 
LVL 4

Assisted Solution

by:chhokra_expert
chhokra_expert earned 250 total points
ID: 18055231
is there a reason you can't use stl stack? also i'm assuming int random is different from trucated version of float

create a struct / class to contain the float and int parts. initialize both member vars and push the struct ptr on to the stack. ps: i haven't compiled this code, but should be relative easy to fix if any errors come up.

#include <iostream>
#include <stack>
#include <cstdlib>
#include <time.h>

using namespace std;

class Message
{
public:
int f;  //these are public for easy access. could use a struct as well if these are teh only things / operations
int n;


Message() : f(rand()*100.0F/RAND_MAX), n(rand())  //initialize members with random values
{
    //add better initializers here
    ;
 }

virtual ~Message() { ; //add dtor code here if needed }
};

int main(int argc, char** argv)
{
    srand(time(NULL)); //initialize psuedo rand gen
    stack<Message *> messageStack;
    Message *pM = new Message();
    //add null check here

    messageStack.push(pM);  //push newly created message on the stack

    //do whatever else you want here with stack

    //clean up
    while(!messageStack.empty())
    {
         delete messageStack.top();
         messageStack.pop();

    }

   return 0;
}
0
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 18055333
btw, f should be a float not an int.. and you want to either comment out the destructor entirely or split it separate lines
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 97
wordlen challenge 3 69
index Out OF Range Exception error 4 63
Is there any way to copy all SSRS reports/datasets/data sources  to new server? 3 63
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question