mrnutty 761 Senior Poster

Good start, now first implement isPrime function first, and test it out
with a loop from 0 to 100.

Remember a prime number can only be a prime if it is only EVENLY divisible
by itself and 1. That means if the mod operator return testPrime %
index== 0 is true and the index is > 2 and not equal to testPrime
then its not a prime.

mrnutty 761 Senior Poster

Actually you asked about theta, this code should solve it :

Declare these variable somewhere where its accessible;

const float DEGTORAD = 3.1415769/180.0f;

//initial velocity
float iVel = 7.0f;

//initial position
float posX = 0.0f;
float posY = 0.0f;

//angle being fired
float theta = 75.0f;

//initial x and y velocity calculated
float ivelX = iVel*cos(theta*DEGTORAD);
float ivelY = iVel*sin(theta*DEGTORAD);

//new velocity
float velX = 0.0f;
float velY = 0.0f;

//changing time 
float Time = 0.0f;

Now your draw function could look something like this :

GLvoid disp()
{	
	//get time in seconds
	Time = clock()*0.001f;

 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glTranslatef(0,0,-2000);
	
	glPushMatrix();
	
	glTranslatef(posX,0.0f,0.0f);
	glTranslatef(0.0f,posY,0.0f);
		glutSolidSphere(10.0f,10,10);
	glTranslatef(-posX,0.0f,0.0f);
	glTranslatef(0.0f,-posY,0.0f);
 
		
	posX += velX;
	posY += velY;
	velY = ivelY - 9.8*Time;
	velX = ivelX;


	glutPostRedisplay();
	glutSwapBuffers();

}

of course instead of glutSphere you would have some other object.

The initial velocity determines how far the object will travel
Theta will represent th angle of fire, which determines the x and y initial velocity
The equation simplifies because I am assuming that there is no other force like drag or friction, and it also
assumes that the ball will not hit a surface. If you want to make the ball bounce then this would reduce the
velocity in the x direction as a result of friction, so it would get a little more complicated. However the above is
just a simple movement. just thought your should realize that.

mrnutty 761 Senior Poster

First have the object ready to fire, meaning have the object at the position
of where its going to be launched.

Then you could do something like this :

Ball.y += ball.velY;
Ball.x += ball.velX;

Have the velX faster than velY;
Then you need to change velY and VelX;
You could do something like this :

Ball.velX += Ball.InitialVelocity - Ball.accel * Ball.time
Ball.velY += Ball.InitialVelocity - Ball.accel * Ball.time

Your ball.accel is just gravity.

mrnutty 761 Senior Poster

First you need to know the Kinematic Equations

You can use them to model projectile motion.

1) Have a initial position, initial velocity, final position, final velocity,
and acceleration. The acceleration is what is going to make your
projectile motion a projectile motion.


Here is another refrence , Link

Particularly, you can use :

x = x_0 + v*t + 1/2at^2

where x is the final position x_o is the starting position, v is the
velocity, t is the time, and a is the acceleration.

Use the equation above to model projectile motion.

Here is another refrence , Ref

It gives your the proper equations as well.

Just remember that acceleration could be position and negative,
when it is negative the velocity will decrease until its zero then the
object should stop, also you will need a timer to control the
velocity.

Give it a go and see how it goes.

mrnutty 761 Senior Poster

How about you give it a go and we'll see how it goes from there.
Be sure to use code tags when posting code.

mrnutty 761 Senior Poster

Ah, ok. I tried this...but it still doesn't work:

......
	if(info == 'c')
	{
		gauge[0]='C';
		gauge[1]='F';
		values(float value1[11]);
		for(int x=0;x<=10;x++)
			value2[x]=(value1[x]*9)/5+32;
	}
	else
	{
		gauge[0] = 'F';
		gauge[1] = 'C';
		values(float value1[11]);
		for(int x=0;x<=10;x++)
			value2[x] = ((value1[x] - 32) * 5)/9;
	}
...............

your function call should be :

values(value1);
mrnutty 761 Senior Poster

