mrnutty 761 Senior Poster

" I was using the class in a program with a self-written form of Vector, and it kept crashing upon destruction of the object."

Mind showing your constructor and destructor?

mrnutty 761 Senior Poster

The proper use of delete and delete []

int *Num = new int;
int *Array = new int[5];

delete Num; //good
delete [] Num; //Bad -- undefined 
delete [] Array; //good
delete Array; //bad --undefined

As you can see, you have :

Category* pCat = new Category;

Which should follow up with a

delete pCat;

You are using an array notation bracket, i.e [] on pCat, and that
is probably why it lead you to think you should use delete [] pCat,
but in fact, if you know abut pointers, pCat[0] is the same as *pCat.

Also (1) pCat[0].GetType() is the same as (2) (*pCat).GetType and (3) pCat->GetType.
Use the option (3), as it is more clear, that pCat is not an array of a class object and "prettier."

mrnutty 761 Senior Poster

If you're curious, test it on your own computer :P
I just did, and it did exactly what I said it would.

Not really a good idea.

mrnutty 761 Senior Poster

Get started.

int main()
{
    //Create array
    //use a for loop to ask user for input
    // for i from 0 to array size   cin >> myArray[i];
   //create a sum,average, high and low variable;
   //Find sum first, initialize it to 0 first;
   //use a for loop from i = 0 to array size and have sum += myArray[i]
   //Next  find average. Average is Sum divided by array size
  //Go on from there and see what happens.
  return 0;
}
mrnutty 761 Senior Poster

Yes. In fact thats very common, using 1d array as a mimic of a 2d.

final int ROW = 5;
final int COL  = 5

char[] Letters = new char[ ROW * COL];

for(int i = 0; i < ROW * COL; i++)
    Letters[i] = (char)( 'A' + i );

for(int i = 0; i < ROW; i++)
{
   for(int j = 0; j < COL; j++)
   {
       System.out.print( Letters[j + i * ROW] + " " );
   }
}

It hasn't been tested, but you should get the idea, if it doesn't work.

mrnutty 761 Senior Poster

Show more code.

mrnutty 761 Senior Poster

It seems like you are trying to make a code to send somebody a
harmful .exe.

Therefore I will not tell you.

If i am wrong then say so, trusting you not to lie.

tomtetlaw commented: Good Thinking :) +0
mrnutty 761 Senior Poster

for a factorial program, its always best to create a recursive factorial method that returns the answer to the factorial..

Uhh, Nope. Where would you get that idea? Recursive functions are expensive. It should be only used when it simplifies a lot of complexity,
and has reasonable performance.

mrnutty 761 Senior Poster
for(int i = 1; i <= 10; i++)
{
    for(int j = 1; i <= i; j++)
          System.print(j);

   System.println();

}
jasimp commented: where's the explanation? -2
mrnutty 761 Senior Poster

I think you might be thinking about a wrapper.

It is a function that wraps another function in a way that it makes it
easier to use for the users.

mrnutty 761 Senior Poster

There are basically the same except for their accuracy.

A float is for example the following :

float F = 3.1415f;

And a double is the following :

double D = 3.1415;

As you can see they are similar, the only difference comes when
accuracy is in play. A double can handle a lot more accurate digits
than a float can.

Usually, a float is good enough to use, than of a type double.
If you don't care about accuracy that much, for example, if
PI = 3.1415972f, is enough accuracy then a double would not be needed. A double will consume more memory than a float will.

mrnutty 761 Senior Poster

you are just changing the string ... which is not the member variable.. itself and printing ... its funny... :P

Well the idea was to use the string, inside the struct as if it was
a member.

mrnutty 761 Senior Poster

why are you confusing the "=" with "==" ?

Do you know what the difference is ?

"=" is called the assignment operator. It assigns a value to a variable.
"==" compares two comparable object/data types

In your for loop you have " i == words.size()". This is a bug,
since " i " will go one passed the end and evaluate the loop.

Change this :

for (unsigned int i = 0; i == words.size(); ++i)		
		if(words[i] == words[i+1]) //check next word if they match?		
		 cout << "bleep" << endl; //if so, bleep!

to

