Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2005, 10:39 PM   #1
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
A problem

Hi all,

I'm a beginner of C++. I'd like to ask how I could input an integer n, and after running the program, return an array with length n.
quantalfred is offline   Reply With Quote
Old Jan 31st, 2005, 2:42 AM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
void main( )
{
  int n = 0;
  cin >> n;

  char *ptr = new char( n );
  return ptr; // WTF?
}

Just for the record, that will not return the array though... applications only return numeric values... not arrays. Perhaps you mean have a function which returns the array? Well that demonstrates how to do it, just remember to release you pointer when you are done with it.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Jan 31st, 2005, 7:14 AM   #3
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
In fact I just want to input an integer n and then I can form an array with length n which can be used in the main program.
quantalfred is offline   Reply With Quote
Old Jan 31st, 2005, 3:12 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Post

I believe you can use this:

int i;
scanf("d", &i); // replace this with cin or whatever
int array[i];
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 31st, 2005, 4:47 PM   #5
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4 Xero is on a distinguished road
Quote:
Originally Posted by Mjordan2nd
I believe you can use this:

int i;
scanf("d", &i); // replace this with cin or whatever
int array[i];
I dont think u can do that... needs to be a constant number, I believe

You would have to use dynamitc arrays:
int i;
cin >> i;
int *arr_ptr = new int [i];

errr... return it after the program???
__________________
...
Xero is offline   Reply With Quote
Old Jan 31st, 2005, 4:49 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You can't define an array with a variable length. You can, however, use memory allocation to do the same thing:
int i;
 cin >> i;
 int *array = new int[i];

At the end of the program, you then have to free the memory. Use:
delete [] array;

You should really know how to work pointers before you mess with this though.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 31st, 2005, 4:50 PM   #7
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4 Xero is on a distinguished road
Quote:
Originally Posted by Ooble
You can't define an array with a variable length. You can, however, use memory allocation to do the same thing:
int i;
cin >> i;
int array = new int[i];
(I think that's right.)
u missed the pointer derefference *

haha beat u by 10 seconds or so
__________________
...
Xero is offline   Reply With Quote
Old Jan 31st, 2005, 4:55 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Heh. Forgot about it until I'd posted.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 31st, 2005, 5:10 PM   #9
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Heh. Guess you guys are correct. I've been getting too used to Java.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 31st, 2005, 10:14 PM   #10
quantalfred
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 quantalfred is on a distinguished road
Question follow up question

Thanks all for replying. But if I want to creat a two-dimensional array, how could I modify the code?
cin >> n;
double *array = new array[n][n];

delete array;

It seems that it doesn't work...
quantalfred is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC