User inputted length of an array without an extra variable.

F

foreverbored75

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;
numbers[numbers.length() - 1] = numbers + 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;
}
 
A

AnonMail2005

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;
numbers[numbers.length() - 1] = numbers + 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.
 
S

Salt_Peter

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

foreverbored75

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

Salt_Peter

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; // <- 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;
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
 
F

foreverbored75

Thanks Salt_Peter,
So what I learn of c++ in school really won't be useful?

I will get a book, thanks :)
 
J

Jim Langston

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;
numbers[numbers.length() - 1] = numbers + 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;
}
 

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,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top