tags:

views:

54

answers:

3

alt text// Calculate the quarters of a set of integers

#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::sort;

int main()
{
 // Ask for a set of integers
 cout << "Please input a set of integers: "
   << endl;

 // Read the set of integers
  // x is the variable to write
 int x;
  // int_set is the set of integers to write
 vector<int> int_set;
 while (cin >> x)
  {
   int_set.push_back(x);
  }

 // Check if the integer set is vacant 
 typedef std::vector<int>::size_type vec_sz;
 vec_sz size = int_set.size();
 if (size == 0)
  cout << "There are no data. "
    << "Please try again. ";

 // Sort
 sort (int_set.begin(), int_set.end());

 // The set of integers multiply 1/4
 vector<double> int_set_quarter;
 cout << "The quarters of the set of integers are: ";
 for (int i = 0; i != size; ++i)
  {
   int_set_quarter[i] = 1/4 * int_set[i];
   cout << int_set_quarter[i]; 
   cout << endl;
  }

    getch();
 return 0;
}

If you run, then it will collapse...

+2  A: 

int_set_quarter's size is 0 and you index on it. Change

vector<double> int_set_quarter;

to

vector<double> int_set_quarter(size);
Armen Tsirunyan
how to fix it out?
Eric Wang
@Armen Tsirunyan,thank you~
Eric Wang
@Eric: What do you mean: how to fix it out? if you change the lines as I said, everything will be fine.
Armen Tsirunyan
@Armen Tsirunyan: yeah, you are right.
Eric Wang
A: 

The problem is this:

vector<double> int_set_quarter;
int_set_quarter[0] = 0.25;

This code will crash, because the int_set_quarter vector does not have a 0th element. You already know how to fix this (you used the same thing in your int_set vector). You add elements to the end of a vector with the push_back method. You can also use Armen's method of setting up the size of the vector initially via it's constructor.

But in this specific example, you don't need that. You never actually use the int_set_quarter vector, so you can just do this:

// The set of integers multiply 1/4
cout << "The quarters of the set of integers are: ";
for (int i = 0; i != size; ++i)
{
    double quarter = 1/4 * int_set[i];
    cout << quarter; 
    cout << endl;
}
Dean Harding
@Dean Harding: that is really a good method.
Eric Wang
A: 

You can't add elements to a vector the way you're trying to do with int_set_quarter, so you end up writing to somewhere that hasn't been allocated yet. This causes an access violation, and your program crashes.

You can either reserve the necessary space in the vector by providing the number of required elements when creating the vector, by calling reserve or resize, or by using push_back to add the element to the end of the vector...

...or, since you don't do anything with int_set_quarter after the loop, you can just calculate what you need to output for that iteration into a local variable, sa shown by Dean.

Michael Madsen
@Michael Madsen: thanks~
Eric Wang