Basic Programming help

Discussion in 'Web Design & Coding' started by Ohzopants, Feb 26, 2004.

  1. Ohzopants

    Ohzopants My mom thinks I'm cool

    Messages:
    165
    Location:
    Montreal
    Hey guys...

    I have this assignment due next week and I have little to no programming experience. My major problem is: How do i create an array whose size depends on a number of user inputs?

    I can't post any of the code I've tried because most of it won't compile and whatever does just produces junk.

    Please help guys...
     

    Attached Files:

  2. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    Messages:
    3,763
    Location:
    London, UK
    Make your subscripts variables - you'll need to check the parameters users input for limits of your platform/language too - and your language will have to support this feature or you're going to have to do something more complex as a workaround which I haven't time to go into here.

    If this makes no sense to you then you're just gonna have to bite the bullet and study a bit :p
     
  3. sboulema

    sboulema Moderator

    Messages:
    2,846
    Location:
    Amstelveen, The Netherlands
    only know how to do this in visual basic.

    dim variable()

    collect user inputs in temp variables
    once you know the final number of inputs

    redim variable(number inputs)
    transfer to the temp variables to the array

    something like that.
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    What language?
     
  5. Hsn

    Hsn King

    Messages:
    1,082
    Location:
    Calgary, Canada
    its C++, its in the attahcment.
     
  6. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    coulda helped no problem were it java, though basically you just try catch and if you try to write to an array index which is out of bounds you catch it and then double or increase the array size, by making a copy of the working array but with a few more spaces, copy the data from the working array into the new larger array and then start working with the new array
     
  7. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    I am going to give Geffy all my Java homework and see if he can do it :D as a test... yeah as a test, just to see if he's as good as he says
     
  8. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    feck off, I got Class Diagrams to do for sheep as coursework for Internet Computing Fundamentals, a SMIL Presentation of my department for Creative Media Technology and a Library System to write in Java for Object Oriented Programming and Design
     
  9. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    do you want some cheese with that whine :D
     
  10. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    I just read the attachment, had no Adobe or PDF reader on the other PC i was on.

    And actually i has to be in C. If it were in C++, it would be easy.

    You would be able to use:

    std::vector, this is basically an array that can be increased as much as you want


    For C, youd need to know how to use dynamic memory allocation schemes, and how to free it. It is quite a bit harder.

    Here is some sample C++ code on how it would be done in C++. I am not a good C programmer, so i cant write you a program in C.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    
    using std::cin;
    using std::cout;
    
    int main()
    {
            std::vector<float> fltVector;
            std::string input("x");
    
            cout << "Usage: \n\tKeep entering a number, this includes decimals, when you have entered all the numbers, type an \"x\"\n\n(c) Copyright X-Istence.com\n\n";
    
            // We run the while loop forever
            while (1)
            {
                    // Get our first input.
                    cout << "Please enter a number: ";
                    cin >> input;
    
                    // If the input is empty, we end this loop, and go on to the rest of the program
                    if (input == "x" || input == "X")
                            break;
    
                    // Add the number to the float vector.
                    fltVector.push_back(atof(input.c_str()));
            }
    
            // User decided they have inputted enough info
    
            // We need an iterator to be able to go over the entire vector
            std::vector<float>::iterator iter;
    
            // Create another float to contain the total, so we can get the mean out of it.
            // I have no clue what variance/deviation is, so ill just keep it to the mean.
            float total = 0;
    
            for (iter = fltVector.begin(); iter != fltVector.end(); iter++)
                    // Add the amount that the vector has to total.
                    total += *iter;
    
            // A bit of output can not hurt us
            cout << "\n\n\nYou have entered " << fltVector.size() << " items.";
            cout << "\n\nThe mean of the numbers you have entered is " << total / fltVector.size() << "\n";
    
    return 0;
    }
    Example:

     
  11. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    I do not know the limits of the type "float", and i do know that it should not be used all that often as it is really not that portable.

    There is thus a possibility of a buffer overflow.
     
  12. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    you can use the sizeof() function to see the limit of float, or you can just go with a double which is set to 64 bits on x86 cpu's (I think, I may be wrong)
     
  13. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    sizeof() will show different results for different OS's on a float, which is why its not really all that portable.

    The document stated that i had to use float, otherwise i would have indeed just rather gone with double.
     
  14. ignipotentis

    ignipotentis OSNN Addict

    Messages:
    127
    I know the assignment is over, but for something as simple as this I'm supprised everyone overcomplicated it. Create a function called array_increment. This is going to be process intensive, but since the user is manually entering in values one at a time it isn't that big of a deal. This function just increases an array size by one each time it is called.