Not at all. The generator is seeded by default to a value of 1. It does not make the sequence returned by rand() any less random. The sequence is deterministic based on the seed, so every time the program runs with a default seed it will produce the same sequence. But the sequence itself is still pseudo random. :)

Ok, OP should realize this point.

mrnutty 761 Senior Poster

Just for trial, try putting :

using namespace SAMSErrorHandling;

at the top of your main.

mrnutty 761 Senior Poster

The simplest way to adjust the range of rand() is with the modulus operator:

#include <iostream>
#include <cstdlib>

int main()
{
    for (int x = 0; x < 20; ++x)
    {
        std::cout << (std::rand() % 900 + 100) << '\n';
    }
}

I am sure someone will chime in with the multitude of problems that this solution has, but if you need to worry about those problems, rand() is not the right random number generator for your application. :)

Forgot to seed.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    srand(time(0)); 

    for (int x = 0; x < 20; ++x)
    {
        std::cout << (std::rand() % 900 + 100) << '\n';
    }
}
mrnutty 761 Senior Poster

Did you call glNormalize in your init function ?

By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).

Try this :

glBegin(GL_POLYGON);
           glNormal(0.0f,0.0f,1.0f);
		glVertex3d(-8,-8,0);
		glVertex3d(8,-8,0);
		glVertex3d(8,-2,0);
		glVertex3d(-8,-2,0);
	glEnd();
mrnutty 761 Senior Poster

i have multiple of these things, how would i do this in a while statement?
this is what i have done

while (myin)
{
getline(myin, name);
cout << name;
while (myin)
{

}
cout << endl;
}

Not quite, something like this :

while(iFile.isgood()){
 //get data
}

The get data could be of the form thats in my previous post.

mrnutty 761 Senior Poster

//if the file is exactly like that then you can do this :

ifstream iFile("file.txt");
string str ;
float num1;
float num2;
float num3;

iFile >> str; //read the string
iFile >> num1 >> num2 >> num3; //read the number

If you wan't something else be clearer.

mrnutty 761 Senior Poster

Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.

mrnutty 761 Senior Poster

You get seg fault. because your overflowing stack.

It seems that you want a number ranging from 0.1 to 0.999

Look here

mrnutty 761 Senior Poster

Sure, but first be clear in your question. How to make a table is a vague question. Exactly what kind of table do you have in mind?

mrnutty 761 Senior Poster

try :

return ( a == 1 ? true : false );
mrnutty 761 Senior Poster

You need to set the stream flags, look below :

cout<<fixed; //make it so that 3.000 comes out instead of 3
	cout.precision(2); // make it so that 3.00 comes out

Now when you print something like cout << GPA << endl; and if GPA is a whole number (0,1,2,3,4) then it will print
it out like 0.00,1.00,2.00,3.00,4.00.

Put that code before you print out the GPA.

mrnutty 761 Senior Poster

Hven't tried it out and its about 3 am but I'll give it a shot :

I will build up :

CASE A : First to create a 1d array :

const int NODE_SIZE = 5;
Node * p = new Node[NODE_SIZE];

CASE B : Now to create a 2 d array you need to have each of the node
element carry some more node element.

const int NODE_X = 5;
const int NODE_Y = 5;
Node **p = new Node*[NODE_X];

