mrnutty 761 Senior Poster

1) Use a string for input.
2) Check if all is numbers.
3) Output the string reverse.

mrnutty 761 Senior Poster

Are you using GUI?

mrnutty 761 Senior Poster

Pop each element. Check if there are equal matching braces/brackets...

mrnutty 761 Senior Poster

It uses the idea of Seive of Eratosthenes.

The code is basically does the following to find Prime Numbers :

1) Populate Array from 0 - > MAX
2) Find 1st Prime, which is 2
3) Delete all Multiple of 2, i.e set it to false
4) Find next prime, which is 3
5) Delete all multiple of 3
6) Repeat steps 4 - 6 until done.

I also use a little try and catch, just to provide as an example.

mrnutty 761 Senior Poster
cout.setf(std::ios_base::fixed, std::ios_base::floatfield);

See your other post. Just put this inside main, near the beginning.

mrnutty 761 Senior Poster

To have a fixed floating point decimal add this code :

cout.setf(std::ios_base::fixed,std::ios_base::floatfield);

http://www.cplusplus.com/reference/iostream/ios_base/setf/

mrnutty 761 Senior Poster

its solved. Copy Ancient dragon code. And place my code into main.
And run.

mrnutty 761 Senior Poster

Sure. Start with Ancient Dragon code. I think you are expected to use
arrays, otherwise using a for loop is pointless with for this program.

Then create 10 int variables like so :

int TenVariable[10];

use for loop to ask user to input variables;

for(int i = 0; i < 10; i++)
{  
   cout<<"Enter variable number : "<< (i+1) <<"  ";
   cin >> TenVariable[i];
   cout<<endl;
}
mrnutty 761 Senior Poster

Do you know about arrays yet?

mrnutty 761 Senior Poster

Yes its possible to cut that picture into correct portion and paste it onto
a 2d tile.

You need to first create a nxn tile set. Then, you need to generate the
correct texture coordinate for each tile.

You could calculate the texture coordinate by having 4 variables,
float bottomLeft, bottomRight, topRight, topLeft;

You need to use a nested for loop.

for(int i = 0; i < N; i++)
{
    bottomLeft = i/float(N);
    bottomRight = (i+1)/float(N);

    for(int j = 0; j < N; j++)
     {
        topRoght = (j+1)/float(N); 
        topLeft = j/float(N);

        //Draw using those texture coordinates.
     }
}
mrnutty 761 Senior Poster

You need to change the limits of min or max depending whether
the guess number is high or low.

There is 1 bug in this code. I'll let you figure that out

do
	{		
		cout<<"Is your number : "<< mid << "  ?  \n\n";
		cout<<"Too (h)igh\n";
		cout<<"Too (l)ow\n";
		cout<<"(c) orrect\n";
		cout<<"(input) : ";
		cin >> pick;

		switch(pick)
		{		   //if its too high then now max should be mid and the guess number should be from 0 to mid or min to max
		case 'h' : max = mid;
				   mid = (int)ceil((min + max)/2.0);
				   break;

		case 'l' : min = mid;
				   mid = (int)ceil((min + max)/2.0);
				   break;

		case 'c' : gameOver = true; break;
		}
		
		cout<<"\n\n";

	}while(!gameOver);
mrnutty 761 Senior Poster

With a minor change, The newton works for at least 200 digits, any more
wasn't tested.

#include <iostream>
#include<cmath>

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

typedef long double Ldouble; //Change to the highest compound data_type you have
typedef unsigned __int64 uInt64; //Change to the highes data_type range you have

uInt64 getDeg(Ldouble Num)
{
	uInt64 deg = 1;
	uInt64 T = uInt64(Num);
	while(Num > 9) { Num *=0.1; deg++; }
	return deg;
}
Ldouble guess(Ldouble num, Ldouble itr = 10)
{
	Ldouble deg = Ldouble(getDeg(num));
	Ldouble i = Ldouble( num * 0.35 );
	Ldouble sub = deg/(deg*1.5);
	while( i*i > num)
		i *= sub;

	if(deg < 10) sub = 1.1;
	else if(deg < 20) sub = 1.25;
	else sub = 1.5;

	for(; i < num; i*=sub)
	{
		if(i * i == num) return (i);
		else if(i*i > num) return (i-sub);
	}

	return (i-sub);
}
Ldouble Sqrt(Ldouble Num)
{	
	Ldouble deg = Ldouble(getDeg(Num));
	Ldouble itr = deg + 10;
	Ldouble initGuess = (guess(Num,itr));
	
	Ldouble Precision = 15;
	Ldouble root = 0.0;
	
	for( Precision; Precision; --Precision)
	{
		root = initGuess - (initGuess*initGuess - Num)/ (2.0 * initGuess);			
		initGuess = root;	
		if(root == Num) return root;
	}

	return root;
}

