mrnutty 761 Senior Poster

May I get some instructions to improve it?

Yes, don't use iostream.h, use iostream, don't use void main, use int main,
don't use gotoxy, find a replacement, and don't use goto. Also why
do you have a case '8' if you don;t use it?

mrnutty 761 Senior Poster

go with opengl(open graphics library) or jogl (java opengl).

mrnutty 761 Senior Poster

do you mean (q!=p) instead of (!=p)

mrnutty 761 Senior Poster

Check if this works. It compiles on Visual studio 2008 express.

/****************************************************************
   FILE:      List.h
   AUTHOR:    Justin R. Smith
   LOGON ID:  Z136340
   DUE DATE:  9/8/09

   PURPOSE:   Contains prototypes and class definitions for a 
              doubly linked list.
****************************************************************/

 
#include <iostream>
template <class T>
class Iterator;

template <class T>
class List;

template <class T>
std::ostream& operator<<(std::ostream&, const List<T>&);


template <class T>
struct LNode
       {
       T data;
       LNode<T>* prev;
       LNode<T>* next;
       
       LNode(const T&);
       };
/****************************************************************
   FUNCTION:   LNode(const T&);
   ARGUMENTS:  newData
   RETURNS:    nothing
   NOTES:      contstructor for the LNode struct, sets data equal
               newdata and prev and next equal to NULL
****************************************************************/

       template <class T>
       LNode<T>::LNode(const T& newdata)
       {
       data = newdata;
       prev = next = NULL;
       }
       
template <class T>
class List
      {
      friend std::ostream& operator<< <>(std::ostream&, const List<T>&);	
      friend class Iterator<T>;      

      private:
              LNode<T>* head;
              LNode<T>* tail;
      public:
             List();
             ~List();
             List(const List<T>&);
             List<T>& operator=(const List<T>&);
             void clear();
             int size() const;
             bool empty() const;
             const T& front() const;
             T& front();
             const T& back() const;
             T& back();
             void copyList(const List<T>&);
             void push_front(const T&);
             void push_back(const T&);
             void pop_front();
             void pop_back();
             bool operator==(const List<T>&) const;
             bool operator<(const List<T>&) const;
	     Iterator<T> begin() const;
	     Iterator<T> end() const;
	     Iterator<T> insert(Iterator<T>, const T&);
	     Iterator<T> erase(Iterator<T> pos);
	     void splice(Iterator<T>, List<T>&);
	     void remove(const T&);
      };
      
      /****************************************************************
      FUNCTION:  List();
      ARGUMENTS: none
      RETURNS:   nothing
      NOTES:     default constructor sets head and tail pointers to NULL
      ****************************************************************/
      template <class T>
      List<T>::List()
      {
      head = NULL;
      tail = NULL;
      }
      
      /****************************************************************
      FUNCTION:  ~List();
      ARGUMENTS: none
      RETURNS:   nothing
      NOTES:     destructor calls the clear function to clear list
      ****************************************************************/
      template …
mrnutty 761 Senior Poster

There is a header called sstream which is very handy in these types of situations. Here is an example, although not compiled.

#include<iostream>
#include<sstream>

using namespace std;

int main()
{
    stringstream convert;
    string result;
    int num = 12345;
    convert << num;
    convert >> result;
    cout<<result<<endl;

   return 0;
}
mrnutty 761 Senior Poster

Convert string to other datatypes via template. See below.

mrnutty 761 Senior Poster

Random number generator under its own namespace.

Its a sample program.

mrnutty 761 Senior Poster
int i = 0;
 

while(!cin >> i)
{
   cout<<"\nNot valid Try again : "<<endl;
   cin.clear();
  while(cin.get() != '\n')
   ;
}

cout<<"Valid\n";
mrnutty 761 Senior Poster

make user prompt to enter the number in ascending order

Its limited to only positive numbers though. (not tested)

int lastNum = -1;
int newNum = 0;
int numEntered = 0;

