A program that makes a double vector with at least 30 negative and positive elements. I also must have functions to calculate/print the standard deviation, the mean value, the highest value, and the lowest value of the vector. Next, I will need a split()
function to split the values of the double vector into 2 new double vectors, positive
and negative
(the former will have all values from 0 - max_positive_value, the latter will have all nonzero negative values). Finally, I need one last function to display both of the modified vectors.
Nothing whatsoever is being printed from my split()
function! For debugging purposes, I made it so that it prints all the values of the positive and negative vectors, but it prints nothing. The compiler makes no mention of any error of any kind, which doesn't really help!
double split(const vector<double> &data)
{
vector <double> positive;
vector <double> negative;
int j = 0; // iterate inner
// add to pos/neg vector depending on value of data[i]
for (int i = 0; i < data.size(); ++i)
{
if (data[i] >= 0)
{
positive[j] = data[i];
j++;
} else if (data[i] < 0)
{
negative[j] = data[i];
j++;
}
}
// debugging, print vectors
for (int vC = 0; vC < positive.size(); ++vC)
{
cout << positive[vC] << endl;
}
for (int vC = 0; vC < negative.size(); ++vC)
{
cout << positive[vC] << endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <ctime> // both this and the next header are needed for
#include <cstdlib> // rand() to randomly generate numbers
#include <cmath> // sqrt
using namespace std;
// passing by reference uses less resources
double split(const vector<double> &data)
{
vector <double> positive;
vector <double> negative;
int j = 0; // iterate inner
// add to pos/neg vector depending on value of data[i]
for (int i = 0; i < data.size(); ++i)
{
if (data[i] >= 0)
{
positive[j] = data[i];
j++;
} else if (data[i] < 0)
{
negative[j] = data[i];
j++;
}
}
// debugging
for (int vC = 0; vC < positive.size(); ++vC)
{
cout << positive[vC] << endl;
}
for (int vC = 0; vC < negative.size(); ++vC)
{
cout << positive[vC] << endl;
}
return 0;
}
// calculate std deviation
double calculateSD(const vector<double> &data)
{
// calculate sum of data
double sum = 0.0;
for (size_t i = 0; i < data.size(); ++i)
sum += data[i];
// calculate average
double avg = sum / data.size();
// calculate and return the standard deviation
double stdDev = 0.0;
for (size_t i = 0; i < data.size(); ++i)
stdDev += (data[i] - avg) * (data[i] - avg);
return sqrt(stdDev / data.size());
}
int main()
{
// vector with minimum 30 values
vector <double> data (30);
// seed rand() with time
srand (time(NULL));
// fill vector with neg/pos num
for (int i = 0; i < data.size(); ++i)
{
data[i] = rand() - (RAND_MAX / 2);
// cout << data[i] << endl;
}
split(data);
}
NOTE: I have not implemented the needed highest/lowest/mean functions, yet.
Your vectors
vector <double> positive;
vector <double> negative;
are empty, so accessing their elements is undefined behavior. You need
if (data[i] >= 0)
{
positive.push_back(data[i]);
} else if (data[i] < 0)
{
negative.push_back( data[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.