int main()
{
	while(1)
	{
		Ldouble P = 0;
		cin >> P;
		cout.precision(40);
		cout<<"The degree of you number is : "<<getDeg(P)<<endl;
		cout<<"Cmath : "<<sqrt(P)<<endl;
		cout<<"Mines : "<<Sqrt(P)<<endl;
		cout<<"\n\n\n";
	}

	return 0;
}
mrnutty 761 Senior Poster

total = distance / mpg * ppg + parking + tolls;

or

total = distance / ( mpg * ppg + parking + tolls ); ???

mrnutty 761 Senior Poster

Before fixing your code, you should fix your grammar.

mrnutty 761 Senior Poster

hey can u pls edit in my program bcz i m really afraid tht i might mess up d whole program...it already took me too long to write the program...thank u n i appreciate ur help

Then copy and paste it into a text file, and save it. Then start playing around with it. If you mess up too much, then just copy and paste the
original content from the text file.

mrnutty 761 Senior Poster

Example :

#include <iostream>

using namespace std;

int main()
{
   int val[4] = {1,2,3,4};

   int limit = 2;

   for(int i = 0; i < limit; i++)
     {
         cout<<"Enter value : ";
         cin >> val[i]
      }
   return 0;
}
mrnutty 761 Senior Poster

Although realize, that you can take certain courses thats not in your major.
So you can take certain courses, thats related to computers, but is not
in your major.

mrnutty 761 Senior Poster

I am guessing you are a senior in high school.

Beginning, you will take course such as :

calculus, physics, chemistry, cse101(intro to programming, language depends), and some general education class, maybe history.

The as time goes on, you will get more into the programming areas.

The school should provide you with a curriculum list, that shows which
class you have to take, in order to get a Comp.E degree.

mrnutty 761 Senior Poster

I would recommend to use

void foo(data_type * Array, int Size),
mrnutty 761 Senior Poster

I think you are looking for a general answer, but its hard to tell without
seeing the code, so post relevant code, or if small enough, the whole code.

mrnutty 761 Senior Poster

OH, I think you don't need have the user input a number. You need
to just have the computer guess a number, and the user should say
if its high,low, or correct.

In that case, you should have a function called average(int,int), that
takes 2 ints and returns its average. The two ints passed will
be your min and max.

min = 0;
max = 100;
int avg = average(min,max); //50

Now see if the average is correct. If its too low
then make you min = average, and else if its too high make your
max = avg, else its correct. From there try to figure out the rest.

mrnutty 761 Senior Poster

Can you post code. I think you have multiple definition, or non proper linking.

mrnutty 761 Senior Poster

Haven't used it for a while, but I think it takes the ascii value in int form.

if you wan't to use variable use a char variable

char print ='0';
for(print = '0'; print != '9'; print++)
  glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, print);

Also google the function, and you get a documentation, like this

mrnutty 761 Senior Poster

yes sure you can. For example you might have a reset method that
resets everything to a default constructor :

class A
{
    int c;
    public : 
   A() { c = 1000; }
    A(int x ) { c = x; }
     void reset() { *this = A(0); }     
};
mrnutty 761 Senior Poster

Use a do while loop

int winningNumber = 42;
int guessNumber = -1;
do
{
   //ask user to guess a number
  //check if its equal or not. Then determine the steps from there
}while(winningNumber != guessNumber)
mrnutty 761 Senior Poster

>Link
failbit, badbit, goodbit and stuff only makes my code difficult to read and understand.

I don't believe, so. A c++ programmer should at least know the basics of input stream, and what happens when it fails.

mrnutty 761 Senior Poster

You're thinking too hard.

string src = "hello"
for(int i = src.length()-1; i >= 0; i--)
      cout<<src[i];
mrnutty 761 Senior Poster

It takes too long because the number passes the limit of double.
If you tried another data type thats greater than double, long double
perhaps in some compiler, then it should work.
Make every data type the same, something bigger than double, if you
have it. The algorithm should work for as much as the limit of a compound datatype.