while(numEntered != 10)
{
     cout<<"Enter  numbers in ascending order : ";
     cin >> newNum;
     while(newNum < lastNum)
   {
      cout<<"Enter a number greater than << lastNum<<" : ";
      cin >>  newNum;
   }
 numEntered ++;
  lastNum = newNum;
}
mrnutty 761 Senior Poster

Basing the code off of what firstperson has above, if you want to loop the program, you could use a while loop like so <snippet removed>

You know you should hint the answer and not give it out. Maybe give
him an example of how to use while loops first?

mrnutty 761 Senior Poster

.

mrnutty 761 Senior Poster

"cannot acces memory at 0x0"

I assume that error means that something is trying to read in an
invalid memory location, one which does not exits as 0x0, which is
NULL.

mrnutty 761 Senior Poster

Here is your program written better. You could have used switch statements, but I wasn't sure if you knew them.

Use code-tags.

/*
Pick a problem and solve
*/

#include <iostream>
using namespace std;

int main()
{

    int x;
    float a,b;

    cout << "Hello, please pick a type of problem from the list. Please use the number of  each: \n\n"; 
    cout << "1. perimiter\n";
    cout <<"2. area\n";
    cout <<"3. addition\n";
    cout <<"4. multiplication\n";
    cout <<"5. subtraction\n";
    cout <<"6. division\n";
    cout <<"7. Quit program. \n";
    cin >> x ;

    //if x is less tha 1 or greater than 7 then throw error
    while(x<1 || x >7)
    {
        cout << "Hello, please pick a type of problem from the list. Please use the number of each: \n\n"; 
        cout << "1. perimiter\n";
        cout <<"2. area\n";
        cout <<"3. addition\n";
        cout <<"4. multiplication\n";
        cout <<"5. subtraction\n";
        cout <<"6. division\n";
        cout <<"7. Quit program. \n";
        cin >> x ;  
    }

    if(x == 1) 
    {

        cout << "Enter the length: "; 
        cin >> a;
        cout<<"\n\n";
        cout << "enter the width: ";
        cin >> b; 
        cout<<"\n\n";
        cout << "The perimiter is: ";
        cout << 2*(a+b);
    }

    else if( x == 2)
    {
        cout << "enter the length: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the width: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The area is: ";
        cout << a*b;
    }


    else if(x == 3)
    {
        cout << "Enter the first number: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the second number: ";
        cin >> …
mrnutty 761 Senior Poster

>In your function, you allocate new memory for the array
>that is passed, which already has been allocated memory.

I suppose he has commented that line.
Hence technically, there are no memory leaks in the first program.

I guess it was late. Didn't see the backlashes.

mrnutty 761 Senior Poster

Depends on :

the number of row of slots that spins.
the number of different symbols.

I think generally it would be :

let N = number of spinning slots;
let S = number of different symbols.

then the probability that one will get the same symbol N times
in a row is = 1/N * 1/N * 1/N ... up until there is S number of 1/N.

Simplify it and get : (1/N)^S; where S is the number of symbols,
and N is the number of slots per slot machine.

Lets try it. Say there are 13 different symbols, 1,2,3,4..j,q,k,A
Say there are 3 different slots that spins.

Then the odds that will get A A A is (1/13)^3 = 0.000455166136.

which is really low. Note (1/13)^3 = 1/13 * 1/13 * 1/13.

mrnutty 761 Senior Poster

also in the first program there is no memory leak.

Are you sure. His program is tricky in a sense that there is a memory
leak. His function is definitely not doing what he thinks its doing.

One way to tell if you have memory leak is to see if every new
is matched with delete. In your function, you allocate new memory for the array
that is passed, which already has been allocated memory. So when
inside the function, the arrays thats passed is located in whatever
memory address that was reserved inside the function. When he
exits out that functions, that memory is no longer in play. Inside
main he has already the array pointing at a specific address.

So the memory inside the function has a leak. Also you use
2 instances of new but only 1 delete. Does that give you a hint?

mrnutty 761 Senior Poster

int [] onedee;
int [][] twodee;
int [][][] threedee;
//so on

import java.util.Random;



public class Main
{
    public static void main(String[] args)
    {
        int [][]twodee =
        {
            {0,0,0},
            {0,0,0}
        };

        Random rand = new Random();
        //populate
        for(int i = 0; i < twodee.length; i++)
        {
            for(int j = 0; j < twodee[i].length; j++)
            {
                twodee[i][j] = rand.nextInt(100);
            }
        }
        //print
        for(int i = 0; i < twodee.length; i++)
        {
            for(int j = 0; j < twodee[i].length; j++)
            {
                System.out.print(twodee[i][j] + "\t");

            }
        }
    }

}
mrnutty 761 Senior Poster

How to use sqrt function in c++ :

int a = 3;
float b = 3;
double c = 3;

float t = sqrt(a); //invalid error. a is of type int. Only float|double allowed
float y = sqrt (  float(a) ); //valid because of typecast
float u = sqrt(b); //valid because b is of type float 
float h = sqrt(c); //valid because c is of type double
mrnutty 761 Senior Poster

comment out code by code to see where the error occurs.

mrnutty 761 Senior Poster

@OP : is the God dam a typecast to pointers, because you hate them?

yellowSnow commented: Very witty post .. brought a smile to my face. +6
Dave Sinkula commented: Partly fixed previous oops. -5
mrnutty 761 Senior Poster

The AI should do something like this :

1) Check if you can move if the middle
2) Check if you can win in 1 move
3) Check if player can win in 1 move
4) Check if opposite corners from user is available
5) Check if any move is available.

