mrnutty 761 Senior Poster

>>Is it possible to do function overloading with different return type?

No its not consider this situation :

void what(){ return /* nothing */ ;  }
bool what(){ return true; }
int main(){
 what(); //which function does this call ?
}

As you see, overloading functions with different return types makes
function calls ambiguous.

However, what you have written here :

void myfunc(double x, double y);
double myfunc(bool x);

is fine because myfunc(...) does not have the same parameters.

mrnutty 761 Senior Poster

try something like this :

const int UPPER_LIMIT = 10;
const int LOWER_LIMIT = 0;
int chosenOne = 3; //within lower and upper limit
int  random = chosenOne;
while(chosenOne == random){
 random = randomNumber(LOWER_LIMIT,UPPER_LIMIT);
}

I'll let you define randomNumber(int,int) function.

mrnutty 761 Senior Poster

First make a matrix class. Have it functional with the appropriate functions.
Then open the file and read it into the matrix. Then solve it using the matrix class and its operations.

mrnutty 761 Senior Poster

Here is an example on how to grow vectors :

int main(){
 std::vector<int> numbers;
 //input 10 numbers from user
 for(int i = 0; i < 10; ++i){
   int input = 0;
   cin >> input;
   numbers.push_back( input );
 }
 return 0;
}
mrnutty 761 Senior Poster

Thats basically it. It divides the problem by 1/2 each time until it either
finds a solution or is out of bounds.

mrnutty 761 Senior Poster

Would something like this work for you :

float cut(float num, int maxDigitsAllowedAfterDecimal){
 //error checking if you want
 int wholePart = num;
 if(maxDigitsAllowedAfterDecimal == 0) return wholePart;
 float decimalPart = wholePart - num;
 long factor = pow(10,maxDigitAllowedAfterDecimal);
 return wholePart + float(( long((decimalPart*factor))/factor));
 
}
mrnutty 761 Senior Poster

I think its hear :

cin >> userGuess;
	while (!cin >> userGuess) {

In there you are asking the user to input a guess first. Then say it failed.
Then in the while loop it goes to ask the user to input a number
again. But the stream is in a failed state so that produces an error.
And since I don't know the operator precedience by head, I am not
sure if the ! gets evaluated before >>.

TO solve your problem change this part :

cin >> userGuess;
	while (!cin >> userGuess)

to this :

//cin >> userGuess;
while (!(cin >> userGuess) ){ ... }

Notice I commented out the cin >> userGuess part, which means you
don't need that.
mrnutty 761 Senior Poster

Probably some thing of this sort

void rev(int* arr, int lower, int upper)
{
     if ( lower >= upper)
        return;

     // Swap the 2 values
     rev(arr, lower+1, upper-1);
}

Where lower and upper are the smallest and the largest indexes of the array

Where are you changing the array?

mrnutty 761 Senior Poster

Composition just means, use another class object inside a class.

You don't have to make a Tree class, if you want you can do something like this :

class Sprite{
 Vector position;
 Vector velocity;
 Image img;
//...
}

The point is that use Vector to store the position of an object by
using composition, in other words, by creating an Vector object inside
our Sprite class. As you see, you can even store the velocity( speed and direction) of the sprite easily. Here is an example that might help :

#include <iostream>
#include <ctime>
using namespace std;
struct Vector{
 float posX;
 float posY;
 Vector() : posX(0.0f) , posY(0.0f){}
 Vector(float x, float y) : posX(x) , posY(y){}
};
class Sprite{
public:
 Vector position;
 Vector velocity;

 Sprite(){}
 Sprite(float posX, float posY, float spdX, float spdY){
  position = Vector(posX,posY);
  velocity = Vector(spdX,spdY);
 }
};

int main(){
 const int NUM_OF_TREES = 10;
 Sprite sprite[NUM_OF_TREES];
 srand(time(0));

 for(int elem = 0; elem != NUM_OF_TREES; ++elem){
  sprite.velocity.x = rand() % 10;
  sprite.velocity.y = rand() % 10;
  sprite.position.x = rand() % 10;
  sprite.position.y = rand() % 10;
 }
}

Now their maybe error because the above code is not compiled. But
the point is the main idea. I did not have to create a posX and posY
variable for each Tree sprite. Instead I let the for loop
do the work, and let it generate random speed and position for
each sprite.

mrnutty 761 Senior Poster

Usually one would use Vectors to store the positions. Like so :

struct Vector{
 float posX;
 float posY;
 //....
}

And if your Tree sprite class is something like this :

class Tree{
 private : 
  Vector position;
 //...
}

You would use composition inside your Tree class. Then if you want
500 trees, you can use an array like so :

Tree allTrees[500];

Now each tree has an x and y position. All you need to do now is
strategically place the trees in position, perhaps using loops or other
mechanism.

mrnutty 761 Senior Poster

>>matrix<T> operator+(const matrix<T> & other) const {

You're still not getting it. You have const corrected function header. That
means you cannot change your member variables state. Which means
this->data.push_back (this->data + s_row); is illegal!!!!!

mrnutty 761 Senior Poster

1st Create an array of 20 elements like so :

const int MAX_STUDENTS = 20;
int Scores[MAX_STUDENTS] = {0}; //create and initialize all 20 elements to 0

next you need to ask the user to enter 20 integers. Using a for loop is
a good idea. For example, you can do something like so :

for i = 0 to MAX_STUDENTS
 cin >> Scores[i]

now you need to find four things, the max , min, average, and median.

The "max" is the highest number in the array
The "min" is the lowest number in the array
The "average" is the total addition of all numbers in the array divided by the number of elements in the array
The "median" is the middle number, when the array is sorted.

To get all of those, I would suggest you to sort the array using simply
a bubble sort. Maybe like so :

for(int i = 0; i < MAX_STUDENTS; ++i){
 for(int j = 0; j < MAX_STUDENTS; ++j){
   if(Scores[i] > Scores[j]){ std::swap(Array[i],Array[j]); }
 }
}

By sorting the array, you can find the min and max easily. You can
then find the average by using a for loop. You can also find the median
easily as well. Give it a go and come back if you have trouble.

mrnutty 761 Senior Poster

Your function prototype is constant corrected, so that means you
cannot change the member variables, which you should not have to.

First you need to check if both the matrix are of equal size( equal row AND equal column ). Then next, you should create a new matrix object, that has a size equal to the matrix passed in. Then in a for loop, like you
were doing, just set the value at the index to the addition of the matrices at the same index.

mrnutty 761 Senior Poster

put a break statement inside all of the if statement. Why not combine all of
the if statement and provide a generic error message, if the detailed
message is not important?

mrnutty 761 Senior Poster

You can use std::find

mrnutty 761 Senior Poster

You should not worry about how fast a fundamental loop is. If you
care about speed, then your bottleneck is probably elsewhere.

mrnutty 761 Senior Poster

You can't define a function inside another function. Here is one way :

#include <iostream>
void aFunction(int x, int y); //function prototype
int main(){
  aFunction(2,2); //function call
}//end of main
void aFunction(int x, int y){ //function header and definition
 cout << "(" << x << "," << y << ")" << endl;
}

You see, in the above code, the function definition is outside in its own
space, and not defined inside main.

mrnutty 761 Senior Poster

First thing that you need to do is write a driver program that has a main function in order to test it. Test the constructors and printRational. If you haven't written printRational, write it because you'll need it a lot when testing.

// driver program 
// includes

int main ()
{
    RationalNumber a();
    RationalNumber b (3, 4);
    a.printRational ();
    b.printRational ();
    return 0;
}

See if it compiles/runs/gives good results. If not, debug. If yes, you're on your way and you can write some of the operators.

Minor error on the red highlighted part. The compiler will see that
as a function. So to fix that we remove the parenthesis. Making it look like this :

RationalNumber a;
VernonDozier commented: Good catch. +11
mrnutty 761 Senior Poster

change this :

# include <iostream.h>
# include <conio.h>
main ()

to this :

#include <iostream>
#include <string>
using namespace std;
int main ()

and instead of getch() use this :

cin.clear();
cin.ignore(256,'\n');
string pause;
getline(cin,pause);
mrnutty 761 Senior Poster

Assuming you are setting it something like this :

class Node{
//...
};
template<typename Type>
class Queue{
//...
};

int main(){
 Queue<Node> queueNodes;
}

If not then post some code so it will be more clearer.

mrnutty 761 Senior Poster

>>I want to calculate sqrt(2) to the maximum level of digits possible...

You can't. Its a irrational number.

jonsca commented: Bazinga! +4
mrnutty 761 Senior Poster

We can but not the way you showed it.

if ( (choice == 'e' || choice ==  'E') && (cur == 'd' || cur == 'D' ) ){
 //...
}
mrnutty 761 Senior Poster

Have a device where men can see what women are thinking, so they wont be so complicated.

mrnutty 761 Senior Poster

First use arrays :
Do something like this :

struct Poles{
 Poles poles[8];
};

Next, your question :
>>I want to make a function which would use correct pole(pole1, pole2...pole8) depending on parametres

I am not exactly sure what you mean, so before I give you a answer,
you should clarify possibly with some codes.

mrnutty 761 Senior Poster

Change your pre-processors to this :

#include <fstream>
#include <string>
using namespace std;

The main thing you forgot was the using namespace std, in your code.

mrnutty 761 Senior Poster

Real Numbers are uncountable, so you can't list all of the numbers.
A proof for this is usually, done by the Cantor's_diagonal_argument.

mrnutty 761 Senior Poster

Right now forget about creating games. Its over your head, unless you
want to use something like gamemaker or something similar.
First learn how to program. Pick a language, whether it be C++ , python,
C# or whatever. After you get a decent amount of experience in programming, then you can move on to 2d or 3d if you want.

bperiod commented: If asked for games, reply for games. +0
mrnutty 761 Senior Poster

Thats because you are doing int arithmatic. Basically :

130/3 = 43
130.0/3.0 = 43.333333...

So to solve your problem, and to be safe, put a .0 at the end of each numbers. So in total it will look like this :

average = float(100.0-((25.0/(130.0-(50.0-(130.0/3.0))))*100.0) );

The float(...) is a cast so the compiler does not give warnings.
Alternatively, you could put a .0f at the end of each number and not do the cast.

mrnutty 761 Senior Poster

>> I am trying to move a player in direction angle by speed

I am kind of hesitant of what you mean exactly by that, can you explain?

mrnutty 761 Senior Poster

In my experience bureaucratese and legalese work far better for programming than does English.

Maybe so, but this certainly gives it a run.

mrnutty 761 Senior Poster

I assumed some things, because I don't know python. Thanks for correcting me.

mrnutty 761 Senior Poster

I use english, because it the most common in america.

mrnutty 761 Senior Poster

Yes its permitted. 1st of all why do you need to use it? Second, the syntax gets pretty tricky. Here is an example :

#include <iostream>
using namespace std;

struct Foo{
	int x;
	int y;
	void print(){ cout << "(" << x << "," << y << ")" << endl; }
	Foo() : x(0), y(0){}
};

//typedef to make syntax simpler
typedef int Foo::*FooMemberPointer;
typedef void (Foo::*FooFunctionPointer)(void);

int main(){
	FooMemberPointer posX = &Foo::x;
	FooFunctionPointer display = &Foo::print;

	Foo littleFoo = Foo();

	(littleFoo.*display)();
	littleFoo.*posX = 100;
	(littleFoo.*display)();
	

	return 0;
}
mrnutty 761 Senior Poster

Make Get static.

mrnutty 761 Senior Poster

Trace it out.

1)

//returns 2 times the number passed in
def T2(x):
return x * 2

2)

//return T2(12) = 12*2 = 24
def dozen():
return T2('12')

3)

//returns T2(3) = 2*3 = 6
def enestrate():
return T2(3)

now this call :

print int (dozen()) + enestrate()

Split it up and analyze it. See what you can come with.

mrnutty 761 Senior Poster

If you are on windows, then you can use PlaySound . If not then there might be something similar to your
OS. If all fails, you will have to use a 3rd party library.

mrnutty 761 Senior Poster

Oh, yes thanks for the catch. It should be rather

populateUniqueRandom(Array,Size,10,10+Size);

Come to think of it I think there is a problem with the above algorithm I posted. It should be rather this :

void populateUniqueRandom(int Array[], const int size, int start, int end){
 assert(end-start == size); //make sure the size the correct size
 for(int i = 0; i < size; ++i){
    Array[i] = start + i;
 }
 std::random_shuffle(Array,Array+size);
}
mrnutty 761 Senior Poster

Simulating the heart or something medical?

A graphical tour of your school ?

Google?

mrnutty 761 Senior Poster

>> The white X is where i want the cubes X position to be. (which changes as it rotates.

Right now where is the rectangle's x position relative to?

mrnutty 761 Senior Poster

>>I'm attempting to make it register when i move a sphere beyond it's x,y,z position.

I am confused by this statement. Can you clarify a what you mean?
Also can you clarify what exactly your problem is. Sorry I couldn't
understand what you were trying to say.

mrnutty 761 Senior Poster

Past the TVector.h and TVector.cpp here.

mrnutty 761 Senior Poster

what you are looking for are more along the lines of this :

void populateUniqueRandom(int Array[], const int size, int start, int end){
 assert(end-start == size); //make sure the size the correct size
 for(int i = 0; i < size; ++i){
    Array[i] = end - start + i;
 }
 std::random_shuffle(Array,Array+size);
}
int main(){
 const int Size = 5;
 int Array[Size] = {};
 populateUniqueRandom(Array,10,10+Size); //random number from 10 to 10+Size
}
mrnutty 761 Senior Poster

When you say it crashes, when does it crash? Do you know at
what line?

Also the cipher you are doing is called ceaser shift cipher.

If you would like to wrap a encoded char to be within 32-126,
then use the modulus operator, although your method looks
like it works but not completely sure.

And also there is no need to convert the char to int and the int back
to char just to add a key to int. You can add the key to char
just like you would to an int. The compiler will implicitly convert
the char to an int during operations that you are performing.

mrnutty 761 Senior Poster

Ok, I see. Thanks. Sometimes, I know what I'm trying to say, but my
notation might be confusing to others or misinterpreted by others. Thanks for the correction.

mrnutty 761 Senior Poster

Another thing to add, I feel like you have a lot of redundant coding.
For example, you can implement the operator -() in terms of the
operator +(), instead of providing a definition for each one. Did you feel
like you have copied and pasted a lot of code?

mrnutty 761 Senior Poster

>> for ( c = 0; c < 13; c++ )

should that 13 be 14 ?

and go ahead and run the debug mode. It will give you more experience.

mrnutty 761 Senior Poster

This code below add two positive numbers in any base from binary
to base 36 with any length allowed by memory. The snippet comes
with examples. There might be some bugs, because I haven't tested
extensively, so forgive me if you find bugs. Hope people find it useful
somehow. Thanks for reading.
--------------------------------------------------------------------------------------
Oh. Found a bug, after positing this post, of course,

For the isValidNum function the if statement inside the for loop should be this :

if( unsigned(toBaseInt(num[pos])) >= base ) return false;

the comparison needed to be >= instead of >. Sorry for the bug.

mrnutty 761 Senior Poster

>> This is complete nonsense. You can't say "O(log(n)) < f(n)".

Why not ? Why can't a function be bounded by another function. For
example :

x < 2x < 3x

Thats certainly true isn't it? After all thats, why Big Omega was created.

>> Wat. No it's not.

Can you explain to me the difference between what I said and what
you said here "measurement of the "size" of the input. Often the measurement is the number of elements in the data structure, or the amount of memory used, or something with multiple variables. ".

I just started learning about big O and its families, so I'm new to this
as well. So forgive me if I said anything incorrect.

mrnutty 761 Senior Poster

thanks, I understand big-O (I've seen it used in math), but I don't understand the actual analysis of "growth rate". What is the growth rate with respect to? For example, in my is_bst function, will my growth rate be with respect to the extra number of comparisons it would have to make as I increase the size of my tree? the number of recursive calls? thanks

Its respect to the number n. Which is the size of the function.
As n gets bigger say for a O(n^2), it will take more time to compute
and finish computing the algorithm

mrnutty 761 Senior Poster

>> What are we even trying to measure

You are trying to measure BigO(), Big ) measures the growth rate of an algorithm. For example say there is a function f(n) , and that it follows the following inequality :

O( log(n) ) < f(n) < O(n)

the above inequality says that the function f(n) is bounded on top by
O(n), that means its worst performance is O(n). That O(n) is bigO(n).

It approximates how fast or slow the performance of an algorithm is.

To start your algorithm, depth-first-search is a well known one.
Its not easy to evaluate its performance. Thats why you can
quote whats already been proven. Take a look here and
read about its performance.