Solved

Make a stack with float and int values

Posted on 2006-11-30
7
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular Expression Calculator Tester 2 96
printf performancy 11 101
collection output issue 9 91
Where to download python requests library 6 105
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.

737 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