online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
// Project 2: Stats Solution Code // The libraries have already been included for you! #include<iostream> #include<vector> using namespace std; /* First, we must define the functions we will be using. We know that we need to calculate the maximum, minimum, range, and mean, so you should write functions for each of these calculations. We also need to include a function that checks if the vectors are in ascending order. Keep in mind that we want all of these to return an int exxept for the last one, which should return a bool. */ // Maximum function - The base syntax has been provided for you for this one. int max(vector<int> v_in){ // Here, v_in is the input vector int max_val = v_in[0]; // declares & initializes variable that will be returned for(int i = 1; i < v_in.size(); i++){ // loop iterates through vector values if(v_in[i] > max_val){ // if value is greater than the current max, the max will be updated max_val = v_in[i]; } } return max_val; // outputs the maximum value; max_val should hold the largest number in the vector } // Minimum function - try the rest on your own! int min(vector<int> v_in){ int min_val = v_in[0]; // declares & initializes variable that will be returned for(int i = 1; i < v_in.size(); i++){ // loop iterates through vector values if(v_in[i] < min_val){ // if value is less than the current min, the min will be updated min_val = v_in[i]; } } return min_val; // outputs the minimum value } // Range function int range(vector<int> v_in){ // Here, we need the max and min values using the funtions you just defined in order to calculate the range. int max_val = max(v_in); // uses max function to find the max value in vector int min_val = min(v_in); // uses min function to find the min value in vector return max_val - min_val; // outputs the range } // Mean function int mean(vector<int> v_in){ int mean_val; // declares value to be returned int sum = 0; // declares & initializes a variable used to calculate vector sum for(int i = 0; i < v_in.size(); i++){ // loop iterates through vector values sum += v_in[i]; // adds vector element to the sum } mean_val = sum / v_in.size(); // calculates the mean return mean_val; // outputs the mean } // Ascending function - should check to see if a vector is in ascending order bool ascend(vector<int> v_in){ bool ans = true; // declares & initializes variable that will be returned for(int i = 1; i < v_in.size(); i++){ // loop iterates through vector values if(v_in[i] < v_in[i-1]){ // checks if ascending ans = false; // changes the output to be false if finds that vector is not in order break; // there's no need to keep going through the vector if we find that it's not in ascending order early on } } return ans; // outputs wether or not vector is in ascending order } /* Now that we have written all of the functions we will need, it's time to write the main code. */ int main(){ // Initialize the two vectors here - the first has been started for you vector<int> vec_one = {7, 11, 12, 9, 14, 10, 9, 15, 7, 8}; vector<int> vec_two = {42, 46, 48, 53, 58, 60, 64}; // Create variables with the each of the values you need (you will call your functions here): // First vector variables int max_one = max(vec_one); // This one has been completed for you int min_one = min(vec_one); int range_one = range(vec_one); int mean_one = mean(vec_one); bool ascending_one = ascend(vec_one); // Second vector variables int max_two = max(vec_two); int min_two = min(vec_two); int range_two = range(vec_two); int mean_two = mean(vec_two); bool ascending_two = ascend(vec_two); // Now let's output our results! - This has been started for you // First Vector cout << "The first vector has the following properties:" << endl; cout << "max: " << max_one << endl; cout << "min: " << min_one << endl; cout << "range: " << range_one << endl; cout << "mean: " << mean_one << endl; cout << "ascending: " << ascending_one << endl << endl; // Second Vector cout << "The second vector has the following properties:" << endl; cout << "max: " << max_two << endl; cout << "min: " << min_two << endl; cout << "range: " << range_two << endl; cout << "mean: " << mean_two << endl; cout << "ascending: " << ascending_two << endl; return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue