User Tag List

Results 1 to 6 of 6

Thread: Simple Arrays In C++

  1. #1
    Simple Arrays In C++

    User Info Menu

    Simple Arrays In C++

    Thanks To Evil Loco

    Code:
    //Simple Arrays By Bl4Ck.KiD...
    #include <iostream>
    #include <cmath>
    
    
    int main()
    {
    using namespace std;
    cout << "Bl4Ck.KiD...'s Simple Arrays Setup" << endl << endl;
    
    cout << "Hits In The Game" << endl;
    
    int hits [5];
    hits [0]=10;
    hits [1]=21;
    hits [2]=34;
    hits [3]=14;
    hits [4]=65;
    hits [5]=36;
    
    cout << hits[0] - hits[5] + hits[1] / hits[4] * hits[2] + hits[3] << endl << endl;
    
    cout << "Home Runs" << endl;
    
    int homeruns [4];
    homeruns [0]=5;
    homeruns [1]=3;
    homeruns [2]=0;
    homeruns [3]=1;
    
    cout << homeruns[0] + homeruns[1] + homeruns[2] + homeruns[3] <<endl <<endl;
    
    system("pause");
    return 0;
    }
    Get At Me Bl4Ck.KiD... Aka BK
    Future Rapper - Spits Fire
    Psp Coder - Mips, C
    Web Designer - HTML, Javascript, Some PHP
    Computer Programmer - Currently Learning C/C++, Visual Basic, Pascal, Brainfuck, Python, Ruby

  2. #2
    Simple Arrays In C++

    User Info Menu

    Re: Simple Arrays In C++

    That's a freaking mess. Oh wait! It's C++, nevermind :)

    In my favorite language, objective-c, arrays are simple as hell.

    Code:
    NSArray *array = [[NSArray alloc] initWithObjects:@"one", @"two", @"3", nil];

  3. #3
    Simple Arrays In C++

    User Info Menu

    Re: Simple Arrays In C++

    Quote Originally Posted by B1G_BR0TH3R View Post
    That's a freaking mess. Oh wait! It's C++, nevermind :)

    In my favorite language, objective-c, arrays are simple as hell.

    Code:
    NSArray *array = [[NSArray alloc] initWithObjects:@"one", @"two", @"3", nil];
    seems easy, i like C++ idk y, i cant do objective-c cuz u need a mac which is dumb
    Get At Me Bl4Ck.KiD... Aka BK
    Future Rapper - Spits Fire
    Psp Coder - Mips, C
    Web Designer - HTML, Javascript, Some PHP
    Computer Programmer - Currently Learning C/C++, Visual Basic, Pascal, Brainfuck, Python, Ruby

  4. #4
    Simple Arrays In C++

    User Info Menu

    Re: Simple Arrays In C++

    Quote Originally Posted by Bl4Ck.KiD... View Post
    Thanks To Evil Loco

    Code:
    //Simple Arrays By Bl4Ck.KiD...
    #include <iostream>
    #include <cmath>
    
    
    int main()
    {
    using namespace std;
    cout << "Bl4Ck.KiD...'s Simple Arrays Setup" << endl << endl;
    
    cout << "Hits In The Game" << endl;
    
    int hits [5];
    hits [0]=10;
    hits [1]=21;
    hits [2]=34;
    hits [3]=14;
    hits [4]=65;
    hits [5]=36;
    
    cout << hits[0] - hits[5] + hits[1] / hits[4] * hits[2] + hits[3] << endl << endl;
    
    cout << "Home Runs" << endl;
    
    int homeruns [4];
    homeruns [0]=5;
    homeruns [1]=3;
    homeruns [2]=0;
    homeruns [3]=1;
    
    cout << homeruns[0] + homeruns[1] + homeruns[2] + homeruns[3] <<endl <<endl;
    
    system("pause");
    return 0;
    }
    Jakey likey, Jakey likey. Here's some helpful tips:

    • This piece of code works but it's technically not right:

    Code:
    int hits [5];
    hits [0]=10;
    hits [1]=21;
    hits [2]=34;
    hits [3]=14;
    hits [4]=65;
    hits [5]=36;
    When you declared you were making an array (int hits [5]), the 5 means there are going to be only five values in it. You have 6.

    Code:
    hits [5]=36
    is technically it's own array. This works when assembled because the computer automatically gives it a somewhat random value. To prove this I made the hits [5]=36 into a comment and ran it, it outputed 280 for the value of hits [5]. After that I ran it without it commented it outputted 36. So it does work but it's technically wrong. In arrays, you define how many variables, the number you define minus one is the last number you can rightfully have when calling variables from that array. In an array 0 counts as it's own number so an array [2] would have 0 and 1, an array [3] would have 0, 1, and 2. An array [4] would have 0, 1, 2, 3. Hence an array [5] would be 0, 1, 2, 3, 4, and so on. You should get the point.


    • You do not need to include cmath

    • "\n" means new line, same exact effect as end line (endl). You will likly use it more often when your programs get larger and larger and you will be doing anything to optimize the file size.

    Rather then:
    Code:
    cout << "Bl4Ck.KiD...'s Simple Arrays Setup" << endl << endl;
    and
    Code:
    cout << "Hits In The Game" << endl;
    and
    Code:
    cout << hits[0] - hits[5] + hits[1] / hits[4] * hits[2] + hits[3] << endl << endl;
    and
    Code:
    cout << homeruns[0] + homeruns[1] + homeruns[2] + homeruns[3] <<endl <<endl;
    Consider:
    Code:
    cout << "Bl4Ck.KiD...'s Simple Arrays Setup\n\n";
    and
    Code:
    cout << "Hits In The Game\n";
    and
    Code:
    cout << hits[0] - hits[5] + hits[1] / hits[4] * hits[2] + hits[3] << "\n\n" ;
    and
    Code:
    cout << homeruns[0] + homeruns[1] + homeruns[2] + homeruns[3 ]<< "\n\n";
    • It's good to have consistent spacing. In most your your "<< endl <<" you wrote it like that but in one you wrote "<<endl <<". When your programs become very large and your trying to optimize them all you can your not going to want to looking for every endl using find. If your spacing was consistent you could easily use the replace feature.

    Example:
    Code:
    //stuff here
    cout << "blah blah blahh" << /* some variable stuff here */ << "blah blah"<<endl << endl << endl;
    This wouldn't very easy to use the replace feature.

    Code:
    //stuff here
    cout << "blah blah blahh" << /* some variable stuff here */ << "blah blah" << endl << endl << endl;
    This would because you could optimize using the replace feature by typing:

    Code:
    " << endl << endl << endl;
    into the find what box and then put

    Code:
    \n\n\n";
    into the replace with box.

    • P.S: It says thanks to Evil Loco but you took credit for it in the actual program itself... He may have just helped you or something in which case it's fine.

    Hitting the Rep+ and Thanks button never hurt anyone ;)

    Off Topic: I signed up to your site, I made a few posts including a coder application but now it says my username doesn't exist or something... Please help? Maybe your system is right but FireFox remembers it being s2h6699... Check in the coder application for the person that programs in SCAR to see if I'm just stupid or your system isn't working properly.

    Last edited by S2h6699; 04-30-2010 at 06:51 PM. Reason: Typo
    Quote Originally Posted by Epic of HITB
    get... 1) a mini fridge | 2) A toilet installed in your room | 3) a closet full of non-perishable foods | 4) Lyscol ( who needs showers anyway?) | 5) tons of books articles lectures zines etc etc

    Dont leave your room...EVER... AND I MEAN EVER
    Damned six line limit! *NERD RAGE*

  5. #5
    Simple Arrays In C++

    User Info Menu

    Re: Simple Arrays In C++

    yea i know u can use \n i do use it i just it just thought of using endl cuz i rarely do an thanks for the tips i was in a rush so i didnt notice i had 6 instead of 5 or 3 instead of 4
    Get At Me Bl4Ck.KiD... Aka BK
    Future Rapper - Spits Fire
    Psp Coder - Mips, C
    Web Designer - HTML, Javascript, Some PHP
    Computer Programmer - Currently Learning C/C++, Visual Basic, Pascal, Brainfuck, Python, Ruby

  6. #6
    Simple Arrays In C++

    User Info Menu

    Re: Simple Arrays In C++

    Quote Originally Posted by Bl4Ck.KiD... View Post
    yea i know u can use \n i do use it i just it just thought of using endl cuz i rarely do an thanks for the tips i was in a rush so i didnt notice i had 6 instead of 5 or 3 instead of 4
    Your homeruns array was fine.

    Now that I read over it I sound very pushy and very bad at explaining. I didn't mean to be pushy or make it hard to read. So I'm really sorry if you got that impression. I too was in a rush cause I pissed off my dad and I'm supposed to be doing homework and not internet. Like that ever stopped me! :P

    Umm... still not trying to be pushy but can you check on my account at your site, pretty pretty please? "/
    Last edited by S2h6699; 04-30-2010 at 09:25 PM. Reason: Added the main point I wanted to say. I have the attention fish of a goldspan
    Quote Originally Posted by Epic of HITB
    get... 1) a mini fridge | 2) A toilet installed in your room | 3) a closet full of non-perishable foods | 4) Lyscol ( who needs showers anyway?) | 5) tons of books articles lectures zines etc etc

    Dont leave your room...EVER... AND I MEAN EVER
    Damned six line limit! *NERD RAGE*

Tags for this Thread

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
  •