mrnutty 761 Senior Poster

For most assignments of this nature, most go with storing user input into a cstring.. then use a loop to display the contents of the string in reverse order.

Simple. Straight-forward.

I think he means something like this :

Enter 10 numbers :   10 9 8 7 6 5 4 3 2 1

The reverse of those inputs are : 1 2 3 4 5 6 7 8 9 10

Is this right, maverick?

mrnutty 761 Senior Poster

What exactly do you need help with?

A composition is when your class is composed of some object.

Here is an example :

class Name {  public : string aName; };

since string is a class and aName is an object, then one could say that
Name is composed of an object of the string class.


And I guess when you say arrays, in this case you mean something like this :

int main()
{
    Names president[10];
    return 0;
}
mrnutty 761 Senior Poster

>>note the question ask to reprint the array but without any repeating number

You can use the library sort method and then display the first element.
Compare it with its adjacent, if different display it, and repeat.

mrnutty 761 Senior Poster

Another way is to make a get/set functions.

mrnutty 761 Senior Poster

It is converting it correctly. It just doesn't display the trailing 0's.

If you want to see the trailing 0's then you can use DecimalFormat

mrnutty 761 Senior Poster

Well its school time so some might not have time.

mrnutty 761 Senior Poster

Just create some functions, for example a function for addition would
look like this :

int addNumbers(int number1, int number2){
   return number1 + number2;
}

And you can do similar for other operation.

It might seem like its not worth it but doing this will make your program
neater and better.

mrnutty 761 Senior Poster

Actually you can also use Pythagorean. Make a right trials with the end point from the center of the sprite and the mouse clicked point, then
all you have to do is rotate the sprite by using the sin function until the
y component of the right triangle goes to 0.

mrnutty 761 Senior Poster

How to use functions :

1) Declare a prototype .

Ex :

//int is the return type
//calculateBonus is the function's name
//int number is its argument
// the semicolon " ; " means its a prototype
int calculateBonus(int number);

2) Give it a body outside of main :

ex :

int calculateBonus(int num)
{
    if( num < 100 ) return 10;
   else if(num < 1000 ) return 100;
   else return 1000;
}

3) Use it inside main :

int main()
{
    int bonus = calculateBonus(100);
  //more code

  return 0;
}

Here is an complete example :

#include<iostream>
using namespace std;

int calculateBonus(int num)
{
    if( num < 100 ) return 10;
   else if(num < 1000 ) return 100;
   else return 1000;
}

int main()
{
   int money = 10;
   int bonus= calculateBonus(money);
   cout<<"bonus = " << bonus << endl;
   return 0;
}
mrnutty 761 Senior Poster

Is there a way to do this without using arrays?

Yep I guess, you can use if statements to sort this.

mrnutty 761 Senior Poster

When the can of frozen Orange juice says "Concentrate" and you do.

Oldy!

When you give out your number to girls in binary.

iamthwee commented: Never give out your number, always get theirs. ;) +0
mrnutty 761 Senior Poster

First you need to get the mouse's position. Then you need to know the
position vector of the sprite. Then you need to rotate the sprite
until its axis aligned with the where the mouse is. You can use the
sin function to orient the position vector of the sprite in a way that its
axis aligned with the mouses position.

If you don't know what axis aligned means then take a look http://img684.imageshack.us/img684/272/12469508.png

As you can see you will also have to calculate the line from x = 0
to x = MAX where the line needs to intersect the sprite position.
Then you will have to do the same for the mouse point. And lastly
you need to rotate the mouse until its line is very similar to that of the
mouse's point.

mrnutty 761 Senior Poster

>>PS: i already searched on the internet

Then obviously you haven't searched at daniweb.

Anyhow, do you first know how to generate a number from a min to max?

mrnutty 761 Senior Poster

Thats not going to be very easy for a beginner. What part do you need
help with? Also is the function for (2) correct? Is that column's law.

mrnutty 761 Senior Poster

How are you getting the sprite to be displayed, meaning what graphics library are you using?

mrnutty 761 Senior Poster

What have you really learned in the class so far, what concepts?

You haven't learned about functions or you don't want to use functions?

Can you use the standard functions?

Think of all possible ways you can do this before using the minimum of
32 if statements.

mrnutty 761 Senior Poster

Hi Guys,

I am trying to reverse engineer some of the openGl function into pure c++ and need some help. I can't use openGL but need to do some of the stuff it does. I am trying to figure out how to code glViewPort(...) in c++. Any help in this matter will be greatly appreciated.

Thanks

Don't waste your time. Opengl has been created when a group
of Genius's got together and built a very well graphics library.
There is no reason to do this. And I doubt that pure C++ can do
what opengl does.

mrnutty 761 Senior Poster

I didnt get it 1stperson.. mind to show me..??

In psuedocode of course :

