Results 1 to 10 of 10

Thread: Coding help

  1. #1
    Depression Moon's Avatar
    Join Date
    Aug 2007
    Location
    Warrior Falls
    Posts
    6,050
    Articles
    45
    Blog Entries
    2
    Contributions
    • Former Editor

    Default Coding help

    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.

  2. #2
    I'm selling these fine leather jackets Aerith's Knight's Avatar
    Join Date
    Jul 2005
    Location
    The Netherlands
    Posts
    10,825
    Blog Entries
    1

    Default

    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.


  3. #3
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    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.

  4. #4
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    double miles = 100;
    constant double MILE_TO_KILOMETER = 1.609;
    double kilometer = miles * MILE_TO_KILOMETER;
    >>cout kilometer;

    ???? It's been a while
    Oh gods, why? ಥ_ಥ


  5. #5
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Code:
    cout << kilometer;
    But you don't learn anything from having your homework done for you.

  6. #6
    Depression Moon's Avatar
    Join Date
    Aug 2007
    Location
    Warrior Falls
    Posts
    6,050
    Articles
    45
    Blog Entries
    2
    Contributions
    • Former Editor

    Default

    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.

  7. #7
    Depression Moon's Avatar
    Join Date
    Aug 2007
    Location
    Warrior Falls
    Posts
    6,050
    Articles
    45
    Blog Entries
    2
    Contributions
    • Former Editor

    Default

    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.


    Code:
    #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;             
      }
    Code:
    #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>
    Last edited by Depression Moon; 06-19-2009 at 10:01 PM.

  8. #8

    Default

    Quote Originally Posted by Depression Moon View Post
    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.

    Code:
     #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:
    Code:
    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

  9. #9
    Depression Moon's Avatar
    Join Date
    Aug 2007
    Location
    Warrior Falls
    Posts
    6,050
    Articles
    45
    Blog Entries
    2
    Contributions
    • Former Editor

    Default

    Oh I had my problem resolved a while ago, but i thank you for the help anyway.

  10. #10
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    Quote Originally Posted by The Unknown Guru View Post
    ...

    Eww, I've always hated switch. :yuck: You can do so much more with if statements...
    ...
    What the smurf crazy planet do you live on?! Switch is the !!! 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

    [borataccent] I'm very excite![/borataccent]

    You should convert to the cult enlightenment known as Java!!!
    Oh gods, why? ಥ_ಥ


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •