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)
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers...
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers from -30 to 60. 2. Copy v1 into a vector v2 and add names 'odd' or 'even' based on the value. 3. Copy v1 into a vector v3 and if the number can be divided by 3, replace it by 'NA'. 4. Assign the mean of v3 to v4 ignoring the 'NA'.
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      
In R-Syntax, create a vector of 100 employees ("Employee 1", "Employee 2", ... "Employee 100") [Hint:...
In R-Syntax, create a vector of 100 employees ("Employee 1", "Employee 2", ... "Employee 100") [Hint: use the `paste()` function and vector recycling to add a number to the word "Employee"] then create a vector of 100 random salaries for the year 2017 [Use the `runif()` function to pick random numbers between 40000 and 50000] and finally create a vector of 100 salary adjustments between -5000 and 10000 [use runif() to pick 100 random #'s in that range]
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT