C++ Epiphany

Discussion in 'Computer Games and General Discussion' started by Sterling, Sep 26, 2010.

Sep 26, 2010

C++ Epiphany by Sterling at 4:04 AM (862 Views / 0 Likes) 6 replies

  1. Sterling
    OP

    Member Sterling GBAtemp's Silver Hero

    Joined:
    Jan 22, 2009
    Messages:
    4,023
    Location:
    Texas
    Country:
    United States
    So, I have taken a class on C++ and for my second assignment I an required to ask for user input and based upon said input, I calculate the average and output it to the console. I did a simple main() iteration, and I wasn't satisfied. So I decided to take a concept of an undefined sized array which stores an [in]finite amount of entries, and when prompted takes the entries and calculates the average. A concept that was fairly easily implemented on JAVA.

    Flow Chart:

    1) Variable initialization

    2) Ask user for input to be stored in an array whose size is defined by a conditional infinite break loop (It's a for loop, but I think it could be done with a Do while loop).

    3) When loop is broken, recall entries and add them together for the sum.

    4) Calculate the average based on the loop variable.

    5) Output

    The first thing I learned is the fact that this isn't as easy as it was in Java. I had to first figure out how to bypass the fact that array initialization cannot be innately undefined. I had to create a pointer value and get familiar with the concept of dynamic memory. After I figured that out it was just a matter of time.

    Code:
    CODE/*
    *Sterling Schneider
    *Culbertson
    *Lab 2: Challenge
    *Using arrays and loops and functions
    */

    #include
    #include

    using namespace std;

    int Exit = 0;


    int main()
    {

    int Sum = 0;
    int Ave = 0;
    int e = 0;
    int n = 1;
    int * Grades;

    //*********//

    Grades = new int [n];
    cout
     
  2. ThatDudeWithTheFood

    Member ThatDudeWithTheFood TRIANGLEZ

    Joined:
    Mar 9, 2009
    Messages:
    2,198
    Location:
    Illuminati
    Country:
    United States
    Good job...?

    That was probably the most interesting thing I've read all day.
     
  3. Sterling
    OP

    Member Sterling GBAtemp's Silver Hero

    Joined:
    Jan 22, 2009
    Messages:
    4,023
    Location:
    Texas
    Country:
    United States
    Thanks. I'll accept that if it isn't sarcasm. ^.^

    Took maybe an hour to sort through several google searches and tutorial sites. It could be better, but I have a feeling that my teacher may pat me on the back in it's current condition.
     
  4. Joe88

    Member Joe88 [λ]

    Joined:
    Jan 6, 2008
    Messages:
    11,179
    Location:
    NYC
    Country:
    United States
    why is exit a global variable
    you never leave main
     
  5. Justin121994

    Member Justin121994 Bitcoin. coinbase.com/andrew

    Joined:
    Jan 12, 2009
    Messages:
    808
    Location:
    Ontario, CA
    Country:
    United States
    Use functions to clear out main dude [​IMG].
    Other than my limited knowledge i'd say it's good.
     
  6. Sterling
    OP

    Member Sterling GBAtemp's Silver Hero

    Joined:
    Jan 22, 2009
    Messages:
    4,023
    Location:
    Texas
    Country:
    United States
    Believe me when I say that I tried. Still haven't grasped the knowledge of functions very well. :/
     
  7. Justin121994

    Member Justin121994 Bitcoin. coinbase.com/andrew

    Joined:
    Jan 12, 2009
    Messages:
    808
    Location:
    Ontario, CA
    Country:
    United States
    Oh then thats good.
    I'm a lil farther.
    I'm just stuck with school work.. Stupid ap classes, that I have no time for it.
    Well good luck.
    http://www.learncpp.com/
    Heres a site I've been using for c++ it's really good.
     

Share This Page