Question

In: Computer Science

Create a vector of 100 integers PRG from 1 to 500. Find the max and min...

Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++.

Solutions

Expert Solution

In case of any query do comment. Please rate answer as well. Thanks

Code:

#include <iostream> 
#include <vector> //for vector
#include <stdlib.h> //for rand function  
#include <time.h>
using namespace std;


int main()
{
    vector<int> numbers;
    int number, max, min;
    srand (time(NULL)); //initilize the seed for random number generation
  
    for (int i = 0; i < 100; i++) 
    {
        number = rand() % 500 + 1; // rand() % 500 will generate number between 0 to 499 hence adding 1 to  generate between 1 to 500
        numbers.push_back(number); 
    }
    
    max = numbers[0];
    min = numbers[0];
    //iterate over vector to find min and max
    for(int i = 1; i < numbers.size(); i++)  //start iterating from the second element
    {
        //if curent number is less than min then set the min to curent number
        if(numbers[i] < min)
        {
           min = numbers[i];
        }
        //if curent number is greater than max then set max to current number
        if(numbers[i] > max)
        {
           max = numbers[i];
        }
    }
    //display the result
    cout << "Max: " << max << endl;
    cout << "Min: " << min << endl;
    return 0;
}

==========screen shot of the code ==========

output:


Related Solutions

Create a vector of 50 characters PRG from A to Z. Sort the list in reverse...
Create a vector of 50 characters PRG from A to Z. Sort the list in reverse order (from Z to A). Please use c++. Please use #include<iostream>. Please don't use the ASCII values.
Find the absolute max and min in [-1,1] for f(x)=ln(x2+x+1)
Find the absolute max and min in [-1,1] for f(x)=ln(x2+x+1)
Find the absolute max/min values of f(x) = x/x2+1 on the interval [-2,2].
Find the absolute max/min values of f(x) = x/x2+1 on the interval [-2,2].
Find the number of integers between 100 and 1000 that are
Find the number of integers between 100 and 1000 that are (i) divisible by 7  (ii) not divisible by 7      
1. Find absolute max and min of     f(x,y)= x^2- xy + y^2 +1 on the closed...
1. Find absolute max and min of     f(x,y)= x^2- xy + y^2 +1 on the closed triangular plate in the first quadrant x=0, y=4, y=x 2. Given position of a particle by π (t)= Cos2ti + 3 sin2ti,         Find the particle velocity and acceleration at t=0
Find the absolute max and min of f(x)= e^-x sin(x) on the interval [0, 2pi] Find...
Find the absolute max and min of f(x)= e^-x sin(x) on the interval [0, 2pi] Find the absolute max and min of f(x)= (x^2) / (x^3 +1) when x is greater or equal to 0
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative...
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative minimum of a function. Our process is to • find the derivative of the function, • set that derivative equal to zero, • and solve for x. Use complete sentences to explain why this process makes sense.
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg) Joe 1.82 72.57 Mary...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or 5? Consider strings formed from the 26 English letters. How many strings are there of length 5? How many ways are there to arrange the letters `a',`b', `c', `d', and `e' such that `a' is not immediately followed by`e' (no repeats since it is an arrangement)?
Find the relative extrema, if any, classify as absolute max/min. a.) f(x)= x+1/x-2 on [2,4] b.)...
Find the relative extrema, if any, classify as absolute max/min. a.) f(x)= x+1/x-2 on [2,4] b.) f(x)= x^(2) -2x-3 on [-2,3] c.) f(x)= x^(2/3) (x^2-4) on [-1,3] Solve for x: a.) 6^(2x) =36 b.) 2^(2x) -4 * 2^(x) +4=0 c.) 3^(x-x^2) =1/9x Determine if the following statements are true or false. If it is true, explain why. If it is false, provide an example. a.) If a and b are positive numbers, then (a+b)^x=a^x+b^x b.) If x < y, then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT