473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

User inputted length of an array without an extra variable.

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

Oct 28 '06 #1
6 2676

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 <doublenumbers(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.

Oct 28 '06 #2

fo************@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>
#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 <doublenumbers(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;
}
Oct 28 '06 #3
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.

Oct 28 '06 #4

fo************@gmail.com 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

Oct 28 '06 #5
Thanks Salt_Peter,
So what I learn of c++ in school really won't be useful?

I will get a book, thanks :)

Oct 28 '06 #6
<fo************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro 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 <doublenumbers(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<intMyVector;
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;
}
Oct 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
31
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
2
by: bryan.a.fowler | last post by:
I'm really new working with VB.net and I'm working on a simple program with a date input. I've got a couple of questions; 1) I'm using a DateTimePicker to show both the user inputted date as...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: Steve | last post by:
I am having problems with this piece of script. Usually the script works and the alert gives an array length of 9. But every so often the array length is 8 and naturally the output is then wrong....
7
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.