Thread: User input for array size.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    User input for array size.

    Hello.


    I haven't done any programming in since I took CS1100 last year. Now I am taking CS1110 and I don't want to get owned, so I am trying to practice some exercises from the book before my professor assigns anything.


    So I am currently stuck with this part of a program--I want to be able to creat an array, the size of which is based upon the input of the user.


    This is what I have right now. It doesn't work for obvious reasons but I can't figure anything out to work around it. (I don't know why the indentation is all weird)


    Code:
    		
    
    cout << "Please enter the number of salespeople./n";
                    int n;
    	cin >> n;
    	const int * const sPtr = &n;
    	int salaryarray[ *sPtr ];

    I have never used pointers until today, and I am very rusty with arrays. I understand that arrays have to have a constant value as their size. But there has to be a way to make that size be specified by the user. Right?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yes there is and it is called dynamic memory allocation.
    Code:
    std::cin >> n;
    int* salaryarray = new int[n];
    The last line creates a pointer to hold the address of memory allocated by the 'new' operator and then you need to specify the type (so that the compiler knows the size of the type) and in the brackets the number of elements of that type to allocate. When allocating with new[], deallocate using delete[] and the same thing if you allocate with new, you'll want to use delete.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, use dynamic memory allocation.

    Code:
    int size, *array;
    
    cout << "How many? ";
    cin >> size;
    
    array = new int[size];
    
    for(int x = 0; x < size; x ++) {
        cout << "Enter number " << x << ": ";
        cin >> array[x];
    }
    
    for(int x = 0; x < size; x ++) {
        cout << array[x] << endl;
    }
    
    delete [] array;  // Don't forget to free the memory
    [edit] Two minutes too late . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I think C99 standard allows this
    Code:
    	cin >> n;
    	int salaryarray[ n ];
    or, since this is a c++ program, you might use a vector
    Code:
    	cin >> n;
    	vector<int> salaryarray(n);

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, the C99 standard allows variable-sized arrays.

    [edit]
    Code:
    cout << "Please enter the number of salespeople./n";
    BTW, I think you meant "\n". [/edit]
    Last edited by dwks; 08-24-2006 at 03:39 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In C++ you should really use the standard vector class for these types of things, although if your professor teaches C style arrays you should learn them as well.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    For such a small need I think using a vector would be like shooting a fly with a canon.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not really. A vector doesn't store any pointers for each element (the memory is just a contiguous chunk, like you would get with a dynamically allocated array). And I can't imagine it would impact execution speed very much.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> For such a small need I think using a vector would be like shooting a fly with a canon.
    It's your opinion, but why are you even using C++ then?

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    >> For such a small need I think using a vector would be like shooting a fly with a canon.

    Or using an array would be like trimming your nails with a butchers knife. And I do echo Daved's question.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    For such a small need I think using a vector would be like shooting a fly with a canon.
    Why you think so?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Because a vector is slower and uses more memory than an array. But its advantages far outweigh its disadvantages. (We're using C++, right?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Desolation
    For such a small need I think using a vector would be like shooting a fly with a canon.
    I tend to disagree. Using pointers in general requires careful planning to avoid memory leaks. Even a simple piece of code like this:

    Code:
    int * a = new int[10];
    int * b = new int[10];
    delete[] a;
    delete[] b;
    has one possible scenario where it leaks memory.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Because a vector is slower and uses more memory than an array.
    That's not a sufficient reason (even if it were true), unless you would say using C++ instead of C, or C instead of assembly is like shooting a fly with a cannon.

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Vector is slower than new/delete? Why?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM