By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,590 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,590 IT Pros & Developers. It's quick & easy.

Simple Q on dynamic arrays

P: n/a
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....
//trying to get input from the user to add all the numbers that user
inputs
//tried to do it without dynamic memory usage but probably cannot
achieve it
//here is using "new" and "delete" operator....
#include <iostream>
using namespace std;

int main()
{
int i,n,sum=0;;
int *p;
cout<<"How many numbers would you like to type ";
cin>>i;
p= new (nothrow) int[i];
if (p == 0) //to check the value returned by nothrow when allocating
memory
cout<<"Error in allocating memory ";
else
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
{
cout << p[n] << ", ";
sum=sum+(*(p+n));
}
cout <<"\nSum of numbers entered by you is "<<sum<<endl;
delete[] p;

return 0;
}

Thanks and cheers

May 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
le*******@gmail.com wrote:
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same
functionality without using dynamic arrays.....just curious.....
Yes, if you declare an array large enough for any user to ever want
or limit the capability of your program to that size.
[...]


Some might suggest that you could accomplish the same thing "without"
using dynamic arrays if you used a standard container, std::vector
for example. However, the reason I used quotes around 'without' is
that std::vector uses dynamic allocation behind the scenes, so you
can't claim complete absence of any dynamic arrays. Of course, if
you use std::list, there is likely no arrays there, and formally you
would do it without any arrays at all.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '06 #2

P: n/a
le*******@gmail.com wrote:
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....
You can use a vector. It will still use dynamic memory, but it automatically
handles it for you.
//trying to get input from the user to add all the numbers that user
inputs
//tried to do it without dynamic memory usage but probably cannot
achieve it
//here is using "new" and "delete" operator....
#include <iostream>
using namespace std;

int main()
{
int i,n,sum=0;;
int *p;
cout<<"How many numbers would you like to type ";
cin>>i;
p= new (nothrow) int[i];
if (p == 0) //to check the value returned by nothrow when allocating
memory
Why not use the regular new operator and catch the exception?
cout<<"Error in allocating memory ";
else
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
{
cout << p[n] << ", ";
sum=sum+(*(p+n));
}
cout <<"\nSum of numbers entered by you is "<<sum<<endl;
delete[] p;

return 0;
}

Thanks and cheers


May 1 '06 #3

P: n/a
<le*******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....


Let me restate the problem.

You want to

1) Read a sequence of numbers of unknown length.
2) Print all the numbers in the order in which they were read.
3) Print their sum.

Obviously, in order be able to do (1) and then (2), you need enough memory
to store all of the numbers. A dynamic array is one way of obtaining that
memory. I can think of two other ways of doing so, one of which does not
involve any explicit dynamic memory allocation at all. However, I think
this may be a homework assignment, so I am reluctant to explain what they
are.
May 1 '06 #4

P: n/a
First of all thanks for a response and I am not a student working on
his homework. So rest assured.......

Actually I am a newbie and was trying to do this a couple of weeks ago
but could not do so as I did not know about dynamic arrays. Today I
learnt about them and was able to write this code but I was wondering
if there are other methods to do the same thing without using fancy
things like dynamic array or container class "vector"........

I wrote a variation of the above code where I basically declared an
array of a certain size [20] and if the user wants an addition of
greater than [20] numbers than program exits. (Slight problem with this
is how can I make the program to exit without giving garbage......

if (i > 20) //to check the value returned by nothrow when allocating
memory
{
cout<<"\nToo big....Specify smaller size "<<endl;
/* I want the program to exit......but right
now if I give i=21 I get all garbage and then the program exits because
it basically is running the whole program */
}

Here is the code using arrays

//trying to get input from the user to add all the numbers that user
inputs
//tried to do it without dynamic memory usage but probably cannot
achieve it
//here is using "new" and "delete" operator....

//removing new and delete operator.....trying not to use dynamic array
#include <iostream>
using namespace std;

int main()
{
int i,n,sum=0;;
int p[20]; //Declare array of size 20 so user cannot add more than 20
numbers
cout<<"How many numbers would you like to type ";
cin>>i;
// p= int[i];
if (i > 20) //to check the value returned by nothrow when allocating
memory
{
cout<<"\nToo big....Specify smaller size "<<endl;

}

else
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
{
cout << p[n] << ", ";
sum=sum+p[n];
}
cout <<"\nSum of numbers entered by you is "<<sum<<endl;
return 0;
}

May 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.