PDA

View Full Version : Coding help



Depression Moon
05-29-2009, 05:45 PM
Hey all i'm having some trouble here on a particular assignment. I should be able to do this just fine, but the teacher I have is just so dull, boring, and rude that I can't quite get anything, nor can most of my other classmates. I'm not even sure if she even really knows what she's doing a couple of my classmates had to correct several times on some equations.

I'm supposed to be tranlating a pseudocode into C++ in M Visual studio I think I may remember the basic codes I'm supposed to put in, but I need a little help here with a couple of these stepos. There are 6 in total, butI'll just list three of them here to just get an understanding.

Step1: Declare a double variable named miles with inital value 100;

Step2: Declare a double constant named MILE_TO_KILOMETER with value 1.609;

Step 3: Declare a double variable named kilometer multiply miles and MILE_TO_KILOMETER and assign the result to kilometer.


I'll appreciate all the help I can get.

Aerith's Knight
05-29-2009, 07:43 PM
I'm a python programmer myself, but assigning variables and constants is about the simplest action in any programmer language. Either check your lesson material for 3 seconds or google it.

o_O
06-03-2009, 04:16 PM
You need to declare your variables using the form: [optional modifiers] [type] [name] = [value]; for example, "const char* string = 'hello\n';"

Step 1: Your type is double and your value is 100.

Step 2: Same process again, only it has to be made constant.

Step 3: You don't have to hardcode your values in variable assignment and you can perform calculations in the process. You can easily assign the value 100*30 to a variable or assign something like length*height, where length and height are predeclared.

NeoTifa
06-08-2009, 01:26 AM
double miles = 100;
constant double MILE_TO_KILOMETER = 1.609;
double kilometer = miles * MILE_TO_KILOMETER;
>>cout kilometer;

???? It's been a while :p

o_O
06-08-2009, 01:49 AM
cout << kilometer;But you don't learn anything from having your homework done for you.

Depression Moon
06-08-2009, 02:17 AM
I had this done last wekk thank you all for your help and I think you can learn from others help, it certainly helped me and that wasn't the entire thing like I said.

Depression Moon
06-19-2009, 09:47 PM
my troubles with the software have gone, but there's an issue here that i'm having with cases. I'm supposed to be adding a question to the code below that allows the user to choose if they want to do an addition subtraction, mulitiplication, or division problem.

I got some help from someone else that says that I need something like this in here. I'm wondering where exactly this should be put in the code and where I should replace some text with the math option of my choice.



#include <stdio.h>

int main(void)
{
int choice = 2;


if((choice>10) || (choice <1)){
choice = 11;
}

switch(choice)
{
case 7:
printf("\nCongratulations!");
break;

case 2:
printf("\nA");
break;

case 8:
printf("\nB");
break;

case 11:
printf("\nC");

default:
printf("\nSorry, you lose.\n");
break;
}

#include <iostream><iostream>
#include <ctime> <ctime> // For time function
#include<cstdlib> <cstdlib> // For rand and srand functions
using namespace std;

int main()
{
char continueLoop = 'Y';
while (continueLoop == 'Y')
{
// Execute body once
long startTime = time(0);
int correctCount = 0; // Count the number of correct answers
int incorrectCount = 0;
int count = 0; // Count the number of questions

while(count < 4)
{
//generate random numbers
srand(time(0));
int number1 = rand() % 10;
int number2 = rand() % 10;

//if number 1 < number 2, swap
if(number1<number2)>
{
int temp = number1;
number1 = number2;
number2 = temp;
}

//ask question "what is number1 - number2?"
cout << "What is " << number1 << " - " << number2 << "?";
int answer;
cin >> answer;

//see if answer is correct

if (number1-number2 == answer)
{
cout << "You are correct!\n";
correctCount++;
}
else
{
cout << "Your answer is wrong.\n" << number1 << " - " <<
number2 << " should be " << (number1 - number2) << endl;
incorrectCount++;
}

// Increase the count
count++;
//ask question "what is number1 - number2?"
cout << "What is " << number1 << " + " << number2 << "?";
cin >> answer;

//see if answer is correct

if (number1+number2 == answer)
{
cout << "You are correct!\n";
correctCount++;
}
else
{
cout << "Your answer is wrong.\n" << number1 << " + " <<
number2 << " should be " << (number1 + number2) << endl;
incorrectCount++;
}

// Increase the count
count++;
//ask question "what is number1 * number2?"
cout << "What is " << number1 << " * " << number2 << "?";
cin >> answer;

//see if answer is correct

if (number1*number2 == answer)
{
cout << "You are correct!\n";
correctCount++;
}
else
{
cout << "Your answer is wrong.\n" << number1 << " * " <<
number2 << " should be " << (number1 * number2) << endl;
incorrectCount++;
}

// Increase the count
count++;
//ask question "what is number1 * number2?"
cout << "What is " << number1 << " / " << number2 << "?";
cin >> answer;
if (number1 == 0)
number1 = 1;
if (number2 == 0)
number2 = 1;
//see if answer is correct

if (number1/number2 == answer)
{
cout << "You are correct!\n";
correctCount++;
}
else
{
cout << "Your answer is wrong.\n" << number1 << " / " <<
number2 << " should be " << (number1 / number2) << endl;
incorrectCount++;
}

// Increase the count
count++;
}

long endTime = time(0);
long testTime = endTime - startTime;
double correctPercent = (correctCount * 100) / (correctCount + incorrectCount);
cout << "correct count is " << correctCount << "\n";
cout << "incorrect count is " << incorrectCount << "correctPercent is " << correctPercent << "\nTest time is " << testTime << " seconds\n";
// Prompt the user for confirmation
cout << "Enter Y to continue and N to quit: ";
cin >> continueLoop;
}
system("pause");
return 0;
}


Edit: Take note that i did post the < iostream > etc part. For some reason this site doesn't show them.
</number2)></cstdlib></ctime></iostream></stdio.h>

The Unknown Guru
07-06-2009, 09:24 AM
my troubles with the software have gone, but there's an issue here that i'm having with cases. I'm supposed to be adding a question to the code below that allows the user to choose if they want to do an addition subtraction, mulitiplication, or division problem.

Eww, I've always hated switch. :yuck: You can do so much more with if statements...

But anyways, there are two routes you can go with this. You could have 4 separate Y/N questions that ask "Would you like to do an addition problem?" etc. Or you could do a strcmp and actually have the user type out what they want. That portion of the code could look something like this:

Side note: I'm weird and use printf and scanf instead of cin and cout. I would show you this with cout if I knew the syntax, but I don't. Sorry. I'm pretty sure it's similar, though.


#include string.h //obviously, put in the carrot things

int main
{
char questiontype[21]; //declare a string that takes users input

//your code

//when you get to the part where they choose:
printf("Would you like to do addition, subtraction, multiplication, or division?\n");
printf("Type in all lowercase letters, and don't misspell anything.\n");

scanf("%21[^\n]", questiontype); //user types what kind of question
flushall(); //required for scanf

if(strcmp(questiontype, "addition") == 0) //if string that took user input is identical to the phrase "addition"
{
//whatever you do to make the questions about addition
}

if(strcmp(questiontype, "subtraction") == 0) //if string that took user input is identical to the phrase "addition"
{
//whatever you do to make the questions about subtraction
}

//repeat for division and multiplication

If you really have to use switch, then you can create an int that will take the value of a strcmp and then put in something like this:

int compare;

//code

compare = strcmp(questiontype, "addition");

switch
{
case 0: //if questiontype is identical to "addition"
//do stuff
break;

default:
break;
}

//then repeat for the other operations.

If you have any more questions, feel free to PM me or something. I know C++ pretty well, and I would be happy to help :)

Depression Moon
07-06-2009, 09:12 PM
Oh I had my problem resolved a while ago, but i thank you for the help anyway.

NeoTifa
07-10-2009, 04:33 AM
...

Eww, I've always hated switch. :yuck: You can do so much more with if statements...
...



What the fuck crazy planet do you live on?! Switch is the :bou::bou::bou::bou:!!! It's a lot easier flowing than if-else statements. Jeebus. In the release of Java 7 they're even going to allow switching of Strings :D

I'm very excite!

You should convert to the cult enlightenment known as Java!!! :love: :bounce: