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