mrnutty 761 Senior Poster

This should help :

string messageToYou(" try it out first");
string vowels("aeiouyw"); //whatever you think should be considered a vowel

size_t upTo = vowels.size();

for int i = 0 to UpTo, increment i
{
    if(message.find(vowels[i] != string::npos)
        //do something
  else //do something
}
mrnutty 761 Senior Poster

First you need to understand the error , read here

mrnutty 761 Senior Poster

Adjust by using glTranslatef, to the whole screen.

mrnutty 761 Senior Poster

For 2d use gluOrtho2d(...);

With 3d you will just have to adjust.

mrnutty 761 Senior Poster

what are you trying to do here :

int nuCoins = 50 - (number || randNo);

Do you really mean that nuCoins will only be 49 or 50, provided that both contain some value ( which is your problem by the way, as pointed out ).

mrnutty 761 Senior Poster

>>Is there any way to stop the axis from rotating but allowing the object to translate or is there a trick to get around this.

Yes, use push and pop matrix for that, as well as return the original coordinate back in place , see below :

//save current matrix for only the code between the push/pop
glPushMatrix();
//rotation code here
glPopMatrix();

And when you want to reset coordinates, after translation to an object,

glTranslatef(x,y,z); //moves object to a certain coordinates
//blah blah
glTranslatef(-x,-y,-z); //resets the coordinate back to its regular position, thus the blah blah has its own relative positioning.
mrnutty 761 Senior Poster

cough* look in code snippet section *cough,cough*

mrnutty 761 Senior Poster

Do you know what a global variable is ?
Do you know what a local variable is ?

If so then read on.

Static variable is a mix between the two. Static variable has the same
lifetime as global, while its scope could be contained like a local variable.
Re-read William's classic total example, while thinking about the
statement above.

mrnutty 761 Senior Poster

what exactly are you trying to do?

mrnutty 761 Senior Poster

>>Hi, from my understanding, if you initialize an object without the "new", it will be only saved in the stack.
Correct, otherwise it would be saved in heap

>>If the method exits, the object is destroyed. Is that correct? What about this code?

Ok lets see,

int main() {
      ///xxx is declared in stack
     //its scope, i.e its lifetime is when the program reaches the 
    //end of this function
	string xxx = getText();  //Look at the function's comment
  
     //thus xxx gets a copy of the string text in getText().
	cout << "from main() " << xxx << endl;
}
 
string getText() {
     //text is declared on stack
    //its lifetime or scope is when the program reaches the 
    //end of this function
	string text("Hello World!"); 
	cout << "from getText() " << text << endl;
	return text; //returns a COPY of text
}

>>In getText() method, I created a string without the "new", so I assumed that the string will be destroyed after the return.
Yep, its destructor is called.

>> But why does xxx in main() equal to the return of the getText() method? Should it be that xxx contain an unknown value since the object it points to was destroyed? Thanks a lot!

What happens with your functions is that getTex() is called first,
the inside that function you create a string object, and do some
stuff and at last you return the string object. When you return the string object, …

mrnutty 761 Senior Poster
#include <iostream>
 
int main(void) {
char c, d=10;
while( std::cin.get(c) &&  //while cin does not fail to get input
        (c!='2' || d!='4') &&  //while c != '2' or d != '4'
        std::cout.put(d)  //while cout does not fail to write
        )
       d=c; //save last input
}

so basically when the user inputs a number , it gets saved into d,
and when the user input another number, it checks if that number is 2 and if the previously saved number was 4. if so then it quite, otherwise it continues.

mrnutty 761 Senior Poster

To answer you question you can do this :

int main()
{
   int * largeInputArray = new int[1000000];

    //use array here, ex, largeInputArray[0] = 12312;

  //after done using largeInputArray
  delete [] largeInputArray;

 return 0;
}
mrnutty 761 Senior Poster

Hi this is the code for life, universe and everything problem, where the program stops only when the user inputs the number "42"

I tried to understand the problem, but could not make it that how the program stops at exactly 42. Any help would be extremely appreciated.

#include <iostream>
 
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

Forget that "ugly code".

This does the same and is more readable :

#include<iostream>

int main()
{
  //enable less qualified name
   using std::cout;
   using std::cin;
   using std::endl;

   int terminateNumber = 42;
   int inputNumber = -1;

  while( inputNumber != terminateNumber )
  {
      cout <<" Enter a number : " ;
      cin >> inputNumber;
      cout<<"\nYour number is : " << inputNumber << endl;
      cout << endl;
   }
  
   return 0;
}

It hasn't been compiled though.

mrnutty 761 Senior Poster

How about you let std do that for you?
Link

mrnutty 761 Senior Poster

Im sorry im not sure what your edit exactly means

Put the definition in your Hastable with the class.

//Idea works

#ifndef FOO_H_
#define FOO_H_

template<class T>
class Foo {
public : 
 Foo() { cout <<" Foo ctor\n"; }
 void fooIt();
};

template<typename T>
void Foo<T>::fooIt() { cout << " Fooing it now\n"; }
#endif
//main.cpp
#include"foo.h"
int main() { return 0; }

//Idea does not works

#ifndef FOO_H_
#define FOO_H_

template<class T>
class Foo {
public : 
 Foo() { cout <<" Foo ctor\n"; }
 void fooIt();
};
#endif
//main.cpp
#include"Foo.h"

template<typename T>
void Foo<T>::fooIt() { cout << " Fooing it now\n"; }

int main() { return 0; }
mrnutty 761 Senior Poster

This should get you started.

void printEvenAndOddSeperately(vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
     {
             if( v[i] % 2 == 0) cout << v[i] << " is even\n";
             else cout << v[i] << " is odd\n";
      }
}
mrnutty 761 Senior Poster

>>1. text pre-processor ( convert .doc to .txt)
When you load in a doc file, you will also load in its formatting and other extra stuff. You need to weed that out.

>>2. Sentence separator
A sentence ends with a period(.), so use that as your end case with strings,

>>3. Word separator
Words are seperated by space, so use that as your end case with strings.
>>4. Stop word eliminator
Need to be more specific.

>>5. Word frequency calculator
Get a word, count its frequency. Just dive in and see what happens.
Also google, and use the search on this forum for "frequency counter"

>>6. Scoring algorithms
What do you mean by this?

7. Ranking algorithms
Relative to what?

mrnutty 761 Senior Poster

>>i cant understand this code plz help me is iit right or not

You tell me. Try it and report back.

mrnutty 761 Senior Poster

Not tested.

bool split_string(string& src, string& dst, char splitter)
{ 
   //check for valid size or if splitter is in src
    if(src.size() == 0 || src.find(splitter) == string::npos)
        return false;
    int i = 0;
    while(src[i] != splitter)
           dst += src[i++];

  return true;
}
mrnutty 761 Senior Poster

I'll make it for you for a mere million dollar, what do you think?

mrnutty 761 Senior Poster

Did you read my edit?

mrnutty 761 Senior Poster

I need help with IT210 week 7 programming problems 1 and 2 pseudocode can any one give some advice?

Realize that we are not in the same class let alone the same school,
most of us any ways, so we don't know what you are talking about.

Imagine someone said that to you but from a different state and different class with different problem.

mrnutty 761 Senior Poster

You need to put the definition and the declaration of a template class
in the same file.

mrnutty 761 Senior Poster

Problem :

Write a C++ program that asks the user to enter 5 large values of n ( > 10), calculates their factorial using sterling’s formula and displays the results in a tabular format.

Do what you can first :

>>Write a C++ program that asks the user to enter 5 large values of n ( > 10).

Can you do that ?

>>calculates their factorial using sterling’s formula and displays the results in a tabular format.

Can you make a function and try to do that ?

Take the sterling function apart, and see what you get.

mrnutty 761 Senior Poster

maybe you want :

HashTable<string> = new HashTable<string>(a, 100);

[edit]
Oh, you need to provide the template class and its definition in the same file.
[/edit]

mrnutty 761 Senior Poster

Opps, thanks Csurfer, its was late when I posted, I think.

@waltP, what do you mean, guessNumber never changes?

if( quit == false ) { guessNumber = rand()%100 + 1; }

When the game is near its end it changes if player wan'ts to play again.

Also the guessNumber should have been a parameter passed into
PlayGame function, sorry.

Thats what I get for staying up late.

mrnutty 761 Senior Poster

>>But specialMath(1.0, &sin , &cos); is not a single variable function,
I cannot put in the Integration:

Why not make a wrapper for it then, or use bind*

mrnutty 761 Senior Poster

This sounded interesting so I made a class that changes its type,
simulates it at least.

I know there are some bugs, but its getting late ( 3 am). Anyways,
it could do the basic things a primitive datatype can to for now,
that is add,subtract,multiply,divide. It can also be used with
cin/cout, or ostream/istream;

Anyways here you are. You can disregard its implementation, and just use it.

I have an example below :

#include <iostream>
#include <sstream>
#include <string>

using namespace std;


namespace Type
{		
	using namespace std;

	class Variable
	{
		string  var;
		ostream& strm;
	public:		
		Variable() : strm(cout), var(""){ }
		template<typename T >
		Variable(T a, std::ostream& os = cout) : strm(os),var("") {
			var = this->changeType( a ); 
		}
		template<typename U>		
		string changeType(const U val ){
			stringstream converter;			
			converter << val;	
			return converter.str();
		}	
		//operator overloading	
		template<typename T>
		Variable operator+(const T a)
		{			
			stringstream ss;			
			T d;
			ss << var;
			ss >> d;
			ss.clear();
			ss.str("");
			T r = d + a;
			ss << r;			 
			Variable tmp(ss.str());
			return tmp;

		}

		Variable operator+(const Variable& v){
			stringstream ss;
			double d;
			ss << v.var;
			ss >> d;
			return operator+(d);
		}
		template<typename T>
		void operator+=(const T a){				
			string tmp = "";
			tmp = this->operator +(a); 
			var = tmp;
		}
		template<typename T>
		Variable operator-(const T a){			
			return this->operator +(-a);
		}	
		Variable operator-(const Variable v){
			stringstream ss;
			double d = 0.0;
			ss << v;
			ss >> d;
			return this->operator +(-d);
		}
		Variable operator-(const string s) {
			Variable tmp(s);
mrnutty 761 Senior Poster

>>Why not (void*) FunctionType?
or void* FunctionType?

Otherwise it won't be a function pointer

The above statement, (void*) FunctionType , or void* FunctionType?
are the same.

void (*pF) ();
means that pf is a void pointer function. If the parenthesis wasn't there
then it would be a regular function that returns a void pointer.

mrnutty 761 Senior Poster

Thanks for replying .. I already did that .. I was looking for a sort of special way

Ahh but there is, you just have to create it. Here is an example :

#include<iostream>
#include<string>

using namespace std;

void SquareString(string str)
{
	int len = str.length();

	int max = len * 2;

	if(max < 3) max = 3;

	for(int i = 0; i < max; i++)
		cout<<"*";

	cout<<endl;

	cout<<"*";

	int Lim = max/4 - 1;

	for(int i = 0; i < Lim; i++)
		cout<<" ";
	
	cout<<str;

	if(str.size() % 2 == 1)
		Lim++;

	for(int i = 0; i < Lim; i++)
		cout<<" ";
	
	cout<<"*"<<endl;	
	

	for(int i = 0; i < max; i++)
		cout<<"*";
}
int main()
{
		
	for(int i = 0; i < 26; i++)
	{				
		string str =  "";
		str += char ( 'A' + i);

		for(int j = 1; j < i+1; j++)
			str += str[0];

		SquareString(str);
		cout<<"\n\n";
	}
	return 0;
}
mrnutty 761 Senior Poster

1) This is not a code snippet, so why are you posting it as one.
2) cin >> extract only disregards white spaces so when the input is
john kane, for your "c" variable, it only contains john.

3) Use getline.

mrnutty 761 Senior Poster

easy way :

you can wrap another bool parameter.

double integrate( pointer_func Pf, int low, int high, bool flag, bool doFlag)
{
    if( doFlag == true) { if( flag == true) { /* something */ } }
  else //blah blah
}
mrnutty 761 Senior Poster

Aha .. I got that ..

what if I wanted the square to surround some writing
.

cout<<"*******************\n";
cout<<"*  Writing stuff  *\n";
cout<<"*******************\n";
mrnutty 761 Senior Poster

try posting your whole code

mrnutty 761 Senior Poster

what is your program supposed to do?

mrnutty 761 Senior Poster

Haven't tested it yet, but it could be similar to this :

int M = 5;
int i = 1;
while(i <= M) cout << i++ <<" ";  //print from 1 to 5
while(--i > 0) cout << i << " "; //print from 4 to 1
mrnutty 761 Senior Poster

I think you want it to be this :

static float calcTaxes(float grossPay)
{
float netPay ; 

if (grossPay <= 300)
netPay = (float) (grossPay - (grossPay * 0.15));

if (grossPay <= 450)
netPay = (float)(grossPay - (grossPay * 0.20));

else if (grossPay > 450)
netPay = (float)(grossPay - (grossPay * 0.25));

return netPay
}

You forgot to set the return type of the function as well as returning a value.

mrnutty 761 Senior Poster

suggestion : if you are a beginner, then forget about graphics stuff.
You need to learn a language well before you can move on.

suggestion : You might want to use Java. There are plenty of GUI that wraps java.awt among other things in java, where it lets you draw shapes.

mrnutty 761 Senior Poster

>>I would then assume that
>>vector< data_type > copyRow( vec2d[3].begin(), vec2d[3].end());
>>copies the 4 row???
No it copies the 4th row. That is, it copies everything from vec[3][0] to vec[3][ end], where end is the las element in the 4th row.


>> What if I need to copy a column?
Here is an example :

#include<iostream>
#include<vector>

using namespace std;

int main()
{
	
	vector< vector< int > > twoD(5, vector<int> (5,0));
	typedef unsigned int uInt;

	//populate
	for(uInt i = 0; i < twoD.size(); i++)
		for(uInt j = 0; j < twoD[i].size(); j++)
			twoD[i][j] = j + i*twoD.size();

	//Print for testing
	for(uInt i = 0; i < twoD.size(); i++)
	{
		for(uInt j = 0; j < twoD[i].size(); j++)
		{			
			cout<<twoD[i][j]<<" ";
		}
		cout<<endl;
	}

	cout<<"\n\n\n Copying column part\n";

	//create enough space to copy column of the 2D vector
	vector<int> columnCopy(twoD.size(),0);
	
	for(uInt i = 0; i < twoD.size(); i++)
		columnCopy[i] = twoD[i][0];

	for(uInt i = 0; i < columnCopy.size(); i++)
		cout<<columnCopy[i]<<" ";

	return 0;
}
mrnutty 761 Senior Poster

>> this will convert 123.0 to "123.0" and that's not what I want
>>(first post) I would like to know what's the way to convert a double to its char[8] (even better string)

What else do you mean. stringstream can convert between double to string and string to double as well as other things.

mrnutty 761 Senior Poster

>> But Plus and Minus are predefined functions, like sin or cos,
but how can i retun with sin+cos or sin+1?

You will have to create a composite functions.

typedef float(*p_f)(float);

//way 1
float composite(float arg){
return sin( cos( arg) );
}
float composite2( float arg, p_f f1 = cos, p_f f2 = sin ) {
   return (*f1)( (*f2)(arg) );
}

p_f retriveSomeFunc(){
  return  &composite;
  //or return &composite2;

composite 1 is straight cos and sin, composite 2 has default cos and sin but you can pass it functions later on if you
decide to change.

maybe something like this :

p_f  specialMath = retriceSomeFunc(); //say it returned composite 2

//if specialMath is pointer to the composite 2 function then we can do this :
specialMath(1.0); // cos( sin ( 1) );
specialMath(1.0, &sin , &cos); // sin( cos( 1) );

 //dangerous because sqrt is not defined for negative numbers, because log can produce negative numbers
specialMath(1.0, sqrt, log ); // sqrt( log( 1.0) )

Realize that this all is not tested, and I don't have much experience with pointer functions as I should have, so I might be misleading somewhere.

mrnutty 761 Senior Poster

make a separate function.

bool isPrime(int i) { /* logic here */ };

//somewhere inside main
if( isPrime(i)  == true ) cout <<" Prime \n" ; 
else cout <<" not a prime\n";

Of course this code depends on isPrime function. You should try to create that on your own. If you have trouble, either google or ask.

mrnutty 761 Senior Poster

Wrap everything around in a do while loop;

char quit = false;
int guessNumber = 0;
srand(time(0));
guessNumber = rand() % 100 + 1;
do
{
   PlayGame();

   cout<< "quit <1 for yes or 0 for no > : ";
   cin >> quit;
  if( quit == false ) { guessNumber = rand()%100 + 1; }

}while(quit == false)
mrnutty 761 Senior Poster
mrnutty 761 Senior Poster
//assume vec2d is a 2 d vector
vector< data_type > copyRow( vec2d[0].begin(), vec2d[0].end());

That would copy the row 0 onto copyRow vector

mrnutty 761 Senior Poster

You might want to change your destructor.

delete [] st;

instead of

delete st;

You'll be creating memory leaks if you don't.

Not memory leak, but undefined behavior.

mrnutty 761 Senior Poster

I though it was vector<type> name(lenght or size, init values);

So..typing words in the name... is that the memory direction of that array? You totally lost me on that one :S

Vectors has many constructors. See here, Link

mrnutty 761 Senior Poster

Can you be more clear.

Do you need to print numbers from 1 to 10 using do while loop?

mrnutty 761 Senior Poster

You could make your code cleaner by using functions, assumming you have learned them already.

Not tested.

#include<iostream>
#include<cmath>
using namespace std;
bool isPrime(int i)
{  
     if(i == 2)
         return true;
   for(int j = 2; j < i; j++)  //you could optimize but this is for simplicity.
         if( i % j == 0) return false;
    return true;   
}

int main()
{
   int MAX = 100;
   int i = 2;
    while( i != MAX)
    {
           if(isPrime(i) ) cout << i  << " is Prime\n";
           i++;
    }
}
mrnutty 761 Senior Poster

Realize the obvious pattern first.

4444**** //there are 4 4's and 4 *'s
333*** //there are 3 3's and 3 *'s
22**  // and so on
1*
22**
333***
4444****