Question

In: Computer Science

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...

You MUST use VECTORS in this lab. Do NOT use ARRAYS.

Write code in C++ with //comments . Please include a screen shot of the output

Part 1: Largest and Smallest Vector Values

Specifications:

Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector.

Create 3 functions in addition to your main function.

  • One function should generate the random numbers and store them in the vector.
  • A second function should find the largest number in the vector.
  • The third function should find the smallest number in the vector.

Your main function should call these functions and display the vector, the largest value and the smallest value.

Solutions

Expert Solution

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

vector<int> generate_vector() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(50 + (rand() % 51));
    }
    return v;
}

int minimum(vector<int> v) {
    int min = v[0];
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] < min)
            min = v[i];
    }
    return min;
}

int maximum(vector<int> v) {
    int max = v[0];
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] > max)
            max = v[i];
    }
    return max;
}

int main() {
    srand(time(NULL));
    vector<int> v = generate_vector();
    cout << "Vector is: ";
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
    cout << "Minimum: " << minimum(v) << endl;
    cout << "Maximum: " << maximum(v) << endl;
    return 0;
}


Related Solutions

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
c++: Exercise 1: Study the tutorial from Unit 6 on Sorting Arrays and Vectors. Write a...
c++: Exercise 1: Study the tutorial from Unit 6 on Sorting Arrays and Vectors. Write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and prints the sorted sequence. Use the sort method from the C++ standard library. Do not add duplicate values to your array.   Hint: #include <algorithm> #include "math.h" using namespace std; const int SIZE = 100; while (numAdded < 100) { val = floor(rand()...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT