User inputted length of an array without an extra variable.

Discussion in 'C++' started by foreverbored75@gmail.com, Oct 28, 2006.

  1. Guest

    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;
    }
     
    , Oct 28, 2006
    #1
    1. Advertising

  2. Guest

    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;
    > 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.
     
    , Oct 28, 2006
    #2
    1. Advertising

  3. Salt_Peter Guest

    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;
    > 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;
    > }
     
    Salt_Peter, Oct 28, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. Salt_Peter Guest

    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; // <- 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
     
    Salt_Peter, Oct 28, 2006
    #5
  6. Guest

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

    I will get a book, thanks :)
     
    , Oct 28, 2006
    #6
  7. Jim Langston Guest

    <> wrote in message
    news:...
    > 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;
    }
     
    Jim Langston, Oct 29, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Warner

    Flexible array member + variable length array

    Adam Warner, Feb 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    803
    S.Tobias
    Feb 10, 2005
  2. mathieu
    Replies:
    3
    Views:
    600
    Bo Persson
    Sep 4, 2009
  3. Scott Andrechek

    Push Inputted Words

    Scott Andrechek, Jun 29, 2009, in forum: Ruby
    Replies:
    7
    Views:
    97
    John W Higgins
    Jun 29, 2009
  4. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    478
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page