Replace the typedef with the highest amount of data_type(i.e the most bit supported) on your compiler. It should work.

#include <iostream>
#include<cmath>

using std::cout;
using std::endl;
 
typedef long double Ldouble; //Change to the highest compound data_type you have
typedef unsigned __int64 uInt64; //Change to the highes data_type range you have

uInt64 getDeg(Ldouble Num)
{
	uInt64 deg = 1;
	uInt64 T = uInt64(Num);
	while(Num > 9) { Num *=0.1; deg++; }
	return deg;
}
Ldouble guess(Ldouble num, Ldouble itr = 10)
{
	Ldouble deg = Ldouble(getDeg(num));
	Ldouble i = Ldouble( num * 0.35 );
	Ldouble sub = deg/(deg*1.5);
	while( i*i > num)
		i *= sub;

	sub = deg * 0.5;
	for(; i < num; i+=sub)
	{
		if(i * i == num) return (i);
		else if(i*i > num) return (i-1);
	}

	return (i-1);
}
Ldouble Sqrt(Ldouble Num)
{	
	Ldouble deg = Ldouble(getDeg(Num));
	Ldouble itr = deg + 10;
	Ldouble initGuess = (guess(Num,itr));
	
	Ldouble Precision = 15;
	Ldouble root = 0.0;
	
	for( Precision; Precision; --Precision)
	{
		root = initGuess - (initGuess*initGuess - Num)/ (2.0 * initGuess);			
		initGuess = root;		
	}

	return root;
}

int main()
{
	Ldouble P …
mrnutty 761 Senior Poster

Google it, then you shall find your answer.

mrnutty 761 Senior Poster
mrnutty 761 Senior Poster

It takes too long because the number passes the limit of double.
If you tried another data type thats greater than double, long double
perhaps in some compiler, then it should work.
Make every data type th same, something bigger than double, if you
have it. The algorithm should work for as much as the limit of a compound datatype.

mrnutty 761 Senior Poster

Here is something that will start you off :

#include<iostream>

using namespace std;

int main()
{
     //Create a variable for   :
    // Min number the user can input
   //Max number the user can input
  //The number of guesses
 //The winning number

 //use random number generator to assign the winning number a value
 //From range [min,max]

 //Create a do while loop
  //Ask user for input
 //Check If its '>' ,  '<' , ' ==' the winning number and print certain statements accordingly

  return 0;
}
mrnutty 761 Senior Poster

before the loop ask the user to enter a number and set min/max to
that number, then go onto the while loop.

mrnutty 761 Senior Poster

A better newton :

#include <iostream>
#include<cmath>

using std::cout;
using std::endl;
 
typedef unsigned long long Uint64;

long getDeg(double Num)
{
	long deg = 1;
	double T = Num;
	while(Num > 9) { Num *=0.1; deg++; }
	return deg;
}
Uint64 guess(double num, int itr = 10)
{
	int deg = getDeg(num);
	Uint64 i = Uint64( num * 0.35 );

	while( i*i > num) i -= deg;
	for(; i < num; i++)
	{
		if(i * i == num) return (i);
		else if(i*i > num) return (i-1);
	}

	return (i-1);
}
double Sqrt(double Num)
{	
	long deg = getDeg(Num);
	int itr = deg + 10;
	double initGuess = double(guess(Num,itr));
	
	long Precision = 15 + deg;
	double root = 0.0;
	
	for( Precision; Precision; --Precision)
	{
		root = initGuess - (initGuess*initGuess - Num)/ (2.0 * initGuess);			
		initGuess = root;
	}

	return root;
}

int main()
{
	double P = 12345678901234567890.99999999;	//Any bigger, I get compiler error
	cout.precision(10);
	cout<<"Cmath : "<<sqrt(P)<<endl;
	cout<<"Mines : "<<Sqrt(P)<<endl;

	return 0;
}
mrnutty 761 Senior Poster

Also you might want to think about abstract classs.

mrnutty 761 Senior Poster

having const after a member function means that, that function will not
and shall not change any of its variables.

mrnutty 761 Senior Poster

Dear firsPerson,

Yes after removing "unsigned" from line 20, your program is working. However, the speed of computation could be improved in comparison with other high-performance algorithms to date.

Regards,

Well I not trying to compete. I am just helping for future reference. I
believe that there are plenty of square root functions here as an
alternative to c++ sqrt(...);

mrnutty 761 Senior Poster
char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }
mrnutty 761 Senior Poster

