Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,527 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,984 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Vectors

2 Pages V  1 2 >  
Reply to this topicStart new topic

Vectors

dwd3773
post 2 Jul, 2008 - 04:06 PM
Post #1


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


I have to do the following assignment for school, and I cant seem to figure out how to get started. I'm going to continue to serch through th forums and do some google searches, but I wanted to go ahead and post to get ahead start. These are the guidelines for the assignment:


1.Create an empty vector

2.Add values that the user inputs to the vector until the user enters "0.0"

3.After a "0.0" is entered, the user is done adding values and the vector is "finished"

4.Print the values in the vector to the screen

5.Loop through each of those values and find both the average of all the values and the value that is the highest number and output that information to the screen
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 2 Jul, 2008 - 07:47 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,604



Thanked 116 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Pieces you are going to need...

1) #include <vector>
2) A loop to prompt the user continuously for a float value
3) A float variable to hold their input
4) A vector variable needs to be defined
5) The push_back method of the vector
6) A vector<float>::iterator variable.
7) A second loop to iterator across the vector and add them to a sum variable.
8) size() function of the vector (sum / size() = avg)
9) A variable to hold the "max" value. Hint: Initialize it to the first value in the vector and compare all other values against it.

That should be enough to get you started. Enjoy!

"At DIC we be vector wielding code ninjas... we also wielded 'Victor' and threw him out the window. Lamahhhh" decap.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 02:29 AM
Post #3


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,372



Thanked 68 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


QUOTE(Martyr2 @ 3 Jul, 2008 - 03:47 AM) *
"At DIC we be vector wielding code ninjas... we also wielded 'Victor' and threw him out the window. Lamahhhh" decap.gif

I'll have whatever you're having.
User is online!Profile CardPM

Go to the top of the page

dwd3773
post 3 Jul, 2008 - 11:30 AM
Post #4


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


Do I need to use a header file or can all this be carried out in one .cpp file?
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 11:36 AM
Post #5


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,372



Thanked 68 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


I can't think of a reason as to why you would need to use a header file...
User is online!Profile CardPM

Go to the top of the page

dwd3773
post 3 Jul, 2008 - 01:00 PM
Post #6


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


Ok I got started but I'm still a little confused about how to get the average and the highest number value. Here is the coe so far:

CODE

#include <vector>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    vector<float> input_numbers;
    float number;
    char answer;

        cout << "Do you want to enter numbers Y/N?";
        cin >> answer;

        while (answer == 'Y')
        {
            cout << "Enter value: ";
            cin >> number;

                // Now that we have the number from the user,
                // append it at the end of the vector

            input_numbers.push_back(number);

            cout << "Do you want to enter more numbers (y/n)? ";
            cin >> answer;

            cin.get();
        }


        for (int i = 0; i < input_numbers.size(); i++)
        {
            cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
        }
                // '\t' is the code for the TAB character

        cin.get();

        return 0;


}

User is offlineProfile CardPM

Go to the top of the page

KYA
post 3 Jul, 2008 - 01:49 PM
Post #7


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 3,087



Thanked 23 times

Dream Kudos: 1150
My Contributions


Check this out (need to iterate over the vector and add it for the average and there are many many ways to sort it, I chose the simplest one I could think of to illustrate the power of STL built in container functions):

cpp

#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

bool sortFunction(int i, int j) {return (i > j); }

int main()
{
vector<float> input_numbers;
float number, average = 0, total = 0, totalNums = 0, highest = 0;
char answer;

cout << "Do you want to enter numbers Y/N?";
cin >> answer;

while (answer == 'Y')
{
cout << "Enter value: ";
cin >> number;

// Now that we have the number from the user,
// append it at the end of the vector

input_numbers.push_back(number);
totalNums++;

cout << "Do you want to enter more numbers (y/n)? ";
cin >> answer;

cin.get();
}


for (int i = 0; i < input_numbers.size(); i++)
{
cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
}
// '\t' is the code for the TAB character

//iterate over and add all values to get average number
for (int i = 0; i < input_numbers.size(); i++)
{
total += input_numbers[i];
}

//Neat little built in sort thing, see function at top
sort(input_numbers.begin(), input_numbers.end(), sortFunction);
//output sorted list for debugging purposes
cout << "\nSorted vector:\n";
for (int i = 0; i < input_numbers.size(); i++)
{
cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
}
average = total/totalNums;
highest = input_numbers[0];
cout << "\nAverage of this vector is: " << average;
cout << "\nHighest number in the vector is: " << highest;
cin.get();

return 0;
}
User is online!Profile CardPM

Go to the top of the page

dwd3773
post 3 Jul, 2008 - 02:55 PM
Post #8


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


Ok got everything to work except for the average. For some reason it keeps giving me the same output no matter how many inputs or what they are.

CODE

#include <vector>
#include <iostream>
#include <cmath>
#include <cctype>
#include <algorithm>

using namespace std;

bool sortFunction(int i, int j)
    {    
        return (i > j);
    }

int main()
{
    vector<float> input_numbers;
    float number;
    char answer;
    float max;
    float highest;
    float total = 0.0;
    float totalNums = input_numbers.size();
    float average;


        cout << "Do you want to enter numbers Y / N ";
        cin >> answer;
    
        while (answer == 'Y')
        {
            cout << "Enter value: ";
            cin >> number;

                // Now that we have the number from the user,
                // append it at the end of the vector

            input_numbers.push_back(number);

            cout << "Do you want to enter more numbers (Y/N)? ";
            cin >> answer;

            cin.get();
        }

        for (int i = 0; i < input_numbers.size(); i++)
        {
            cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
        }
                // '\t' is the code for the TAB character

               //iterate over and add all values to get average number  
        for (int i = 0; i < input_numbers.size(); i++)  
        {  
            total += input_numbers[i];  
        }  
  
        //Neat little built in sort thing, see function at top  
        sort(input_numbers.begin(), input_numbers.end(), sortFunction);  
        //output sorted list for debugging purposes  
        cout << "\nSorted vector:\n";  
        for (int i = 0; i < input_numbers.size(); i++)  
        {  
            cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;  
        }  
        average = total/totalNums;  
        highest = input_numbers[0];  
        cout << "\nAverage of this vector is: " << average;  
        cout << "\nHighest number in the vector is: " << highest;
        cin.get();

        return 0;


}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 03:07 PM
Post #9


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,372



Thanked 68 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


You define totalNums before filling the vector, so it will always equal 0.

To be honest, you don't even need totalNums:
cpp

#include <vector>
#include <iostream>
#include <cmath>
#include <cctype>
#include <algorithm>

using namespace std;

bool sortFunction(int i, int j)
{
return (i > j);
}

int main()
{
vector<float> input_numbers;
float number;
char answer;
float max;
float highest;
float total = 0.0;
float average;


cout << "Do you want to enter numbers Y / N ";
cin >> answer;

while (answer == 'Y')
{
cout << "Enter value: ";
cin >> number;

// Now that we have the number from the user,
// append it at the end of the vector

input_numbers.push_back(number);

cout << "Do you want to enter more numbers (Y/N)? ";
cin >> answer;

cin.get();
}

for (int i = 0; i < input_numbers.size(); i++)
{
cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
}
// '\t' is the code for the TAB character

//iterate over and add all values to get average number
for (int i = 0; i < input_numbers.size(); i++)
{
total += input_numbers[i];
}

//Neat little built in sort thing, see function at top
sort(input_numbers.begin(), input_numbers.end(), sortFunction);
//output sorted list for debugging purposes
cout << "\nSorted vector:\n";
for (int i = 0; i < input_numbers.size(); i++)
{
cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
}
average = total/input_numbers.size();
highest = input_numbers[0];
cout << "\nAverage of this vector is: " << average;
cout << "\nHighest number in the vector is: " << highest;
cin.get();

return 0;


}

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

dwd3773
post 3 Jul, 2008 - 03:20 PM
Post #10


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


Thank you for all your help. The program runs great, but I think I'm gonna have to make another change. I sent my instructor an email asking to be able to use the Yes/No option for inputing numbers into the vector. He said no, that I needed to use a loop to keep asking for input numbers until the user inputs 0.0
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 03:25 PM
Post #11


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,372



Thanked 68 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


No problem, just do something like:
cpp
do {
cin >> number;
// whatever else needs to be in the loop
} while (number != 0.0);

smile.gif
User is online!Profile CardPM

Go to the top of the page

dwd3773
post 3 Jul, 2008 - 03:25 PM
Post #12


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 100


My Contributions


Ok here is code with the instructed change:

CODE

#include <vector>
#include <iostream>
#include <cmath>
#include <cctype>
#include <algorithm>

using namespace std;

bool sortFunction(int i, int j)
    {    
        return (i > j);
    }

int main()
{
    vector<float> input_numbers;
    float number;
    char answer;
    float highest;
    float total = 0.0;
    float average;


    cout << "Please enter your number: If no other number is to be entered, Enter 0 " << endl;
    cin >> number;
    
        while (number != 0)
        {

            input_numbers.push_back(number);

            cout << "Please enter your number: If no other number is to be entered, Enter 0 " << endl;
            cin >> number;

            cin.get();
        }

        for (int i = 0; i < input_numbers.size(); i++)
        {
            cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;
        }
                // '\t' is the code for the TAB character

               //iterate over and add all values to get average number  
        for (int i = 0; i < input_numbers.size(); i++)  
        {  
            total += input_numbers[i];  
        }  
  
        //Neat little built in sort thing, see function at top  
        sort(input_numbers.begin(), input_numbers.end(), sortFunction);  
        //output sorted list for debugging purposes  
        cout << "\nSorted vector:\n";  
        for (int i = 0; i < input_numbers.size(); i++)  
        {  
            cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl;  
        }  
        average = total/input_numbers.size();  
        highest = input_numbers[0];  
        cout << "\nAverage of this vector is: " << average;  
        cout << "\nHighest number in the vector is: " << highest;
        cin.get();

        return 0;


}

User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 10/7/08 03:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month