//each node_x needs its own node y to create a 5x5 2d array
for(int i = 0; i < NODE_X; i++){
        p[i] = new Node[NODE_Y]

CASE C: //For a 3d array each node has to has NODE_Y element and each
NODE_Y element has to have its NODE_Z element

const int NODE_X = 5;
	const int NODE_Y = 5;
	const int NODE_Z = 5;
	
	//currentl 1d array
	Node*** p = new Node**[NODE_X];
	//now a 2d array
	for(int i = 0; i < NODE_X; i++){
		p[i] = new Node*[NODE_Y];		
	}
	//now a 3d array
	for(int i = 0; i < NODE_Y; i++)
	{
		for(int j = 0; j < NODE_Y; j++){
			p[i][j] = new Node[NODE_Z];
		}
	}

For case A ( 1 D array ) you can access and write data like this :
Node = ...

For case B (2d array) you can access and write data like this :
Node[j] = ...

For case C( 3d array) you …

mrnutty 761 Senior Poster

In your first post you haven't defined a name within scope. I think what
you are trying to is something like this :

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

class Test
{
    string name;
    public :
     Test(string str) { name = str; }
     Test() { name = "NULL"; }
     void setName(string str) { name = str; }
     string getName() { return name; }   
};

int main()
{
    Test test1("Greatest 4 eva");
    cout << test1.getName();
    test1.setName("4 eva the greatest");
    cout << test1.getName();
  return 0;
}
mrnutty 761 Senior Poster

>> However, when rotating a cube, my cube slowly shrinks. Is this s result of loss of accuracy in Math.sin() and Math.cos()? Or is it possibly because I'm casting these values to float, since I use this later for drawing...

Rotating the cube has nothing to do with its size!
Sin and cos are accurate enough. Its probably your logic somewhere.
Maybe if you show some code we can help you.
So maybe you are changing its z axis, thus making it appear smaller.

mrnutty 761 Senior Poster

>>Hi there,
Hello there.

>>I am new in java and some basic help would be appreciated. e.g
Ok lets see...

>>1-What's the out put of;
>>double number = (1/3)*3;
>>System.out.println("(1/3)*3 is equal to " + number);
>>What's missing?
Did you try this out on a compiler ? Is this a H.W question ?
Here is a hint : in programming , 1/3 != 0.333...

>>2- Convert each of the following mathematical formula to java >>expression;

>>3x and 3x+y
More hints :
Declare a variable x ;
Declare a variable result1 and result2 and a variable y
For result1 make it equal to 3 time the variable x
For result2 make it equal to result1 plus 3

>>Thank you in advance
Hope that helped. Next time show some effort.

mrnutty 761 Senior Poster

Tell me in psuedo code what each of these function below needs to do in order to achieve its purpose

void reverse(char unencrypted[], char encrypted[]);
void flip(char unencrypted[], char encrypted[]);
mrnutty 761 Senior Poster

>>Yes, I'm using a 2d array as my map. Thanks for the help, i appreciate it!
Ok then your 2d has a limit in its row and column, that limit will be your
MAP_SIZE_X and MAP_SIZE_Z.

mrnutty 761 Senior Poster

1)Draw the top border
2)Draw the first '*' and follow it up with space
3)Repeat 2 until end
4) Draw the bottom border

Maybe this will help :

void drawB()
{
     const int C = 4;
     const int R = 4;

      for(int i = 0; i < R; i++)
     {
          for(int j = 0; j < C; j++)
           {
               cout<<"*";
            }
            cout<endl;
      }
    
      
}

So you know the code above draws :
* * * *
* * * *
* * * *

what you need inside the second for loop is a if/else statement.
But what will be the condition ?
Well from the picture we can see that Row 1 and Row 4 needs all the star, i.e i = 0 && i = 3 and in the middle we see that when j = 0 we need
a star, and the we need spaces until j = 3.
putting all these together we get something like this :

//inside the second for loop
  if( i == 0 || i == 3 ) /*print stars */
  else if(j == 0 || j == 3 ) /* print start */
  else /*print spaces */

So here is what the result might look like in java :

for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if( i == 0 || i == 3)
                    System.out.print("*");
                else if(j == 0 || j == 3)
                    System.out.print("*");
                else System.out.print(" ");
            }
            System.out.println();
        }
mrnutty 761 Senior Poster

well it should have a break statement there but, when isPrime is set
to false in the loop, no matter what it will come out as false because its not being set true inside the loop, its only being set to false. So it wouldn't matter after it finds its first factor.

mrnutty 761 Senior Poster

:-O >> goto P;

Huh-oh, you don't know what you just did.

mrnutty 761 Senior Poster

Look up the definition of a prime number, and you will see that
it is a number that can only be divisible evenly by itself and 1.

For example , 3 is a prime number because 3 can only be divided by its
self(3) and 1, without having any remainder left over,
3/3 = 1 remainder 0
3/2 is a decimal number
3/1 = 1 which is a whole number, with remainder 0

Now lets see what the loop does :

for(i=1; i<=100; i++) //starts from 1 to 100, i is used as the number to see if its prime
  {
    isprime = true; //say i is a prime for now
 
    // see if the number is evenly divisible
    for(j=2; j<=i/2; j++) // j starts from 2 to i/2 because every number above i/2 is divisible by i 
    // if is is, it is not prime
    if((i%j) == 0) isprime = false; //check is evenly divisible any other number, i.e check if i / j = a whole number, if so then it is a not a prime
 
    if(isprime)
     cout << i << " is prime.\n"
  }

Read up on prime then you will get this algo.

mrnutty 761 Senior Poster

>>WHAT IS WRONG WITH THIS CODE?
1) You do not use code tags
2) You haven't included the necessary library
3) I don't think you understand what the question is asking, so
understand it before you write the program.

I'll try to break the question down for you :

Write a program that uses cin to read a given name (like Louis) and a family name (like Armstrong) into two string variables, given and family. 
//Meaning you have 2 string variables called family and given, and 
//you need to ask the user to input a first name and a last name

The program then makes assignments to two other string variables: givenFirst and givenLast. To givenFirst it assigns the concatenation of given, a space, and family. 
//means that you need 2 other string variables called givenFirst and 
//givenLast. And to givenFirst you need to concatenate or add the 
//string variable given, a space and the string variable family.
//Here is an example : 
string givenFirst = given + " " + family

To givenLast, it assigns the concatenation of family, a comma followed by a space, and given.
//same concept as above, try it out.
Sky Diploma commented: Nice!! +3
mrnutty 761 Senior Poster

>>Are the MAP_SIZEX & MAP_SIZEY built in features

No they are the limit of your map size in row and column, are you
using a 2d array as your map ?

>>Another question is, would i use the same code to scattered the battleships like this:

void placeRandomly()
{
  int x = rand()%MAP_SIZEX;
  int y = rand()%MAP_SIZEY;
 
  this.place(airCraftCarrier[], x , y);  //airCraftCarrier array
  this.place(battleShip[], x , y);
  this.place(destroyer[], x , y);
  this.place(submarine[], x , y);
  this.place(patrolBoat[], x , y);
}

It depends on your place function, if its function prototype is like so :

place(int shipType[], int size,int x, int y);

Then your function call would look something like so :

this.place(battleShip,battleShip.size(),x,y);

But I am not sure how you are representing your battle ship so I cannot say for sure.

mrnutty 761 Senior Poster

There are other alternatives like boost shared_ptr where
more than 1 pointer can have reference to an object.

I assume Node is a class or a struct, so have the user pass in
a Node *[MAP_WIDTH][MAP_HEIGHT] and from there you can copy the
original content onto the array that passed on, note what you are trying
to do might be expensive depending on the size of you 3d array.

mrnutty 761 Senior Poster

Is this for a tradition pong game, where the paddle could only have
1 x coordinate and move only up or down?

mrnutty 761 Senior Poster

Not sure what you just said but use brackets :

void print()
{
  if (num != 0) {
	  for(int i = 0; i < n; i++)
      {
		  for (int j = 0; j <n; j++) 
                 {
                      cout << num[j + n*i]<<"";
                 }
   	cout <<"\n";		  
	  }

   }
}
mrnutty 761 Senior Poster

ROTFL : Thank you :icon_smile:

mrnutty 761 Senior Poster

in your preprocessor add :

using std::vector;

Use the rand function in your library to place the battleship in random
spot.
Something like this :

void placeRandomly(){
  int x = rand()%MAP_SIZEX;
  int y = rand()%MAP_SIZEY;
  this.place(bigShip, x , y);
}
mrnutty 761 Senior Poster

Just to add, another way to prase string using sstream :

