Solved

Make a stack with float and int values

Posted on 2006-11-30
7
552 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 124
VB6 Compile Compatibility Issue 4 87
parse convert xml feed to text (python) 2 66
Not needed 13 92
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now