Need help with programming assignment.

I'm a complete newbie to C++ and only know some basic functions, my professor on the other hand must think that we all understand this language and what he's telling us to do, but to me and my mates in class, he just seems like an alien trying to communicate. Anyways, my problem is that I have a programming assignment and don't really know where to go from writing my name. As I am a student in college I must write the program alone and not copy anything, but I don't really understand the instructions. So what I am asking is that hopefully one of you guys can explain to me what I have to do.
The program is to create a basic game where the user is to locate bombs and defuse them before they go off. They have 20 tries(200 seconds) and the game gets harder/easier depending on if they win or lose. My first question is,

- The professor asks us to create a building that is 0-99 in all 3 directions. What does he mean by this? What directions? I was assuming a X Y coordinate graph so (cons int x_dim = 99 and cons int y_dim = 99) but then what would be the third direction?

- We are asked to call srand( time( null ) ). We have never even learned how to use srand so I am very confused as to what to do for this one.

For now I am asking those 2 questions, if I am to find the solution to them then I might be able to go from there, but whenever I get stuck i'll keep you guys posted. Please help!
0-99 in all 3 directions

A cube, it has height, width and depth - or x, y and z if you prefer.

0 to 99 means 100 elements, (same quantity as 1 to 100) but it starts counting from zero as arrays do in C++. const int x_dim = 100;

srand( time( null ) )
Your program will work without this, but it will generate the same sequence of random numbers every time it is run. Using the current time as a seed to the random number generator helps ensure that different random numbers are generated. But call srand() once only, at the start of main().

See
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/cstdlib/rand/
http://www.cplusplus.com/reference/ctime/time/

Thank you for that, it really helped. I have two more questions now if you do not mind.

- The program asks for each try to be 10 seconds. I was wondering how I would do that? I already set "const int initialTries = 20;" , how would I make each of those tries 10 seconds?

-The program also asks me to use a formula, but I have no idea on how to make the numerator. Here's the formula:
Signal = 1000 * (1- ( sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3)

here is my program so far:


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

int main (void) {
	
cout << "In this computer game, you are to locate a bomb and defuse it." << endl;
cout << "You are required to guess until you find the bombs. You have 20 tries." << endl;
cout << "You have a bomb detector with you that reports a signal from 0 - 1000." << endl;
cout << "The signal will be greater the closer you are and each try will take 10 seconds to complete." << endl;
cout << "The program is created by mahmad33." << endl;

srand ( time ( NULL ) );
const int initialTries = 20;
const int x_dim = 100;
const int y_dim = 100;
const int z_dim = 100;
int xBomb, yBomb, zBomb;
double xGuess;
bool Won = false;
bool Repeat = false;
xBomb = rand() % 100;
yBomb = rand() % 100;
zBomb = rand() % 100;
signal = 1000 * ( 1 - )

if ( xGuess == xBomb || xGuess == yBomb || xGuess == zBomb) {
	Won = true
}
else {
	Repeat = true
}

while ( Repeat ) {
	
}
else ( Won ) {
	return 0;
}
return 0;
}
This is only partially making sense to me.

One comment on your code so far, you went to the trouble of defining constants x_dim, y_dim and z_dim. They should be used where appropriate, rather than so-called magic numbers such as 100.
I'd suggest
1
2
3
    xBomb = rand() % x_dim;
    yBomb = rand() % y_dim;
    zBomb = rand() % z_dim;



Now the user should I suppose enter the values for xGuess, yGuess and zGuess.

However, I'm a bit unsure of the formula:
Signal = 1000 * (1- ( sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3)
The phrase "Summation of" suggests to me that you might need to add together multiple different values of i.

i = x,y,z(iGuess - iBomb)^2) / 100 radical 3
I don't understand this, it looks like a concise expression of something longer.
It may be an expression of the Pythagoraean theorem, where the distance between the guess coordinate and the bomb coordinate is calculated. (Same as finding the length of the hypotenuse of a right-angled triangle). But I don't know for sure. And the meaning of 100 radical 3 Perhaps it means the cube root of 100? See for example https://www.mathsisfun.com/definitions/radical.html

In the opening post quote,
They have 20 tries(200 seconds)

I would use a variable something like this:
int time_remaining = 200; // number of seconds left

On each try, subtract 10 from the time remaining.


Last edited on
Okay so I was able to do even more to the program and would say I am almost done with the main program. (Then i'll do the touch ups.) Here it is so far:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

int main (void) {
	
cout << "In this computer game, you are to locate a bomb and defuse it." << endl;
cout << "You are required to guess until you find the bombs. You have 20 tries." << endl;
cout << "You have a bomb detector with you that reports a signal from 0 - 1000." << endl;
cout << "The signal will be greater the closer you are and each try will take 10 seconds to complete." << endl;
cout << "The program is created by mahmad33." << endl;

srand ( time ( NULL ) );
const int initialTries = 20;
int remainingTries;
const int x_dim = 100;
const int y_dim = 100;
const int z_dim = 100;
int xBomb, yBomb, zBomb, seconds;
double xGuess, zGuess, yGuess, a, b, c, d, e, f, g, h, j, i, signal;
seconds = remainingTries * 10;
bool Won = false;
bool Repeat = false;
bool Tryagain = false;
char choice;
xBomb = rand() % x_dim;
yBomb = rand() % y_dim;
zBomb = rand() % z_dim;
a = (xGuess - xBomb);
b = (yGuess - yBomb);
c = (zGuess - zBomb);
d = pow(a, 2.0);
e = pow(b, 2.0);
f = pow(c, 2.0);
i = (a + b + c);
g = (100 * sqrt(3)); 
h = sqrt(i);
j = ( h / g );
signal = 1000 * ( 1 - j );

if ( initialTries == 20 ) {
	Tryagain == false;
	remainingTries == initialTries;
}

while (Tryagain == false) {


	for (remainingTries == initialTries; remainingTries > 0; --remainingTries ) {
		// Enter guesses and if wrong then V. If guesses are right, set Won to true.
		cout << "Enter your guess for xBomb." << endl;
		cin >> xGuess; 
		cout << "Enter your guess for yBomb." << endl;
		cin >> yGuess;
		cout << "Enter your guess for zBomb." << endl;
		cin >> zGuess;
		cout << "The bomb detector detects a signal at:" << signal << endl;
	
	if ( xGuess == xBomb && xGuess == yBomb && xGuess == zBomb) {
		Won = true;
}
	else {
		Repeat = true;
}

	while ( Repeat && remainingTries > 0 ) {
		cout << "You fucked up. You have " << seconds << "seconds left!" << endl;
		remainingTries == --remainingTries;
                break;
		
}
	if ( Won ) {
		cout << "Congratulations!!! You won!! Would you like to try again? [Y/N]?" << endl;
		cin >> choice;
		if (choice == 'y' || 'Y') {
			Tryagain = false;
		}
		else if (choice == 'n' || 'N') {
			cout << "Goodbye! Try again soon!" << endl;
			Tryagain = true;
		}
		else if (choice != 'n' || 'N' || 'y' || 'Y') {
			cout << "Invalid character. Program will exit.";
			Tryagain = true;
		}
	
}

	
}
}
system ("pause");
return 0;

}


Right now what I am having trouble getting the signal to work. Im guessing it has something to do with xBomb, yBomb, and zBomb. Any solutions to this? Also when I run the program, the amount of seconds displayed is incorrect. It should be 200 and be deducted by 10 after each try but instead it displays the number 327630 seconds? Any help on this subject?
Okay nevermind I found the solution to the seconds problem. Still need help with the signal.
Still need help with the signal.

Well, we can piece it together from the description I think.
You have a bomb detector with you that reports a signal from 0 - 1000.
The signal will be greater the closer you are

Now the formula looks a bit like this:

Signal = 1000 * (1- ( sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3)
Now there are mismatched parentheses there which already presents a problem. For present purposes I will assume there should be another ')' at the end.
which gives
1000 * (1- ( sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3))
Now the 1000 at the start is simply a scaling factor, it means without it, the rest of the expression would give a result between 0.0 and 1.0.
(1- ( sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3))
For that to work, this part of the expression must give a value from 1.0 to 0.0

sqrt ( Summation of i = x,y,z(iGuess - iBomb)^2) / 100 radical 3

My interpretation would be
sqrt ( (xBomb - xGuess)2 + (yBomb - yGuess)2 + (zBomb - zGuess)2 ) / cube_root(x_dim * y_dim * z_dim)

Edit:
No, I was wrong. The denominator is another scale factor. It corresponds to the maximum possible distance between the bomb and the guess. In turn, that is equal to the length of the diagonal of the building. If all the sides are the same (i.e. a cube) the length of the diagonal is given by 100 * sqrt(3)
Last edited on
I wrote a c++ program that fills a one dimensional array with the first 50 Fibonacci numbers. How do I then print out the numbers in reverse order I just do not seem to get the code right and then print out each number divided by the previous one
Last edited on
Help me for getting correct answere.
in this program i want to get answere with tax. But tax function is not working.

Here it is

#include <iostream>
using namespace std;
struct calls
{
int noOfCalls;
float result;
float condition();
float resultt();
};
float calls :: condition()
{
if (noOfCalls >= 0 && noOfCalls <= 100)
{
result = (noOfCalls + 0.80) * 250;
}
else if (noOfCalls >= 100 && noOfCalls <= 250)
{
result = (noOfCalls + 1.00) * 350;
}
else if (noOfCalls >= 250)
{
result = (noOfCalls + 1.25) * 500;
}
return result;
}
float calls::resultt()
{
if (result >= 1000 && result <= 1500)
result = result *(7 / 100 + 1);
else if (result >= 1501 && result <= 2000)
result = result *(10 / 100 + 1);
else if (result >= 2001)
result = result *(15 / 100 + 1);
return result;

}
void main()
{
calls e1;
e1.result = 0;
cout << "Enter NO of Calls ";
cin >> e1.noOfCalls;
e1.condition();
e1.resultt();
cout << " Your Bill is = " << e1.result;
system("pause");
}
@Mazino013, @ibrahim069
Please post your question in a new thread of your own. Don't hi-jack someone else's thread.
Topic archived. No new replies allowed.