void praseString(string& src, float dst[], int size){
	stringstream convert;
	convert << src;
	for(int i = 0; i < size; i++)	
		convert >> dst[i];	
}
mrnutty 761 Senior Poster

oh, didn't see you need to round off to 2 decimal places....

setpricision is the way to go then.

No, not really, if the OP wants the value to be rounded to 2 decimal
places then he needs to convert that value into a two decimal
placed value, setprecision just output(i.e makes it appear) the final
value as rounded to 2 decimal place.

Maybe this will help;
//Its not perfectly precise

float myConvert(float num){
	return  ( int(num*100)/100.0f );
}
mrnutty 761 Senior Poster

here is a short example of what might help you :

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

class FileReader
{
	string fileName;
	string fileContent;
	bool fileRead;
public :
	FileReader(const string FileName) {
		fileName = FileName; 
		fileContent = "";
		fileRead = false;
	} 	
	bool printFileData();
	bool readFileData();
	void reset(){
		*this = FileReader(fileName);
	}
	void reset(string newFileName){
		*this = FileReader(newFileName);
	}
};

bool FileReader::readFileData()
{			
	//open file for reading
	ifstream  readingFile(fileName.c_str());	

	if(! readingFile ) { //check if file exist
		return false;
	}
	char ch = 0;
	fileRead = true;
	while( readingFile.get(ch)) fileContent += ch;	

	return true;
}
bool FileReader::printFileData()
{
	if( fileRead == false) return false;
	cout << fileContent << endl;
	return true;
}
int main()
{		
	FileReader fileIn("readMe.txt");
	
	if(! fileIn.readFileData() ){
		cout<<"File1 does not exist... exiting...\n";
		return 1;
	}

	fileIn.printFileData();

	fileIn.reset("readMe2.txt"); //give it new file to open
	if( ! fileIn.readFileData()) {
		cout<<"File2 does not exist... exiting...\n";	
		return 1; 
	} 
	
	fileIn.printFileData();

	return 0;
}
mrnutty 761 Senior Poster

>>Would you recommend breaking the order string down? If so how?

I guess if you really have to. Here is an example, breakString2 is just
breakString1 using loops, see how much space is saved. Anyways, I
provided both so you can understand what one function is doing :

#include<iostream>
#include<string>

using namespace std;

 //breaks string according the finding of a space
//vector would be better but I am not sure if you know about them.
void breakString1(string& source, string& dest1, string& dest2, string& dest3){
	//include error checker here
	string firstFavThing = "";
	string secondFavThing = "";
	string thirdFavThing = "";

	//finds the end of the position of each word
	size_t foundPos1 = 0;	
	size_t foundPos2 = 0;	
	size_t foundPos3 = 0;

	foundPos1 = source.find(" "); //find end of first space deliminated string
	firstFavThing = source.substr(0,foundPos1);
	
	foundPos1++; //by pass the space

	foundPos2 = source.find(" ",foundPos1); //find end of second string
	secondFavThing = source.substr(foundPos1,foundPos2-foundPos1);

	foundPos2++; //bypass the space

	foundPos3 = source.find(" ",foundPos2); //find end of third string
	thirdFavThing = source.substr(foundPos2,foundPos3-foundPos2);

	dest1 = firstFavThing;
	dest2 = secondFavThing;
	dest3 = thirdFavThing;
}
void breakString2(string& source, string& dest1, string& dest2, string& dest3)
{	
   //include error checker here
	size_t start = 0;
	size_t end = 0;
	string subStr[3] = {""};
	int subStrIndx = 0;
	while(subStrIndx < 3)
	{
		end = source.find(" ",start);
		subStr[subStrIndx] = source.substr(start,end-start);
		subStrIndx++;
		start = ++end;		
	}
	dest1 = subStr[0];
	dest2 = subStr[1];
	dest3 = subStr[2];
}
int main(){	
	
	string allAnswer = "";
	
	cout<<"Enter 3 of your favorite things …
mrnutty 761 Senior Poster

or you can just make your own function :

int round(float num){	
	return (int)(num+0.5);
}
int roundDown(float num){
	return (int)(num);
}

example output :

Enter a number : 3.14
Round = 3
roundDown = 3

Enter a number : 3.5
Round = 4
roundDown = 3

For negative values you will have to do a little more, but I don't think
GPA should be negative, so there is no need.

mrnutty 761 Senior Poster

You need a conversion equation , look below as an example :

#include<iostream>
using namespace std;

int main(){
	const int R = 4;
	const int C = 4;
	int A[R * C];

	for(int i = 0; i < R*C; i++)
		A[i] = i;

	//print out as row and colum 
	for(int i = 0; i < R; i ++)
	{
		for(int j = 0; j < C; j++)
		{
			cout.width(3);
			cout<<A[j + R*i ] << " ";
		}
		cout << endl;
	}

}
mrnutty 761 Senior Poster

How about dynamically allocating your Node, then your return type
can be of Node***.

Alternative, you can use 3d vectors and have the return types as 3d
vectors as well.

mrnutty 761 Senior Poster

What can I do to help?

mrnutty 761 Senior Poster

1.i cant understand the statement and dont know what i ve to do in program like i dont know what r the requirements of statment. like what is 1st step, what is 2nd one n so on.................

What statement are you talking about? Have you written any code
yet?

mrnutty 761 Senior Poster

>>Is it possible to have them all round out to only two decimal places?

First you have to decide if :

1) You want to output up to 2 decimal
2) You want the actual value of the decimal to be 2 decimal places long.