for (unsigned int i = 0; i < words.size() - 1; ++i)		
		if(words[i] == words[i+1]) //check next word if they match?		
		 cout << "bleep" << endl; //if so, bleep!

The new things there is i < words.size() - 1; Its words.size() - 1 because
in your loop you are checking " i " to " i + 1". Thus if I was at the
last element, it would compare the last element, to one passed the end
which is junk.

mrnutty 761 Senior Poster

1) Use code tags.

2) This is incorrect :

for (int row = 0; row > maxRow; row++)

It should be :

for (int row = 0; row [b]<[/b] maxRow; row++)

You have that mistake in other places, such as both of your constructors. Fix it, and see what happens.

mrnutty 761 Senior Poster

Why ?

Maybe something like this :

#include <iostream>
#include <string> 
 
using std::cout;
using std::string;
using std::endl;

namespace Curious
{
	string mystery = "Magic";

	struct Wonder
	{
		Wonder(string msg) { mystery = msg; }

	};
}
int main()
{  
	using namespace Curious;

	cout<<mystery<<endl;
	Wonder why("What is going on ? ");
	cout << mystery << endl;
 

	return 0;
}
mrnutty 761 Senior Poster

That takes too much processing and CPU time. Just use the win32 api function Sleep(int seconds), and don't forget to include windows.h header file.

Thanks for the tip. What about portability?

mrnutty 761 Senior Poster

Then we will have to disagree until the OP clarifies his question. I think he was asking for realistic uses of recursion, not contrived ones.

Agreed. To me, OP's question, seems to say one thing, and then
another.

mrnutty 761 Senior Poster

sorry i forgot to post my code

#include <stdio.h>

int main() {

int i ;
int c;

while ( ( i = getchar() ) != EOF )
c++ ;

printf( "%d characters\n" , c) ;

return 0;
}

does this seem ok? ... when i run it ./a.out i click enter
then i enter a word say "daniweb" and press enter and then hit ctrl+d and it says 8 characters

where is c initialized ?

tux4life commented: Tada! The actual thread solver. +24
mrnutty 761 Senior Poster

All of these are pretty bad examples of recursion

Its not supposed to be a good example. Usually, good examples aren't
for beginners. Its supposed to show the concept of recursion,
which is what the OP asked for. I don't think there is a point in
giving a lecture about recursion, and where it could help at this
stage of the OP.

But at the least, the OP should know that most of what recursion can
do, can be done in loops. And most beginner example he will see,
is not worth it. But, still examine the concept, which is what you
are after right now.

mrnutty 761 Senior Poster

Here you go :

#include <iostream>
#include <ctime> //for clock()
 
using std::cout;

void Delay(float milliSeconds)
{
	const float clk_strt = float(clock());
	
	while( clock() - clk_strt  < milliSeconds )
		continue;
}
int main()
{  
	for(int i = 0; i < 10; i++)
	{		
		Delay(1000); //1000 milli seconds = 1 seconds
		cout<< "Time : " << (i+1) << " seconds\n";
	}

	return 0;
}
mrnutty 761 Senior Poster

use the = assignment operator, not the == boolean operator.

Its getting late,here.

mrnutty 761 Senior Poster

I truly do not understand what you just wrote. I'm sorry

void getFact(long N); //assume already created

I am confused where "void" came from and all the code you see in my first post is all that I have written

for(int i = 0; i < MAX; i++)
      Array[i] = getFact(i);

and the "MAX" where did that come from? I do understand the for loop (we just went over that last week)

and I don't understand where "Array" came from

and This problem is for my 100-level class--my teacher must be insane--none of us have prior programming knowledge and expects us to know arrays like the back of our hand

I'm sorry my mistake, it should have been int getFact(int N);

Max is just any number. I just put it there so, as an arbitrary value.
Its not meant to be a full code, just enough to give you a hint.

int getFact(int N)
{
  int Tot = 1;
  while(N > 0 )
  { 
      Tot *= N; 
      N--;
  }
  return Tot;
}

somewhere in your main :

final int MAX = 5;
int Array[MAX] = new int[MAX];
for(int i = 0; i < MAX; i++){
    Array[i] = getFact(i);

//Print out Array
}

Is that what you were looking for?

mrnutty 761 Senior Poster

From the google God himself.

mrnutty 761 Senior Poster
vector<string> words();
	words[0] == "roller coasters";
	words[1] == "waiting";
	words[2] == "people who talk on the phone while driving";
	words[3] == "wars";

You haven't allocated any memory for the vector.

Try this :

vector<string> words(4); //Create 4 elements for now
	words[0] == "roller coasters";
	words[1] == "waiting";
	words[2] == "people who talk on the phone while driving";
	words[3] == "wars";
mrnutty 761 Senior Poster

create function;

void getFact(long N);  //assume already created

for(int i = 0; i < MAX; i++)
      Array[i] = getFact(i);
mrnutty 761 Senior Poster

Thank you; ^ and ^^ for your prompt reply :)

I have another problem similar problem; i.e.

now ONCE i have declared a char array of a particular size; if I add more values into other indexes of it; it stores them; its like the char array is EXPANDING automatically; while in int array; it gives an error !! Why does this happen ?

Bottom line, array does not resize itself. Your problem has to
do with memory. Your code has a bug; You declared char arraY[1],
but in your for loop you are looping it from 0 to 2, which has 3 index.
Again, arrays does not resize its self, nor does it check for bound
errors. Don't worry about whats happening in the memory right
now. Just remember that when you have a code similar to this :

data_type  dataVariableArray[ someConstantNumber ] ;

That array has elements from 0 to someConstantNumber - 1,
anything else then its invalid.

mrnutty 761 Senior Poster
//For positiver integers thats within range of int
int RecursiverMult( int x, int y)
{
    //if y is less than 1 then return 0
   //else return x plus RecursiveMult(x,y-1);
}
mrnutty 761 Senior Poster

Do I use std::sort like this

std::sort(letters[j]);

Just to answer your question :

const int SZ = 10;
int Array[SZ] = {1,6,-2,0,5,54,71,24,70,31 };
std::sort(Array, Array + SZ);

Note you need to include <algorithms>

mrnutty 761 Senior Poster

Here is a recursion that prints a message n number of time. Its not been
tested.

void PrintUsingRecursion(string msg, int howMany)
{  
     if(howMany <= 0) return; //BASE CASE
    
      cout<<msg<<endl;
      PrintUsingRecusion(msg,howMany-1);
    
}
mrnutty 761 Senior Poster

[TEX]Hello please how can i get a pointer value that is in for loop???[/TEX]

[TEX]Why are you talking like this?[/TEX]

mrnutty 761 Senior Poster
#include<iostream>
using namespace std;

int main()

{
    char name[10]= ""; // empty string
    int counter = -1;
    
	while (strcmp(name, "stop") != 0)
	{
		cout<<"Enter your friend's name (""stop"" to quit): ";
		cin>>name;
		counter++;
	}
	if (counter == 0)
	{
		cout<<"Go Make some friends you emotional loser!\n";
	}
	else
	{
		cout << "Congratulations you have " << counter << " friends!!\n";
	}
	system("pause");
	return 0;
}

My list of question :

1) WHY ?
2) What happens when a name is longer than 9 characters ?
3) Is it easier ?
4) Less code ?
5) Better looking ?
6) more understandable ?
7) Less prone to bugs ?

Additional details :

It needs to use <cstring> library for it to be able to use strcmp(...).

This code :

if(counter == 0)

is the same as

if(!counter);

So, there is no need to use the boolean compare operator when
a "not" can do the job.

mrnutty 761 Senior Poster

You should google, " sorting arrays".

There are many algorithms for this task.

Or you can let std::sort do the job.

mrnutty 761 Senior Poster

Why not :

bool isEmpty() { return ( position.size == 0 ); }
mrnutty 761 Senior Poster

You could make the search-algorithm yourself (and that would be fun), but you can also save yourself alot of trouble and let std::sort do it for you.
- load all numbers in a vector with two loops
- std::sort them
- print them out
- done

for example:

#include <iostream>
#include <vector>
#include <algorithm>

int main(){
    int a[2] = {4,8};
    int b[4] = {6,2,1,6};

    std::vector<int> all_numbers;
    for (unsigned i = 0; i < 2; ++i) all_numbers.push_back(a[i]);
    for (unsigned i = 0; i < 4; ++i) all_numbers.push_back(b[i]);

    std::sort(all_numbers.begin(), all_numbers.end());

    for (unsigned j = 0; j < all_numbers.size(); ++j) std::cout << all_numbers.at(j);
}

