Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > User inputted length of an array without an extra variable.

Reply
Thread Tools

User inputted length of an array without an extra variable.

 
 
foreverbored75@gmail.com
Guest
Posts: n/a
 
      10-28-2006
Hello All!

I am just learning c++ in school and I have the following question:

Is there a way for the user to input the length of an array (console
application) without using another variable? I made this program that
finds the average of x number of numbers and stores the average in the
last index of the array of the numbers. The other other variable I have
is to determine the length of the array (totalNums). Is there a way to
get rid of it? Thanks in advance

CODE:

#include <iostream.h>
#include <apvector.h>

int main()
{
int totalNums;
cout << "Welcome to the average finder!\nHow many numbers will you be
finding the average of?: ";
cin >> totalNums;
apvector <double> numbers(totalNums + 1);
numbers[numbers.length() - 1] = 0;
for(int i=0;i<numbers.length()-1;i++)
{
cout << "Please enter the " << i+1 << " number: ";
cin >> numbers[i];
numbers[numbers.length() - 1] = numbers[i] + numbers[numbers.length()
- 1];
}
numbers[numbers.length()-1] /= numbers.length() - 1;
cout << "The average of those numbers is " << numbers[numbers.length()
- 1] << endl;
for(int k = 0;k<numbers.length();k++)
cout << "Array Index " << k << " = " << numbers[k] << endl;
return 0;
}

 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      10-28-2006

foreverbore...@gmail.com wrote:
> Hello All!
>
> I am just learning c++ in school and I have the following question:
>
> Is there a way for the user to input the length of an array (console
> application) without using another variable? I made this program that
> finds the average of x number of numbers and stores the average in the
> last index of the array of the numbers. The other other variable I have
> is to determine the length of the array (totalNums). Is there a way to
> get rid of it? Thanks in advance
>
> CODE:
>
> #include <iostream.h>
> #include <apvector.h>
>
> int main()
> {
> int totalNums;
> cout << "Welcome to the average finder!\nHow many numbers will you be
> finding the average of?: ";
> cin >> totalNums;
> apvector <double> numbers(totalNums + 1);
> numbers[numbers.length() - 1] = 0;
> for(int i=0;i<numbers.length()-1;i++)
> {
> cout << "Please enter the " << i+1 << " number: ";
> cin >> numbers[i];
> numbers[numbers.length() - 1] = numbers[i] + numbers[numbers.length()
> - 1];
> }
> numbers[numbers.length()-1] /= numbers.length() - 1;
> cout << "The average of those numbers is " << numbers[numbers.length()
> - 1] << endl;
> for(int k = 0;k<numbers.length();k++)
> cout << "Array Index " << k << " = " << numbers[k] << endl;
> return 0;
> }

Not sure what an apvector is but with a std:vector,
you can just use the push_back member function
to append elements (as they come in) to the end
of the vector.

You can get the number of elements in the array
with the size member function.

BTW, if you don't need to print out the numbers
entered, there's no need to store the numbers.
Just keep a running total (with a double) and
the number entered.

If you do store the numbers, check out the
std algorithms such as accumulate, for_each,
to help with vector processing.

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      10-28-2006

wrote:
> Hello All!
>
> I am just learning c++ in school and I have the following question:
>
> Is there a way for the user to input the length of an array (console
> application) without using another variable? I made this program that
> finds the average of x number of numbers and stores the average in the
> last index of the array of the numbers. The other other variable I have
> is to determine the length of the array (totalNums). Is there a way to
> get rid of it? Thanks in advance
>
> CODE:
>
> #include <iostream.h>
> #include <apvector.h>


#include <iostream>
#include <vector>
#include <limits> // for std::numeric_limits, see note below

>
> int main()
> {
> int totalNums;


size_t totalNums;

> cout << "Welcome to the average finder!\nHow many numbers will you be
> finding the average of?: ";
> cin >> totalNums;


// see note* below for a cool error checking mechanism

> apvector <double> numbers(totalNums + 1);


std::vector< double > numbers(totalNums);

> numbers[numbers.length() - 1] = 0;
> for(int i=0;i<numbers.length()-1;i++)
> {
> cout << "Please enter the " << i+1 << " number: ";
> cin >> numbers[i];
> numbers[numbers.length() - 1] = numbers[i] + numbers[numbers.length()
> - 1];
> }


for(size_t i = 0; i < totalNums; ++i)
{
std::cout << "Please enter numbers[";
std::cout << i << "] = ";
double d;
std::cin >> d; // see note*
numbers[i] = d;
}

*note: http://www.parashift.com/c++-faq-lite/input-output.html
read section 15.3

> numbers[numbers.length()-1] /= numbers.length() - 1;


?? use an accumulator and divide by # of elements
better yet, write a simple function that takes the std::vector
by reference
and let it do the average for you.

> cout << "The average of those numbers is " << numbers[numbers.length()
> - 1] << endl;
> for(int k = 0;k<numbers.length();k++)
> cout << "Array Index " << k << " = " << numbers[k] << endl;
> return 0;
> }


 
Reply With Quote
 
foreverbored75@gmail.com
Guest
Posts: n/a
 
      10-28-2006
The apvector.h is what we use for vectors because it has some checking
in it (I'm not sure exactly what, the teacher said it was used for the
pre-2004 AP computer Science course and the checks prevented a program
from deleting the hard drive if you wrote an array wrong.) I am not
using other functions because we have not learned how to make them (I
do know how though), and the only libraries we have learned in class is
iostream.h and the math.h and we just started vectors using the
apvector.h.

Thanks for the help though.

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      10-28-2006

wrote:
> The apvector.h is what we use for vectors because it has some checking
> in it (I'm not sure exactly what, the teacher said it was used for the
> pre-2004 AP computer Science course and the checks prevented a program
> from deleting the hard drive if you wrote an array wrong.) I am not
> using other functions because we have not learned how to make them (I
> do know how though), and the only libraries we have learned in class is
> iostream.h and the math.h and we just started vectors using the
> apvector.h.
>
> Thanks for the help though.


OK, your welcome.
Let me point out a few issues if you'll allow me.

issue #1 - length()
If the container has 5 elements then:
numbers[0] <- zero based index
numbers[1]
numbers[2]
numbers[3]
numbers[4] <- is the fifth element, count them, C++ is not VB

for( int i = 0; i < numbers.length(); ++i)
{
std::cout << numbers[i]; // <- from 0 to 4 = 5 elements
}

So the length() of the container is used as an upper limit. No length()
+1 or length() -1 should be seen in the code at all.

issue #2
Teaching iostream.h, amongst other ancient libraries, should be
strongly discouraged if not outlawed!
There is no excuse. None whatsoever. The header iostream.h is_not and
was_not ever part of C++.

issue#3
Considering the safety that a std::vector has its probably
nuclear-proof when compared to the apvector.h container. The std
containers are used in real, everyday commercial code because of their
safety, amongst other benefits.

Instead of:
std::cout << numbers[i];
you can have the std::vector carry out a range-check with:
std::cout << numbers.at(i);
Anyways, its so that you know. The above does implicate exception
checking.

I'ld kindly suggest reading the faq and consider getting a real book,
not any book.
http://www.parashift.com/c++-faq-lite/newbie.html

 
Reply With Quote
 
foreverbored75@gmail.com
Guest
Posts: n/a
 
      10-28-2006
Thanks Salt_Peter,
So what I learn of c++ in school really won't be useful?

I will get a book, thanks

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-29-2006
<> wrote in message
news: ups.com...
> Hello All!
>
> I am just learning c++ in school and I have the following question:
>
> Is there a way for the user to input the length of an array (console
> application) without using another variable? I made this program that
> finds the average of x number of numbers and stores the average in the
> last index of the array of the numbers. The other other variable I have
> is to determine the length of the array (totalNums). Is there a way to
> get rid of it? Thanks in advance
>
> CODE:
>
> #include <iostream.h>
> #include <apvector.h>
>
> int main()
> {
> int totalNums;
> cout << "Welcome to the average finder!\nHow many numbers will you be
> finding the average of?: ";
> cin >> totalNums;
> apvector <double> numbers(totalNums + 1);
> numbers[numbers.length() - 1] = 0;
> for(int i=0;i<numbers.length()-1;i++)
> {
> cout << "Please enter the " << i+1 << " number: ";
> cin >> numbers[i];
> numbers[numbers.length() - 1] = numbers[i] + numbers[numbers.length()
> - 1];
> }
> numbers[numbers.length()-1] /= numbers.length() - 1;
> cout << "The average of those numbers is " << numbers[numbers.length()
> - 1] << endl;
> for(int k = 0;k<numbers.length();k++)
> cout << "Array Index " << k << " = " << numbers[k] << endl;
> return 0;
> }


Is this what you wanted? Enter some non integer to end (such as x)

int main( void )
{
std::vector<int> MyVector;
int i;
while ( std::cin >> i )
MyVector.push_back( i );

std::cout << "You entered " << MyVector.size() << " values." <<
std::endl;

std::cin.clear();
std::cin.ignore(50, '\n');

std::string wait;
std::getline( std::cin, wait );

return 0;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
Push Inputted Words Scott Andrechek Ruby 7 06-29-2009 01:57 PM
Get length of returned array without storing? boole Perl Misc 11 11-06-2007 05:49 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57