mrnutty 761 Senior Poster

Welcome, a few things :

1) When posting a question :
1_a) Make sure you research first
1_b) Ask question clearly
1_c) Use code tags

2) Try your best to write in a good grammatical way, and try not to use
slang language, yo.

MIT, huh? How is it there? I always wanted to go there for programming
but didn't have enough money, and a few other problems arose.
I hear their programming tracking are pretty good. Have fun!

P.S what is the problem that you are having?

mrnutty 761 Senior Poster

did you try <ctime>

mrnutty 761 Senior Poster

Try this :

double norms(double vectorA_xcomponent, double vectorA_ycomponent, double vectorA_zcomponent, double& norm_1)
{
    norm_1 = (vectorA_xcomponent+vectorA_ycomponent+vectorA_zcomponent);
    return norm_1;
}

Change the prototype as well.

mrnutty 761 Senior Poster

Sounds like H.W. Try googling it up, and read a couple articles and
post what you think the answer should be, and from there we could determine if you got it( from our opinion)

mrnutty 761 Senior Poster

You have a function prototype called distance AND you have a
variable called distance. I am not completely sure but your local variable
should hide the global distance function, or its just an error.
This :

squareroot = pow (float(x2-x1),2) + pow (float(y2-y1),2);
	distance = squareroot/squareroot; //will always be 1 or even worse undefined

is wrong, change it to :

squareroot = sqrt (  pow (float(x2-x1),2) + pow (float(y2-y1),2)  );	distance = squareroot;

And should you have calculate the distance inside your function instead of in your main like so :

string distance(float x1, float x2, float y1, float y2)
{
       float distance = sqrt (  pow( (x2-x1),2) , pow( (y2-y1),2) );
	if (distance < 25.0)
		return "hit";
	else if (distance == 25.0)
		return "on the rim";
	else if (distance > 25.0)
		return "miss";
}

Notice the return type specified, you need to change the prototype as
well to return string.

mrnutty 761 Senior Poster

So by now you should know that you need nested loop, right?
since there are 3 rows, your outer loop should go up to 3.

Now for your nested loop. What you can have is have 2 for loop inside the
first loop. One loop prints out '*' starting from 5 to 3 and Another loop
starting from 3 to 5 that prints out '#'.

Here is what your for loop might look like :

const int N_ROW = 3;
for(int i = 0; i < N_ROW; i++) 
{
    for(int j = 5; j > =3 ; --j){ /*do something here */ }
    for(int k = 3; k <= 5; k++) { /*do something here */ }
  
    cout << endl; //because its the end of the row
}