473,387 Members | 1,517 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,387 software developers and data experts.

Vector Sum and Median Trouble

I'm supposed to write a program in which a user will input a list of numbers into a vector. the program will run and...
  1. output the list of numbers put into the vector
  2. identify the highest and lowest number in the list
  3. find the sum of the numbers
  4. and find the median of the list

i was able to get it to this but i still dont know how to add the average formula and the median formula to the code

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6.     std::vector <double> values;
  7.     std::cout<<"Enter Values, 0 To Quit:\n";
  8.     bool more= true;
  9.     while(more){
  10.         double s;
  11.         std::cin>>s;
  12.         if(s==0)
  13.             more=false;
  14.         else
  15.             values.push_back(s);
  16.     }
  17.     double highest= values[0];
  18.     double lowest= values[0];
  19.     int i;
  20.     for(i=1;i<values.size(); i++)
  21.         if(values[i]>highest)
  22.             highest=values[i];
  23.         else if(values[i]<lowest)
  24.             lowest=values[i];
  25.  
  26.     for(i=0; i<values.size(); i++)
  27.     {
  28.         std::cout<<values[i];
  29.         if(values[i]==highest)
  30.             std::cout<<" <== Highest Value";
  31.         if(values[i]==lowest)
  32.             std::cout<<" <== Lowest Value";
  33.         std::cout<<"\n";
  34.     }
  35.  
  36. { if (values.size() == 0) return 0;
  37.     double sum = 0;
  38.     for (int i = 0; i < values.size(); i++)
  39.         sum = sum + values[i];
  40.     return sum / values.size();}
  41.     return 0;
  42.  
  43.  
  44. }
Dec 5 '14 #1
3 1668
weaknessforcats
9,208 Expert Mod 8TB
Think in terms of functionality instead of linear "stream of consciousness" code.

If you need to:

1.output the list of numbers put into the vector

2.identify the highest and lowest number in the list

3.find the sum of the numbers
4.and find the median of the list

then your program can look like:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     std::vector <double> values;
  4.     GetVaues(values);
  5.     ShowVector(values);
  6.     ShowHighValue(values);
  7.     ShowLowValue(values)
  8.     ShowSumOfValues(values);
  9.     ShowMedianValue(values);
  10. }
  11.  
This is much cleaner and easier to read. Plus you can add features just by writing a new function without disturbing what is already working.
Dec 5 '14 #2
could you possibly show me how you would write it for the GetValues(values); part. I'm little confused on how to do it that way
Dec 5 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
You have already written the code in your Post #1. Just package the code in a function format:

Expand|Select|Wrap|Line Numbers
  1. void GetValues(vector<double>& values)
  2. {
  3.      //Just put your get values code here
  4.      //Since the function argument is a reference, the vector
  5.      //values inside the function is the same vector values
  6.      //that is outside the function.
  7. }
Rules prevent me from writing all the code for you.
Dec 5 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
5
by: Yannick Tremblay | last post by:
Hi, I have a std::map<id, timestampcollection. The id is the normal reference so the key-value ordering is correct. However, once in a blue moon, I want to prune this collection. So that...
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
11
by: nandor.sieben | last post by:
I am trying to replace template < class T void set2vector (const set < T &s, vector < T &v) { typename set < T >::iterator it; for (it = s.begin (); it != s.end (); it++) { v.push_back (*it);...
5
by: zfareed | last post by:
I created a program to determine the medians of arrays, be it of type int or float. I have used a template function and the problem I'm having is returning the float median for even arrays. Can...
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
3
by: utab | last post by:
Dear all, I have the below template (1) which I would like to implement to accept either a vector or an array. I thought on it some time and came with a solution as in (2) , however the call of...
8
by: vince1266 | last post by:
I have a little bit of trouble finding the median in a list. I am trying to ask the user what his/her marks are and i am trying to find the median. I have a little bit of trouble because the user...
12
by: Dave Smith | last post by:
I’ve spend the last day researching online how to make access calculate the Median. I really hoping someone can help me find what I need to do to find the median Number of Loans w/Ovg Pts for...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.