Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: (C++) my array elements and variables reside in the same memory addresses...

  1. #11
    Join Date
    Jun 2009
    Location
    new York
    Beans
    461
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: (C++) my array elements and variables reside in the same memory addresses...

    Yes you should always have your #define one line down from the #include statements. As well, any time you use << or >> (input/output operator) you should always have a space before and after.

    ex:
    Code:
    cout << "Hello World!" << std::endl;
    Math operations and assignment are the same thing.
    Code:
    int a = 0;
    Code:
    num1 = mu2 + num3;
    You can actually read about it here: http://www.possibility.com/Cpp/CppCodingStandard.html
    Don't forget how ever that you can have your own stile, but for the most part you keep it standard. Plus it's easier for you to read too! A lot of people use their own way of commenting.
    :wq

  2. #12
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: (C++) my array elements and variables reside in the same memory addresses...

    Quote Originally Posted by sp0tz View Post
    I agree with you on the spacing. Readable syntax has been a problem of mine for awhile, so thank you for reminding me to work on it.

    However, the point of the multidimensional array was to hold a lot of data in one place. Maybe the small chunk of code I entered here isn't a good example but if the array includes one dimension for the operation and another for whether the question was answered correctly or not, it's going to be relatively easy to determine the % of multiplication questions answered correctly, what operation the user most commonly answers incorrectly/correctly, etc. The point of this array is to hold data to analyze the player's performance with, so being more detailed is better, I think.

    Using the suggested array questionsAnswered[operation], we can't determine the player's aptitude for a given question type.

    Later on, I was thinking I would add another array (or something) to keep track of things like how often the player got double-digit multiplication problems correct. This is advanced stuff that will need thorough blueprinting, but the idea is to keep a detailed record of the player's performance, so that the program is capable of doing something other than barraging the user with random math problems.
    I would consider writing a class to handle the statistics.
    would be easier to handle and more flexible than raw multidimensional arrays

  3. #13
    Join Date
    Feb 2009
    Location
    Texas, USA
    Beans
    51
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: (C++) my array elements and variables reside in the same memory addresses...

    damn. I keep trying to write something simple that I can solidify my knowledge of the basics with, but then it turns out I need all this stuff I barely know how to use. I'll look up implementing classes after I finish this program, maybe use them to add the more complicated stat tracking, but I'm gonna be so f%*#^ng bored when I take intro to programming next semester....
    ~.~

  4. #14
    Join Date
    May 2009
    Beans
    791
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: (C++) my array elements and variables reside in the same memory addresses...

    Quote Originally Posted by sp0tz View Post
    damn. I keep trying to write something simple that I can solidify my knowledge of the basics with, but then it turns out I need all this stuff I barely know how to use. I'll look up implementing classes after I finish this program, maybe use them to add the more complicated stat tracking, but I'm gonna be so f%*#^ng bored when I take intro to programming next semester....

  5. #15
    Join Date
    Jun 2009
    Location
    new York
    Beans
    461
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: (C++) my array elements and variables reside in the same memory addresses...

    Haha sp0tz I know how it is, I have already learned a lot of C when I took computer programming I using C++ and it was very boring. But on the bright side, I got an A and didn't even have to go to class, and I (as you will also) still learned a good bit mate.
    :wq

  6. #16
    Join Date
    Jun 2009
    Location
    new York
    Beans
    461
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: (C++) my array elements and variables reside in the same memory addresses...

    Code:
    //math test
    //a program that asks a lot of math questions, then records your answers
    //it's fun.
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    #include <fstream>
    using namespace std;
    
    //Global variables
    #define ADDITION             1
    #define SUBTRACTION          2
    #define MULTIPLICATION          3
    #define DIVISION          4
    #define ANSWERED_CORRECTLY   0
    #define ANSWERED_INCORRECTLY 1
    
    
    int main(){
        //Set seed to 0
        srand((unsigned)time(0));
    
        //initialize integers
        int questions[4][1];
        int answer_sane = 0;
        int operation   = 0;
        
        questions[MULTIPLICATION][ANSWERED_CORRECTLY]   = 0;
        questions[DIVISION][ANSWERED_CORRECTLY]         = 0;
        questions[ADDITION][ANSWERED_CORRECTLY]         = 0;
        questions[SUBTRACTION][ANSWERED_CORRECTLY]      = 0;
        questions[MULTIPLICATION][ANSWERED_INCORRECTLY] = 0;
        questions[DIVISION][ANSWERED_INCORRECTLY]       = 0;
        questions[ADDITION][ANSWERED_INCORRECTLY]       = 0;
        questions[SUBTRACTION][ANSWERED_INCORRECTLY]    = 0;
    
        cout << "before:\n";
        cout << questions[DIVISION][ANSWERED_CORRECTLY]         << endl;
        cout << questions[DIVISION][ANSWERED_INCORRECTLY]       << endl;
        cout << questions[MULTIPLICATION][ANSWERED_INCORRECTLY] << endl;
    
        answer_sane = 55555;
        operation   = 44444;
    
        cout << endl << "after:\n";
        cout << questions[DIVISION][ANSWERED_CORRECTLY]               << endl;
        cout << questions[DIVISION][ANSWERED_INCORRECTLY]             << endl;
        cout << questions[MULTIPLICATION][ANSWERED_INCORRECTLY] << endl;
    
        cout << endl << "memory addresses:\n";
        cout << &questions[DIVISION][ANSWERED_CORRECTLY]         << endl;
        cout << &questions[MULTIPLICATION][ANSWERED_INCORRECTLY] << endl;
    
        cout << &operation                                 << endl;
        cout << "\nmore addresses:\n";
        cout << &questions[DIVISION][ANSWERED_INCORRECTLY] << endl;
        cout << &answer_sane;
    
        return 0;
    }
    It's formatted differently when i paste it into here but, but that's general style you use when programming, not only in C++, but in any language
    :wq

  7. #17
    Join Date
    Feb 2009
    Location
    Texas, USA
    Beans
    51
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: (C++) my array elements and variables reside in the same memory addresses...

    n3rD, I could look at a text file, but in the meantime, what I'm getting from first glance is that it's important to space between operands and operators, and it is ideal to tab stuff out so as to make columns and patterns and such. Maybe this doesn't make sense but, like:

    Code:
    int stuff,things,penguin;
    
    cout <<  whatever_blablahblah << endl;
    cout <<  another_thing        << endl;
    cout <<  a_third_thing        << endl;
    cout <<  ^same_charcount?!    << endl;
    
    stuff   = things + 7;
    penguin = things + stuff;
    ~.~

  8. #18
    Join Date
    May 2009
    Beans
    791
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: (C++) my array elements and variables reside in the same memory addresses...


Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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