void insertionSort(int * Array, int size)
{
	
	//declare minElem variable to 0

	//for i = 0 to i < size, increment i by 1
	{
		//set minElem to i 
              //for j = i +1 to j < size; increment j by 1
                {
                         //if array[j] is less than array[minElem]
                              //then set minElem to j
                  }
             //now swap array at i with array at minElem
	}
}
mrnutty 761 Senior Poster

well since since there are 5! possible combination, the minimum
number of if stated you will use is 2^5 = 32.

That will suck.

Just use insertion sort, the idea behind it is to mid a max* element
and swap it with the last element. Then repeat it with the last element
decreasing by 1 each time.

mrnutty 761 Senior Poster

From stranded island to hofner, cool!

mrnutty 761 Senior Poster

I wont it
like that

*  *  *  *
*        *   
*        *
*  *  *  *

By looking at the figure,what can you tell me about the; line by line?

mrnutty 761 Senior Poster

Take it step by step.

create this part first :

Enter values in first array: a h b c u v I j k e 
Enter values in Second array: y u d f g k I w q a

Then create this part :

Merged array: a y h u b d c f g v k I j w q e

And then sort it.

For the merge part, you function prototype could look like this :

void mergeArray(char * src1, int src1Size, char * src2, int src2Size, char * dest);

It assumes that dest has enough memory.

mrnutty 761 Senior Poster

I would consider the 'safer' way to be to ditch the array and go with an STL container such as a vector. In fact, a std::vector is often nicknamed a "C++ Array".
They're automatically initialised since they start out empty by default, and resize automatically every time you "push" some data into it

In addition, vectors are easier to use, and easier to learn (Which is reflected in the fact that some of the 'best of breed' C++ beginner books introduce vectors in their opening chapters)

Yea, or that.

mrnutty 761 Senior Poster

>>void class::getNumberOfoBJECTS(HOW DO I PASS FILENAME HERE?)
<<
I guess you were not listening.

void class::getNumberOfoBJECTS(string fileName) {
    fstream* file = new fstream(fileName.c_str(), fstream::in | fstream::binary);
mrnutty 761 Senior Poster

And what do you consider "the safer way" ?

Letting the standard function handle initialization and forget about
using for loops. Or use something like, int A[2] = {1,2} and let the compiler generate any compile time error there may
be.

mrnutty 761 Senior Poster

There are many ways, I will show you one way :

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

using namespace std;
 

void extractWords(string source, string * wordArray, const int size)
{
	stringstream seperate; //our stream object that will extract each word

	seperate << source; //insert what we want to extract

	int i = 0;
	//extract each word by word making sure not to go over bounds
	while(i != size && seperate >> wordArray[i]) 
		i++;

}
int main()
{		
	string phrase("To kill or not to kill");
	
	const unsigned short MAX_SIZE = 6;
	
	string words[MAX_SIZE];

	extractWords(phrase,words,MAX_SIZE);

	for(int i = 0; i < MAX_SIZE; i++){
		cout<<words[i]<<endl;
	}


	return 0;
	
}
mrnutty 761 Senior Poster

If yu wer strnded in a is_land ten wat wud u brin , u gt at mst fiv tings and no triky buzzness, understand?

I would bring (1)Angelina Jolie, then my (2)laptop (with infinite battery life,
that catches wifi from earth to outerspace), a (3)knife, an a (4)manual on
how to survive if you are stranded in an island.


ow bout yu?

Will Gresham commented: Learn to speak English, and post in the correct forums +0
nav33n commented: Member rules: We strive to be a community geared towards the professional. We strongly encourage all posts to be in full-sentence English. Please do not use "leet" speak or "chatroom" speak. +0
Ancient Dragon commented: Unreadable. Write in standard English sentences. +0
jephthah commented: here. have some more. +0
mrnutty 761 Senior Poster

I have a question. Lines #21, #22 and #23:

employee *first=NULL;
first=new employee;
if(first==NULL){
//...

If you allocate memory for pointer 'first', is that possible for 'first' to be NULL unless you get some exception?

Yes, but what he is doing is wrong. If you use nothrow
when allocating for memory, then first could be null, if it failed to allocate memory.

like this :

int *p = new(nothrow) int[100000000];
if(! p ){
cout<<"Error allocating memory\n";
}
else cout<<"good\n";
mrnutty 761 Senior Poster

If practice is what you want then

google1

google2

mrnutty 761 Senior Poster

Its undefined, google sequence points.

Aia I like your avatar.

mrnutty 761 Senior Poster

just a simple if statement will do before the calculation.

if input is -1 then return 0 
else carry on with the calculation.
mrnutty 761 Senior Poster

This one is pretty simple :

void bubbleSort(vector<int>& vec, bool sortAccendingOrder = true){

	for(int i = 0; i < vec.size();i++)
		for(int j = i+1; j < vec.size(); j++)
		{
			if(sortAccendingOrder)
			{
				if(vec[i] > vec[j])
					std::swap(vec[i],vec[j]);
			}

			else if(vec[i] < vec[j]) //else decendingOrder
				std::swap(vec[i],vec[j]);
		}
			
}
mrnutty 761 Senior Poster

For your cube array do your calculation like this :

for(int i = 0; i < Size; i++){
values[i] = values[i] * sqrt( float(values[i]) );
}

That way you multiply your square number by its root, which is the
original number you inputed.

mrnutty 761 Senior Poster

Yes, your if/else is incorrect, only 1 of them gets evaluated.

mrnutty 761 Senior Poster

From what my debugger is telling me, you have a stack overflow on line 544

mrnutty 761 Senior Poster

You have :

1 2 3
2 3 1
3 1 2

Think of it as a variable :

a b c
b c a
c a b

1) you can see that they are rotated to the left.
a b c //original
b c a //rotated to the left and is circular
c a b //rotated to the left and is circular


But an easier solution is to get input >> a>> b >> c;
and create a matrix ,
int Matrix[9] = { a , b , c , b, c, a , c , a , b }

and print it out in columns of 3.

mrnutty 761 Senior Poster

If you are running on windows then , this might help you

mrnutty 761 Senior Poster

Clock() is not "that" accurate but it should be accurate enough, unless
you need more accuracy.

Try it out first and then decide.

#include<ctime>
#include<iostream>
using namespace std;

int main()
{
    long clk_strt = clock();

   // some code goes here to test

    long clk_end = clock();
   cout<<"Difference in milliseconds : "<<clk_end - clk_strt << endl;
  return 0;
}
mrnutty 761 Senior Poster

1) use Code tags.

2) The second one. Its more reusable.

mrnutty 761 Senior Poster

This gets rid of the error in visual studio 2008, but it doesn't mean
your program works. Try it :

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

using namespace std;

int main()
{
    vector<string> gameName;
    int choice = 0;
    string game = " ";
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;

    cout<<"This allows you to maintain your favorate games \n to get started, press enter."<<endl;
    cin.get();
    cout<<"What do you want to do?"<<endl;
    cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
    cin>>choice;
	

    while(choice >= 1 && choice < 4){
        switch(choice){
            case 1:
                cout<<"Enter a new favorate game: "<<endl;
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                gameName.push_back(game);
                break;

            case 2:
                cout<<"Enter the name of the game you wish to remove:";
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                for(myIterator = gameName.begin(); myIterator !=  gameName.end(); myIterator++){
                    if(*myIterator == game){
						gameName.erase(myIterator);
                    }else{
                        cout<<"Invalid name; try again later"<<endl;
                    }
                }
                break;

            case 3:
                for(iter=gameName.begin(); iter != gameName.end(); iter++){
                    cout<<*iter<<endl;
                }
                break;
        }
        cout<<"What do you want to do?"<<endl;
        cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
        cin>>choice;
    }
    cin.get();
}
mrnutty 761 Senior Poster

I forgot to mention that this adds 2 positive string numbers.

If you make a subtraction function, then you can easily modify the addition function to add negative numbers as well.

mrnutty 761 Senior Poster

The function adds two large numeric string together, like "12345667" + "12345678" = ?

It does not have complete error checking, I'll leave that up to you.

It should work but I am prone to bugs, so do advise if bugs are found.

Included are also helper function that helps us add the large numeric string

mrnutty 761 Senior Poster

what exactly about them are troubling you?

mrnutty 761 Senior Poster

but my teacher wants the user to be able to be able to input a number of [B]any [/B]length

That means that you shouldn't use int, double , long long or whatever.
It means you should get your input as a string in order to have any length.
Primitive data types have limits on the max and min range of number
they can represent, but my teacher wants the user to be able to
be able to input a number of any length"]More Info on that.

mrnutty 761 Senior Poster

try char * retail.

But you will have to allocate memory before the use of it. BTW why do you
need it to be global?

mrnutty 761 Senior Poster

You could sort the array and the check if adjacent values are the same.

mrnutty 761 Senior Poster

Also, if the numbers aren't too far apart, then what you could do
is get the average of the 3 numbers, and find the number that is closest
to the average. This will work long as the number aren't too far apart, like
1,2,100;

for example if the input was 1,2,3 and you know that the median is 2,
but if you found the average, totalSum/totalElement = (3+2+1)/3 = 6/3 = 2, then you see that this also gets the correct answer. Use it at your
own risk.

mrnutty 761 Senior Poster

in your reverse function, what happens if num is less than 0?
What would the function return?

Get the input as a string and print the string backwards. Makes sure the string contains numeric data if you want.

mrnutty 761 Senior Poster

ouch that hurts my eyes. WHY aren't you using loops ?

do
{
    for(int i = 0; i < 15; i++){
            cout<<"|";
          for(int j = 0; j < 15; j++){
               cout<<intmap[i][j];
             }
           cout<<"|";
         }

   //rest of you code for input
}while(true)

Your teacher would fail you if she had seen that. make change ASAP.

mrnutty 761 Senior Poster

Wow. Imagine how much process a game could save by using this
technique.