jonsca commented: Are you okay, heck of a cough there LOL +1
mrnutty 761 Senior Poster
Also , "&" means "the address of", it returns the address of a variable.
"&" is the bitwise operator when used in proper context.
"&" is reference operator when used in proper context.
"*" is a multiplication operator when used in proper context.
"*" is a de-referencing operator when used in proper context.
"&*" is a reference to a pointer type.
Find the zero of the function
x^2 - 5 = 0
, that is, find what value for x, will cause this function
x^2 - 5
to equal zero.
A good approach will use calculus and for loops.
Note by finding the zero of the function x^2 - 5, you have found the sqrt(5).
Hint : newton raphson method.
Yes. The main use of glut is to create a window and handle events. Windows API can do the same, so you can use win32 with opengl. In fact
in NEHE tutorial, thats what they use.
Thank Lud - just mark this thread closed and be done with it. If you want to espouse your religious views on Evolution, start a new thread.
Don't listen to him. We will not start debating about religion, Please?
This code :
if (positionHuman == 'A')
{
grid [0][0] = 'X';
showgrid();
}
else if (positionHuman == 'B')
{
grid [0][1] = 'X';
showgrid();
}
else if (positionHuman == 'C')
{
grid [0][2] = 'X';
showgrid();
}
else if (positionHuman == 'D')
{
grid [1][0] = 'X';
showgrid();
}
else if (positionHuman == 'E')
{
grid [1][1] = 'X';
showgrid();
}
else if (positionHuman == 'F')
{
grid [1][2] = 'X';
showgrid();
}
else if (positionHuman == 'G')
{
grid [2][0] = 'X';
showgrid();
}
else if (positionHuman == 'H')
{
grid [2][1] = 'X';
showgrid();
}
else if (positionHuman == 'I')
{
grid [2][2] = 'X';
showgrid();
}
Can be reduced to this :
int index = positionHuman - 'A';
int row = 0;
int col = 0;
if( index < 3 ){
row = 0;
col = index % 3;
}
else if( index< 6){
row = 1;
col = index % 3;
}
else {
row = 2;
col = index% 3;
}
The same goes for your placePiecePc function.
It could be shorter but my brain is not working right now. You should have used 1d array to simplify things.
If you wan't to check if a position is occupied, you can do the same thing I did above. Find its corresponding row, and column and check if it containes 'X' or 'O', if not then its free.
EDIT:
you can substitute this code for the above as well.
int index = humanPosition - 'A';
int row = 0;
int col = index % 3;
while( index/3 >= 1 ){
row++;
index /= 3;
}
well you know that the file contains an integer and a char and it repeats.
So read in a integer first then read in the char. Use the integer and forget
about the char.
You can do something like this :
bool isPressedInsideCircle = false;
while( game.stillRunning() ){
if( Z_KEY_PRESSED ){
if( fallingCircleIsWithinScoreBounds() ){
isPressedInsideCircle = true;
player.increaseScore();
}
}
if( circleIsOutOfScreen ){
if( isPressedInsideCircle == false ){
player.setScore( 0 );
}
circle.setLocation( TOP_OF_SCREEN );
}
}
If you wan't to learn about linked list, try implementing it. Give it a shot and
tells us how it goes, that is if your up for it.
Actually, here is the code. Your teacher will be impressed! =)
<snipped>
I beg to differ. Please, if you are going to blatantly give the answer
out, at least give a good answer. Not trying to be rude, just telling
the truth.
You could try making a tic-tac-toe game.
In my last class, we made a ALU. We worked with 4 bit adder. It accepts
2 4-bit number and returned the addition of those number, but you knew
that. One usefulness I used the cout was to detect an overflow. Since a
4 bit number has the max representation 2^4-1, adding 2 4-bit could
cause an overflow, thus when carry out is true, there is an overflow,
since the highest bit carried.
You can do something like this :
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Trouble : public std::exception
{
private:
string errMsg;
public:
Trouble(const wstring msg)
//convert from wstring to string
: exception( string(msg.begin(), msg.end() ).c_str() )
{
errMsg = string( msg.begin(), msg.end() );
//testing
throw *this;
}
};
int main()
{
try{
Trouble(L"testing");
}
catch(Trouble& e){
cout << e.what() << endl;
}
return 0;
}
Although its probably not portable.
I didn't see anywhere that specified the input filename, so I assume it is argv[1]? Here's my submission. I think it works if I understand the problem correctly.
<snipped>
It should be right. There is no input file.
The codechef compiler compiles the code trying some input cases. The input should be
from the standard input, cin for C++.
Since people posted their solution. I figure i'll post mines. Its built
more for speed than safety, since the time limit is < 1 sec for about
10^6 test cases.
#include <stdio.h>
int main()
{
size_t T = 0;
scanf("%i",&T);
size_t *ansArray = new size_t[T];
unsigned long answer = 1;
int currIndx = 0;
while(T--)
{
unsigned long maxApp = 0;
scanf("%ul",&maxApp);
while(answer <= maxApp){
answer <<= 1;
}
answer >>= 1;
ansArray[currIndx++] = answer;
answer = 2;
}
for(int i = 0; i < currIndx; i++){
printf("%i\n",ansArray[i]);
}
delete [] ansArray;
return 0;
}
Thats the slowest your program should run. I will test your code with codechef. It will advise me if your program runs slower than 1 second. There will be up to 10^6 test cases.
>>I can't believe nobody gets me. I believe in pi just like you do but I found out the hard way.
Are you sure, because all this time I thought we were arguing
because we disagreed in what pi was ?
How did we go from pi to evolution to religion ?
Just leave him and his theories alone; There is no point.
"OpenGL is the industry's most widely used, supported and best documented 2D/3D graphics API making it inexpensive & easy to obtain information on implementing OpenGL in hardware and software"
Its a graphics library that provides an interface for the user to draw 2d
or 3d.
Glut and SDL are similar.
"GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms."
glut and sdl provide an interface for the user to handle creating window.
They help it make it easier to program applications. For example, using
glut, you can handle creating a window, and communicating with the
mouse and keyboard, and use opengl to just draw stuff.
Just using, SDL you can do 2d graphics as well.
Don't you realize that since this :
char *fileName = new char;
makes fileName a char variable and not an array,
then this
string *RINGS = new string;
makes RINGS a string variable and not an Array.
Again, you need this :
int max = 4;
string * arrString = new string[4];
Better yet, if you can use vectors.
std::vector< string > arrStr;
for(int i = 0; i < rings; i++){
string tmp;
getline( cin , tmp );
arrStr.push_back( tmp ); //add string into our array
}
To firstPerson
If I want to string array2D[][MAX_COL] as a parameter in the function
in main() how would I declare that variable?
string array2D[][Max_COL] // gives error saying undetermined sizeand how would I initialize that function at the beginning of the code?
Thanks
Like this :
const int ROW = 5;
const int COL = 5;
void foo(string array2d[ROW][COL]){
}
int main()
{
string mainArray[ROW][COL];
foo(mainArray);
}
OMG, this is so urgent. Let me help you quick before the world ends.
This part :
char *fileName = new char;
That makes fileName a char variable, not an array. What you meant to
do was this :
char *fileName = new char[100];
But thats dangerous at this stage. Since you are using strings, just
do this :
string fileName;
...
cin >> fileName;
ifstream inFile( fileName.c_str() );
...
First the numbers you can accept is limited to the bytes of memory the
computer has. For an algorithm for division of large numbers, google
newton raphson method.
You have the answer in your face. All you have to do is copy it into code.
>>The examples below is based on the number: 129
Ex: Round down 129 to nearest 40 gives: 120 in this way:
40 + 40 + 40 = 120
Try to do exactly that, in code.
If we are rounding down to the nearest 10. We can do this
int num = 129;
int roundedDown = 1;
//round down to the nearest 10
while( roundedDown < num - 10 ){
roundedDown += 10;
}
For this problem, if you get 1, other will be simple. So focus on 1
function first.
>>Round down to nearest 5 gives: 125
Before we go further.
What happens if we say this :
Round down to nearest 5 for number 124 ?
Round down to nearest 5 for number 3 ?
Can you give more input/output and a little more info?
Edit : I see
If you know the RowSize and colSize you can do this, but I see no point.
Using 2 for loops makes it more clear.
void print(int A[MAX_ROW][MAX_COL])
{
int row = 0;
int col = 0;
for(row = 0, col = 0; row < MAX_ROW; )
{
//increment row and reset col if it reaches the end
if(col != 0 && col % MAX_COL == 0)
{
row++;
col = 0; //reset column
printf("\n");
}
if(row < MAX_ROW)
printf("%i ", A[row][col]);
col++;
}
}
When you say game, do you mean console ( just C++ ) or using a
graphics library. If "using a graphics library", how much knowledge or
experience do you have using that graphics library? Give more details.
Although I am still in college, I can still give my opinion hoping it will
help somehow.
For hardware designer, you will need about half CSE and half EE.
From physics 1, you might using basics law. I would think you will use more physics 2, since it will deal with electromagnetism, and more with
the physics of electricity. You will need higher level of math, presumably,
Although calc 1 might be used more than others.
You said that you wanted to create your own language. I suggested
that you create your own language inside a text file, and prase it using
C++ and compile it.
If you wan't to create it from scratch, then you probably need to learn
assembly or some other low level language.
Give it a shot and see how it goes.
>>it'd be neat if I can return a 2-d array
It would also be slow and maybe dangerous depending on some things.
Why not just pass in a 2d array, and fill it with the data from the file?
From what I see , you do not need to return a 2d array. Consider just
doing this :
void set_element(string array2D[][MAX_COL], const int MAX_ROW)
{
for (int i = 0 ; i < MAX_ROW ; i++)
{
for (int j = 0 ; j < MAX_COL ; j++)
{
array2D[i][j]=" ";
}
}
}
So writing your language inside a text file and using C++ to read it
and interpret it and compile it not an option?
This question is from code chef, named Odd. I believe this
is a good problem to play with for all levels. If I can solve it surely
anyone else can, cough * only if pi is fake* cough*.
Here is the question :
The captain of the ship TITANIC is a little .... off the track. He needs to select the crew for the ship. But everyone seems to be eligible. So to test their intelligence, he plays a game. The contestants have to stand in a line. They are given the numbers in the order in which they stand, starting from 1. The captain then removes all the contestants that are standing at an odd position. Initially, standing people have numbers - 1,2,3,4,5... After first pass, people left are - 2,4,...
After second pass - 4,....
And so on.
You want to board the ship as a crew member. Given the total number of applicants for a position, find the best place to stand in the line so that you are selected.
[B]Input[/B]
First line contains the number of test cases t (t<=10^6).
The next t lines contain integer n, the number of applicants for that case. (n<=10^9)
[B]Output[/B]
Display t lines, each containg a single integer, the place where you would stand to win a place at TITANIC.
Example
(Added) *Comments below not needed in the output;
Input:
2 //number of inputs
5 //number of applicants
12 //number of applicants
Output:
4 //best position …
>>This works, although for large exponents is highly inefficient. Which leads us to the next challenge:
Write a program which given an exponent N, designs an optimal plan of calculating N'th power.
I think you should start a new thread if you wan't to purpose a different
problem.
This code :
class A { virtual c Read(); }
class B: public A {
public c Read(){
D d ;
return d;
}
}
class C {}
class D: public C {}
void main (){
B b;
D d = b.Read(); // how will i convert it??
}
}
Makes no sense. What is "c" before the Read function in class A.
Are you trying to practice polymorphism? What are you trying to do?
>>working on the theory of what makes all the elements on the periodic table and its shape
Here I will give you the answer. So you can use you time even more
wisely and amuse me.
Atoms. Electrons , protons , neutrons, Quarks. And their charge gives them their unique shapes.
Here is a way you can solve it : (152)_8 = (1 * 8^2 ) + (5 * 8^1) + (2*8^0) = 64 + 40 + 2 = (106)_10
Similarly : (211)_x = (2 * x^2) + (1 * x^1) + (1 * x^0) = 2x^2 + x + 1
Now we need it to be : 2x^2 + x + 1 = (106)_10
To do that we start inputting values for x, particularly starting from base 3 and up. Ex : base 3 : 2x^2 + x + 1 becomes 2(3)^2 + (3) + 1 = 18 + 3 + 1 = 22 == (106)_10 (FALSE)
Ex : base 5 : 2x^2 + x + 1 becomes 2(5)^2 + (5) + 1 = 50 + 5 + 1 = 56 == (106)_10 (FALSE)
Ex : base 7 : 2x^2 + x + 1 becomes 2(7)^2 + (7) + 1 = 98 + 7 + 1 = 106 == (106)_10( TRUE)
From there we see that its base 7, since it matches the other answer.
>> assuming I understand them
Yes, but instead of asking the user for the number N; N has to be
the sum from 2^0 -> 2^50. I though the instructions were clear enough,
especially with the examples.
Frankly, I did not understand the statement of the problem. The more I read it the more contradictory it looks. Maybe I shall quit drinking.
Can you please translate the following
into English so that a drunk Russian may understand?
Sorry for the unclarity.
A multiple sum of digits, is a number N, where N is the multiplied
of the individual digits of a number M, where M is the sum of the
digits of a number O.
For example:
In the problem above, first find a number O, where O is the sum
of numbers from 2^0 to 2^50.
For now assume the sum of the numbers from 2^0 to 2^50 is 123456789. Then next, you need to find the sum of the digits of those number.
The sum of the digits of the number, 123456789 is = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.
Now the multiplied sum of digits is the multiplication of the individual
digits that consists in the sum of digits. Since the sum of the digits
we found is 45. The multiplied sum of digits is the multiplication
of the individual digits, so its = 4*5 = 20.
Therefore the multiplied sum of digits from the number 2^0 to 2^50
is 20.
Hope that was a bit clearer.
If you std::strings, then you could just compare them as if you would compare it with numbers.
int x = 4;
int y = 4;
bool b = x == y;
std::string str = "abc";
std::string str2 = "abc";
bool b2 = str == str2;
Hey guys, Need some back up on the challenge question in C++ forum.
Or is this just going to be another washed up thread?
Start over. Delete that file. Create a new C++ file. First post
the simple code into it and run it. Check that it works. Now Paste the
second equally simple code. Now "build" it, either from the tab above
or press F7. Then run it.
<quote>
So from this point on, how can i use these geographical data, generated by known math equations regarding earth & sun, to create a flat world map with a line separating day & night parts. (if possible a half-shaded area in between to show the transition but this is not indispensable.)
Oh ok, so You need to use some type of GUI to create a flat surface.
Do you know of any?
>>So, how do i go from coordinates the position of sun rays to creating dots that separate illuminated (day) portion of earth on a flat map from dark portion?
Can you clarify a bit more?
Some of us were having some discussion about posting some
challenge question for the community to participate in. This way
people could learn from the more experienced person's solution.
Here is the question that :
Intro :
A multiplied Sum Of digits is the sum of the digits of a number
N, in which those those sum of digits, are then multiplied of digits.
An example :
Let N = 12345;
SumOfDigits = 1 + 2 + 3 + 4 + 5 = 15;
Multiplied Sum of Digits = 1 * 5 = 5;
Problem Statement : Find the multiplied Sum Of digits of The number
N. Where N equals to the sum of the number from [ 2^0 , 2^50]
Example run :
N = The sum of the numbers from [2^0, 2^4] = 1+2+4+8+16 = 31
The sum of the Digits of N = 3 + 1 = 4;
The multiplied sum of digits of N = 4
[EDIT]
Guides :
1) Make your code clean.
2) Follow conventional coding standards
3) Make you code readable.
4) Make it pure C++ since this is a C++ forum
5) Try to make it more portable as possible
6) Post code Here I guess
7) Do not post questions or anything else except solutions here.
8) PM me if something is unclear or you have a question, so I can fix the question or answer yours.
Merry Christmas.
[/EDIT]
Change this :
char *MainMsg[20],*SearchChar;
To
const int MAXSIZE = 100;
char MainMsg[MAXSIZE];
char SearchChar[MAXSIZE];
Better yet, get rid of those and use std::strings.
From my understanding of what I have been taught you cannot move diagonally on the grid so say you were on point 0,0,0 (x y z) and wanted to get to point 1,1,1 (x y z) then you would need to first move up then move across x then move across y as the corners of where the lines meet on the grid are like barriers. And that is where the cube effect comes in which makes me wonder if there are by far more than 3 dimensions.
You just disregarded my main point :
"So realize that, Cartesian coordinate, polar coordinate, cylindrical coordinate, or any other coordinate one uses, is only a model. Its just a way to represents something. Its NOT REAL "