so basicly this is what i have right now and nothing else. This tutor online was
ID: 3544769 • Letter: S
Question
so basicly this is what i have right now and nothing else. This tutor online was trying to help me but he kept using #include <stdio.h> or something like that. Keep in mind im a little bit of a slow learner so please use the most simplest forms possible. Thanks and god bless you!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<double> vobject(50);
return 0;
}
Write a program that generates 50 random numbers of type integer ranges from 1 to 1000 and load all the numbers into a C++ vector. The program should then display the largest, the smallest, the sum, and the average values stored in the vector. Sort the vector in ascending order and display all values on screen.
so basicly this is what i have right now and nothing else. This tutor online was trying to help me but he kept using #include or something like that. Keep in mind im a little bit of a slow learner so please use the most simplest forms possible. Write a program that generates 50 random numbers of type integer ranges from 1 to 1000 and load all the numbers into a C++ vector. The program should then display the largest, the smallest, the sum, and the average values stored in the vector. Sort the vector in ascending order and display all values on screen.Explanation / Answer
Hey there. Its me again. I removed some extra code and added comments. Hopefully this is better for you.
#include <vector>
#include <algorithm>
#include <iostream>
int main(){
// constructs vector
std::vector<int> vec;
// fills vector with 50 random numbers from 1 - 1000
for(int i=0; i < 50; i++ ){
int randNum = rand() % 1000 + 1;
vec.push_back(randNum);
}
// intializes min, max, and sum
// good practice to set min and max to first element in array rather than a constant
int min = vec.front();
int max = vec.front();
int sum = 0;
// loop to go through all 50 numbers in the vector
for(int i=0; i < 50; i++ ){
// gets the number in the vector at postion i
int num = vec.at(i);
// checks to see if the num is smaller than the min
if(num < min){
min = num;
}
// checks to see if the num is larger than the max
if(num > max){
max = num;
}
// increments the sum
sum += num;
}
// finds the average
int avg = sum / 50;
// sorts the vector in ascending order
std::sort (vec.begin(), vec.end());
// prints all the elements in the vector
std::cout << "vector: ";
for (std::vector<int>::iterator it=vec.begin(); it!=vec.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << ' ';
// prints min, max, sum, and average
std::cout << "min = " << min << " ";
std::cout << "max = " << max << " ";
std::cout << "sum = " << sum << " ";
std::cout << "avg = " << avg << " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.