mrnutty 761 Senior Poster

I prefer to err on the side of caution. If I have to make an assumption, it is an assumption that things will be worse than expected, not better. Experience has proven that to be a good assumption. :)

Yep, experience does help. But either way its still an assumption.

mrnutty 761 Senior Poster

in you while loop display all letters(maybe on the right side of the screen).
And turn by turn, remove the letters that has been picked. So the user
can see which letters he could get to choose from.

mrnutty 761 Senior Poster

@OP : Just another idea. Generate some random numbers and
add it into a string, making sure that the strings lengths is in the desired
range. And the convert it back into the desired data type.

mrnutty 761 Senior Poster

The function being called is from wxMathPlot, and wxMathPlot is a third party library. It is a good assumption that he either does not have the code to change, or rightfully thinks that changing the code is a really bad idea.

Should have read the rest of his comment/questions, if it even says
it there. Didn't know there was a 3rd party. In which I wasn't invited?

mrnutty 761 Senior Poster

If you have to say 'probably', it is not a safe assumption to make. ;)

Well, lets ask. To the OP is the above code Good enough for
your experiment? And by the way, when can one be sure about
anything, except math. Isn't everything based on assumption?
Shout outs to Descartes.

mrnutty 761 Senior Poster

If you are happy with that result, then OK. But do not expect the same quality of random numbers that a 64 bit generator would have. All you are doing is taking the small period of rand and throwing it into a larger type. The period is not changing, and the distribution is probably going to be much worse.

Maybe but it is probably good enough for this experiment. And if you wanted better distribution then go with
something like me Mersenne Twister.

mrnutty 761 Senior Poster

"im facing the problem on how to use the ctime random,for loop and array "

Seems more like your having problems using Google,
let me help YOU

mrnutty 761 Senior Poster

Look around and observe. They are everywhere. Trees, board,
water, foods, wall, most of which seems to look like fractals.

mrnutty 761 Senior Poster

just pass 2d vector and use only its column?

mrnutty 761 Senior Poster

outputs number from 10000000000 to 99999999999

#include<iostream>
#include<ctime>

using namespace std;

typedef unsigned long long Type;

Type myRand(const Type Max, const Type Min = 0)
{
	return rand()/(float)RAND_MAX * (Max-Min) + Min ;
}

int main()
{
	srand(unsigned(time(0)));

	const Type MAX = 99999999999;
	const Type MIN = 10000000000;

	for(int i = 0; i < 12; i++)
	{
		if(i && i % 4 == 0)
			cout<<endl;

		cout.width(10);
		cout<<myRand(MAX,MIN)<<" ";
	}

	cout<<"\n\n";

}
mrnutty 761 Senior Poster

1) Seed random number
2) use rand()/RAND_MAX * (max-min) + min;

