Vectors for College Class

**Hey, I am rusty on C++ and my college professor just dropped this question to us. He barley brushed on the topic and I have no idea how to do vectors. Can anyone walk me through this program? When I look up resources online I don't understand.**




Please write a complete C++ code to perform the following tasks:(1)Read a sequence of double values into a vector. Think of each value as the distance between two cities along a given route. (2)Compute and print the total distance (the sum of all distances). (3)Find and print the smallest and greatest distance between two neighboring cities. (4)Find and print the mean distance between two neighboring cities.
Hello MrIDKWHATIMDOING,

Have you written any code yet?

I would start with this first:
(1) Read a sequence of double values into a vector.
Do not worry about the vector part for now just get your input.

Should you be getting your input from a file post the file or a good sample to work with. This way everyone is using the same information.

Even if your input overwrites the same variable for now that is OK. Adding the vector is easy to walk you through.

One question that came to me is do you need to deal with just a single number or is it part of something bigger, I.E., Would you need a struct to deal with more than just the number input?

This may be of some help:
http://www.cplusplus.com/reference/vector/vector/
https://www.learncpp.com/cpp-tutorial/6-16-an-introduction-to-stdvector/

Andy
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;


int main()

{
vector<double> dist;

double sum = 0; // accumalte tolat distance in sum
double min = 0; // smallest distance
double max = 0; // largest distance


}


honestly i got this far from a friend
Hello MrIDKWHATIMDOING,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



That is a good start. You need one more variable to start with. I would most likely call it "num", but you are free to choose whatever name you like.
1
2
3
4
5
6
7
8
double num{};   // Used for input;
double sum = 0; // accumulate total distance in sum
double min = 0; // smallest distance
double max = 0; // largest distance

// Add code here for input of your numbers.

// Get this started. The vector part is easy to add later. 

I find that getting the input first works best because then you have something to work with and the rest tends to fall in place.

Look at the whole the "sum" can not be computed until you have the vector and the vector can not be created until you have a way to get the numbers.

Andy
look at an example of push_back for vectors in the tutorials and it will get you 90% of the way done.
Thanks for the helps guys!
Topic archived. No new replies allowed.