Simple Q on dynamic arrays

L

learnfpga

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;
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
 
V

Victor Bazarov

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
 
R

Rolf Magnus

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;
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
 
A

Andrew Koenig

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.
 
L

learnfpga

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;
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top