mrnutty 761 Senior Poster

1) Seed random number
2) use rand()/RAND_MAX * (max-min) + min;

mrnutty 761 Senior Poster

When you have a Integral or compound data type variable
and you ask the user to enter a number like so :

int num = 0;
cin >> num;

The cin object does not read in spaces or newline. In this case
when you input say 5<enter> then press enter, the 5 will be
read into the variable num, and the part when you press enter,
the computer sees this as a new line character and leaves it in
the input stream. So the next time when you read a character,
it reads that newline. Thats whats your cin.get() is doing.
Its reading the newline character thats stuck in the input stream.
you can use another cin.get(), or clear the stream, or
read the integers as strings and the convert it accordingly.

mrnutty 761 Senior Poster

Your cin.get doesn't work because it read the newline character.

use another cin.get().

mrnutty 761 Senior Poster

cin does not read white spaces.
use cin.getLine(...), or cin.get(...) or getline(...); They don't skip spaces.

mrnutty 761 Senior Poster

Can you post a example of an input and output?

mrnutty 761 Senior Poster

printf() beats the pants off cout every time.

In what, user-friendliness? cout and cin are much more safer than
printf. Most of the time cout is easier to format than printf; In my
opinion of course. I see that you are a c guy?

mrnutty 761 Senior Poster

"This means that a virtual destructor cannot be pure"

In c++ a destructor can be a pure virtual destructor.

mrnutty 761 Senior Poster

forgot the int in int main(), or does your compiler support default
int. And system command, argghhh. Throw them away. cin.get() would
be a better choice to "pause" the screen.

mrnutty 761 Senior Poster

If you look at the code I posted, you would see that
there is a badchar string, which hold all the guesses.
when the user enters a letter, it checks if he already guessed that
by checking if its in badchar.

You can do similar in c.

char badchar[256] = "";
int badCharIndx = 0;
//get user input
//check if user's input exist in badchar
//if not then add it into badchar
badchar[badCharIndx++] = user's input

mrnutty 761 Senior Poster

I think it would help if you look at it in binary perspective.

int x = 0x26;

in binary 0x26 =
0010 0110 //its separated for you to see it better

now we have this binary number : 0010 0110 and we want to toggle
the 3rd bit : the third bit is 00100110 the one on bold.

Lets use & to toggle it
int x = 0x26; // 0010 0110
int y = 0x22;// 0010 0010
int z = x&y; // ????

Lets see what happens in binary form.

Note that :
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0
0 & 1 = 0

0010 0110 //x
&
0010 0010 //y
--------------
0010 0010 //z

so , you see that the 3rd bit is 0 while rest is remained unchanged.

mrnutty 761 Senior Poster

Are you asking me to explain the article to you?

No, I mean what was with the quote to my response?

mrnutty 761 Senior Poster

Hint1 : use a for loop.
Hint2 : if your up to it, use recursion.
Hint3 : Google oggle it

mrnutty 761 Senior Poster

adds and subtracts for doing encryption and decryption

If your program justs shifted character x units to the right for encryption then you need the same translation to the left for decryption

You cant use 2 different keys for simple shifting encrypt/decrypt

Unless for the decrypt, you have a formula that converts a
different key to the same key as the encrypt key.

mrnutty 761 Senior Poster

come on. i want find the answer

The answer is within you my child. Just unleash it!

Salem commented: Ah yes grasshopper, to know the path is to know the way +36
tux4life commented: Indeed! +21
mrnutty 761 Senior Poster

See: http://www.codeguru.com/cpp/tic/tic0163.shtml

whats that supposed to mean?

mrnutty 761 Senior Poster

how about a pure virtual destructor.

Also gotW

mrnutty 761 Senior Poster

In c++ , recursively, not tested.

void printSeq(int num)
{
    cout<<num<<" ";
    if(num == 1 ) return;
  
   if( num & 1) //if odd
         num = num*3+1;

   else num /=2;

   printSeq(num);
}
mrnutty 761 Senior Poster

Don't mess with Narue. Look at her avatar, you will get some idea :sweat:

Yea I see the anime avatar. Its cute.