Hey, Iv decided to tackle a bit off C++ before I start it in college in fall and Im sort of stuck on a problem. The book im reading is Accelerated C++ and its exercise 3.2.

Basically the question asks to get values from the user and take for at a time and give the sum of it...I have taken a few liberties since the actual question is very vague. My idea is that the user enters enough numbers to be divisible by 4 after the vector gets sorted. So you basically sort the vector and take the first 4 and the the second 4 and so on until you reach the end of the vector. I am not going to divide by 4 yet just sum the values right now.

My question is,
a) how can I terminate the second loop
b) will my variable z go out of scope before it goes through the whole vector.

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>

using std::cin;             using std::sort;
using std::cout;            using std::streamsize;
using std::endl;            using std::string;
using std::setprecision;    using std::vector;

int main()
{
    // ask for and read the value
    cout << "Please enter an integer value: ";

    vector<int> value;
    double x;
    while (cin >> x)
        value.push_back(x);

    // check that a value has been entered
    typedef vector<double>::size_type vec_sz;
    vec_sz size = value.size();
    if (size == 0) {
        cout << endl << "You must enter your grades.  "
                        "Please try again." << endl;	
        return 1;
    }

    // sort the grades
    sort(value.begin(), value.end());

	// compute the values taking the largest 4 and so on
	int y = 0;
	int i, z;
	vector<int> sum;


	while(z != size){
		while(y != 4){
			y++;
			sum[i] += value[z];
			z++;
		}
			i++;
	}


    // Output and create a vector for sum
	cout << sum[i] << endl;

    return 0;
}

> b) will my variable z go out of scope before it goes through the whole vector.
No sir. The scope of z is the same as the scope of the vector, so as long as you can access the vector object, you can access z. :)

> a) how can I terminate the second loop
If Edward understands what you're doing, the loop won't sum all of the sets of 4 numbers because you don't reset y. You also need to call push_back before indexing a vector. Ed would write something like this:

for (int i = 0; i < value.size(); /* i updated in the body */ ) {
  int tempSum = 0;

  // Sum the next set of 4 (assume at least 4 remaining numbers)
  for (int j = 0; j < 4; ++j)
    tempSum += value[i++];

  sum.push_back(tempSum);
}

for (int i = 0; i < sum.size(); ++i)
  std::cout << sum[i] << '\n';

I think you are misunderstanding the exercise.

The quartiles of a set of data have nothing to do with groups of four numbers. Rather, the notion of a quartile is a generalization of the notion of a median.

In other words: To find the median of a set of numbers, you sort the numbers and then divide the sorted set into two pieces that are as close to equal in size as you can manage. The median is the value that fits between those halves. In other words, the median is the value such that the same number of values are >= the median as are <= the median.

The quartiles of a set of numbers are the values that divide the set into four subsets of equal size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.