Did you try it?

#include <iostream>

using namespace std;

class A
{
public:
	A() { }
	template<typename T>
	void Print(T a) { cout<<a<<endl; }
};
int main()
{  
	A t;
	t.Print(3.1415f); //implicitly
	t.Print<unsigned int>(100); //explicitly

	return 0;

}
mrnutty 761 Senior Poster
mrnutty 761 Senior Poster

Try hitting alt+f6. Then click ok if a window pops up.

mrnutty 761 Senior Poster

use for loop. It will be conceptually easier.

int F =5;
int R = 1;
for i = F to i >1 ; i--
{
    R equals R times F;
    F equals F minus 1;
}
mrnutty 761 Senior Poster

Your copy constructor is not complete.

my_int_vector::my_int_vector(const my_int_vector& b)
{
	numbers=new int[capacit];
	for(size_t j=0; j<siz; j++)
	{
		numbers[j]=b.numbers[j];
	}
}

What happens is numbers is already pointing at another memory location.
What happens if the copy constructor is copying its self?

mrnutty 761 Senior Poster

Yes, use template.

You could use template specialization, to make sure that the template
parameters is a number.

Here is a way to do that :

#include <iostream>

using namespace std;


template<typename Type>
struct is_numeric { static const int i = 0; };

//Template specilization
template<> struct is_numeric<short>				{ static const int i = 1; };
template<> struct is_numeric<unsigned short>	{ static const int i = 1; };
template<> struct is_numeric<int>				{ static const int i = 1; };
template<> struct is_numeric<unsigned int>		{ static const int i = 1; };
template<> struct is_numeric<long>				{ static const int i = 1; };
template<> struct is_numeric<unsigned long>		{ static const int i = 1; };
template<> struct is_numeric<float>				{ static const int i = 1; };
template<> struct is_numeric<double>			{ static const int i = 1; };
template<> struct is_numeric<long double>		{ static const int i = 1; };

template<typename Type>
class Numeric
{	
	//Check is Type is numeric
	int AssertType[is_numeric<Type>::i];

	Type * Vector_;
	unsigned int length;
public:
	Numeric()	{ Vector_ = new Type[0]; };
	~Numeric()  { delete [] Vector_; }
};

int main()
{ 
	Numeric<int> a;
	Numeric<float> b;
	Numeric<unsigned short> c;
	Numeric<string> d; //Error  
	Numeric<char> e; //Error  
	

	return 0;

}
StuXYZ commented: Nice, easy to understand template traits example +6
mrnutty 761 Senior Poster

Search the forum first. See if that helps. Then ask question if it comes
empty.

mrnutty 761 Senior Poster

1) No Code tags

2)

int opt,x,y,fact,ans; // right now all variable has junk value
for(ans=opt;ans!=0;ans++)
//Read as ans = junkvalue, while junk is != 0, junk++

3) Didn't bother with the rest

mrnutty 761 Senior Poster
char end = 'n';
do
{
   //do logic
}while(end != 'E' || end != 'e');
mrnutty 761 Senior Poster

Dear firstPerson,

> And just when you thought there isn't another way :

There is a bug in line 20 as reported by CODE::BLOCK and Dev-C++, viz.,
"expected primary-expression before "unsigned"". Borland 5.02 reported "expression syntax error".
Please check.

BTW, what compiler are you using?

Visual studio 2008. Just take away the unsigned part, I guess.

mrnutty 761 Senior Poster

Flawed and bigger :( you win.

You learn from mistakes.

mrnutty 761 Senior Poster

Heyy guyss i would REALLY Appreciate if you could please tell me how to do this.
I need to create a program which will take 5 dollar amounts from the user (keyboard) and output the total entered. The program should have output that looks like the following:
$5.34
$10.62
$0.25
$148.23
Total: $164.43

- make 5 variable, or arrays.
- Make a total variable. initialize it to 0;
- ask user to input 5 times
- make total += input1, total += input2 ...

[Example]

Scanner read = new Scanner(System.in);
float Total = 0.0f;
float input1 = 0;
float input2 = 0;
input1 = read.nextInt();
Total += input1;
input2 = read.nextInt();
Total += input2;
System.out.print("Total is = " + Total + "\n");