You can get rid of one of the loops.

std::vector<int> all_numbers(a,a+2);
    for (unsigned i = 0; i < 4; ++i) 
          all_numbers.push_back(b[i]);
mrnutty 761 Senior Poster

another way :

char RanChar = char('A' + rand()%26)
mrnutty 761 Senior Poster

Something like this :

vector<short> cntr(10,0); // 0 through 10 elements initialize to 0
vector<short> value(10,0); // same as above;

for(int i = 0; i < value.size(); i++)
    cin >> value[i];

//then you know the value is between the  ranges of 0 to 9, and realize
//that when user enter a number it will match the index, so.

for(int i = 0; i < value.size(); i++)
     cntr[ value[i] ] += 1;

//Then print it out.
mrnutty 761 Senior Poster

Uh good. But I think you realised your limitation.
For sorting integers, there are many algorithm available.
But before that, you need to build a common program to try those. This program includes code to input N numbers into an array. Then it sorts the array using one of the algorithm and finally prints the result( that is the sorted array itself).
So go and try each of the algorithm and post back if you find any difficulties.

Tip: start out with insertion sort.

I'm not sure He knows about arrays yet.

@OP this code :

//declare variables
	bool Number_1 = 0;
	bool Number_2 = 0;
	bool Number_3 = 0;

Type bool should be changed to int at least.
bool can either be 0 or 1.
int can be anyNumber within its range, −32,768 to +32,767

@ OP this code, and similar :

if (Number_1 <= Number_2 <= Number_3)

Is not doing what you think its doing.
The compiler first evaluates the first term , Number_1 <= Number_2.
This return either 0 or 1, or true or false.
The the true of false is evaluated with the right hand side.
0 <= Number_3 or 1 <= Number_3 which is the same as
false <= Number_3 or true <= Number_3.

As you can see its not comparing the number2 to number 3,
rather, its comparing the result from number_1<=number_2.

mrnutty 761 Senior Poster

>Its c++, is there anything cute about it?
Yes. I find std::strings , std::vectors very cute. I have a less of headache when I use these (provided I am not concerned so much about the program speed).

Oh, so you like the followings :

std::vector<vector<float>> vec2d; //might be compiler error to some
vec2d.resize(100,vector<float>(100,0)); // resizes it to 100 x 100

And string in its beautified form is alright, but its true form is ugly,
and definitely not cute?

Or you can typedef everything, and add more complexity to your code

And no matter how cute it might look, one can always make
it much more complex with c++ syntax, particularly n00bs.

come on, c++ is not cute, its simply a Beast

mrnutty 761 Senior Poster

Think in similar manner. Its not complete.

#include<iostream>

using namespace std;
 
int main()
{ 	
	int X = 0;
	int Y = 0;
	int Z = 0;

	cin >> X >> Y >> Z;

	if(X < Y)   
	{
		if( X < Z ) 
		{
			cout << X << " ";
			if( Z < Y)
			{
				cout << Z << " ";
				cout << Y << " ";
			}
			else 
			{
				cout << Y << " ";
				cout << Z << " ";
			}
		}

		else{
			cout << Z << " ";
			cout << X << " ";
			cout << Y << " ";
		}
		
	}


	
	return 0;

}
mrnutty 761 Senior Poster

To my knowledge you can't specify what to inherit and what not. You either inherit the whole shebang or you don't inherit anything. I always am receptive to being shown wrong, and thereby learn in the process, however.

You know I wasn't 100% sure. Then I tried it. Tell me what you think :

#include<iostream>
#include<sstream>

using namespace std;

class A
{
private :
	int aVar;

public:
	
	static void attack() { cout <<"Rarr\n"; }	

	int run() { return 0; }

	struct A_o 
	{
		int x;
		A_o() { x = 0; }	
		void Roar(){ A::attack(); }
	};
};

class B : public A::A_o
{
public:
	void Roar(){
		this->A_o::Roar(); 
	}
	
	//Error below
	//void flee() { A::run(); };
};

int main()
{ 	
	B classB;

	classB.Roar();

	return 0;

}

Its inheriting A_o( part of A) and not the whole class A, if that
is what you mean.

mrnutty 761 Senior Poster

You know using sstream is a lot easier :

#include<iostream>
#include<sstream>

using namespace std;

int main()
{ 
	
	
	int NumberToReverse = 123456;
	
	string str = "";

	stringstream sstrm;

	sstrm << NumberToReverse;

	sstrm >> str; //str = "123456";

	int i = str.length() - 1;
	
	cout<<NumberToReverse <<" reversed is : ";
	
	for( i; i  >= 0; --i)
		cout<<str[i];
	
	cout<<endl;


	return 0;

}
mrnutty 761 Senior Poster

how about you give it a go, and see what happens. Try your best first.
Think about this in small steps.

mrnutty 761 Senior Poster

The bottom line is: cin, by default do not handle invalid input so cutely.

Its c++, is there anything cute about it?

mrnutty 761 Senior Poster

Create two int variable.
Ask user for input
Check if input is valid
if so the proceed else go back to get user input
start a for loop from i = 0; i < L;
start another loop ( nested loop ) that ranges from j = 0; k < H
print out a symbol, in your case its an "*";
after the second nested loop is done, print a new line character to start
the new process in a different line.

Then you are done.

mrnutty 761 Senior Poster

sort the array.
And from there see which value occurs the most.

mrnutty 761 Senior Poster

if( variable is equal to a Number )
the do this code;

Translation :

if( temp == -173 ) 
   //do stuff

what you are doing is this :

if ( assign a number to temp )
//then do this

Translation :

if( temp = 100) //do stuff;

And generally, when you have more than 2 if-else, in which it could be replaced by a switch statement, then use
a switch statement.

const int BOILING_POINT  = 100;
const int FREEZING_POINT = -100;
const int EQUILIBRIUM_POINT = 0;

cout<<"Enter a temperature : ";
int tmpInDegrees = 0;
cin >> tmpInDegrees;

switch(tmpInDegrees)
{
  case BOILING_POINT :   cout<<"Boiling point \n"; break;
  case FREEZING_POINT : cout<<"Freezing point\n"; break;
  case EQUILIBRIUM_POINT  : cout <<" Equilibrium point\n"; break;
  default : cout<<"Nothing here for that temperature\n"; break;
}
mrnutty 761 Senior Poster

hello......sir and madam..........
i m new for this site so please help and tell me how i can share my problem with you......
sorry for waste your time .
thanks

To share your problem, just post the problem. Ask specific question.
Maybe google it before you ask question, in which you might find the
answer.

mrnutty 761 Senior Poster

Something like this ? :

#include <iostream>
#include <vector>
#include <string>

using std::string;
using std::cout;
using std::endl;
using std::cin;

class RationalNumbers
{
private:
	float Numerator ;
	float Denominator ;
public :
	RationalNumbers() { }

	template<typename T>
	RationalNumbers(const T& RationalNumbers);
};
template<typename T>
RationalNumbers::RationalNumbers(const T& var){
	cout<<typeid(T).name()<<" specialized\n";
}
template<>
RationalNumbers::RationalNumbers(const int& var){
	cout<<"Int specialized\n";
}
template<>
RationalNumbers::RationalNumbers(const float& var){
	cout<<"Float specialized\n";
}
int main()
{ 
	 		
	float A = 4.2f;
	int B = 2;
	char C = 'a';
	string str;

	RationalNumbers R1(A);
	RationalNumbers R2(B);
	RationalNumbers R3(C);
	RationalNumbers R4(str);

	return 0;

}

denominator

mrnutty 761 Senior Poster

Its because you are not overriding the assignment operator. The virtual
function needs to have the same signature, for a derived class of that of
a ABC class or a base class.

Just replace your pony operator with this :

void operator=(const Horse& rHorse) {itsAge = rHorse.GetAge(); std::cout<<"Pony operator= ..."<<std::endl;}
mrnutty 761 Senior Poster

Haven't looked at your code but generally :

//setter function
void alterVariableX(data_type x_o)  { X = x_o; }
//alter/mutator function
data_type getVariableX() { return X; }