![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 4
| User input for array size. 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? |
| coolmoniker is offline | |
| | #2 |
| Registered User Join Date: May 2006
Posts: 894
| Yes there is and it is called dynamic memory allocation. Code: std::cin >> n; int* salaryarray = new int[n]; |
| Desolation is offline | |
| | #3 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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
__________________ 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. |
| dwks is offline | |
| | #4 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| I think C99 standard allows this Code: cin >> n; int salaryarray[ n ]; Code: cin >> n; vector<int> salaryarray(n); |
| Ancient Dragon is offline | |
| | #5 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Yes, the C99 standard allows variable-sized arrays. [edit] Code: cout << "Please enter the number of salespeople./n";
__________________ 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. Last edited by dwks; 08-24-2006 at 03:39 PM. |
| dwks is offline | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| 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. |
| Daved is offline | |
| | #7 |
| Registered User Join Date: May 2006
Posts: 894
| For such a small need I think using a vector would be like shooting a fly with a canon. |
| Desolation is offline | |
| | #8 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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. |
| dwks is offline | |
| | #9 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| >> 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? |
| Daved is offline | |
| | #10 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,656
| >> 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. |
| Mario F. is offline | |
| | #11 | |
| System Novice Join Date: Jan 2006 Location: Tehran
Posts: 1,075
| Quote:
__________________ Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2) Learn the language before using it. (C++ Books and C Books) Read the FAQ before making a problem. Then make a Google and Forum search. My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!) SiavoshKC | |
| siavoshkc is offline | |
| | #12 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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. |
| dwks is offline | |
| | #13 | |
| Registered User Join Date: May 2003
Posts: 1,199
| Quote:
Code: int * a = new int[10]; int * b = new int[10]; delete[] a; delete[] b;
__________________ 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. | |
| Cat is offline | |
| | #14 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| >> 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. |
| Daved is offline | |
| | #15 |
| System Novice Join Date: Jan 2006 Location: Tehran
Posts: 1,075
| Vector is slower than new/delete? Why?
__________________ Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2) Learn the language before using it. (C++ Books and C Books) Read the FAQ before making a problem. Then make a Google and Forum search. My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!) SiavoshKC |
| siavoshkc is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| displaying user input values of an array by calling functions... PLEASE HELP ME!!! | hiddenprophecy | C Programming | 12 | 04-12-2009 05:49 PM |
| timed user input | sainiabhishek | C Programming | 4 | 04-01-2009 11:59 AM |
| Truncating user input | CS_Student8337 | C Programming | 10 | 03-19-2009 12:34 AM |
| MFC: need varying number of user input files - best interface? | BrianK | Windows Programming | 4 | 04-21-2004 04:18 PM |
| problem: reading user input into an array | alpha561 | C Programming | 13 | 05-24-2002 07:23 PM |