Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Laboration 5, Assignment_1B.cpp
- // Fabian Tjernström (fatj1700) 2018-12-14
- #include <iostream>
- #include <algorithm>
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- using namespace std;
- int main() {
- const int aSize = 600;
- vector<int> a(aSize);
- int average = 0, numFreq = 0, maxFreq = 0, hit = 0;
- int mostFreq, searchNum, currentNum, max, min;
- //Randomize Vector
- srand(time(NULL));
- for(auto &e: a) {
- e = rand() % 100 + 1;
- }
- //Copy Vector
- vector<int> aCopy(aSize);
- aCopy = a;
- //Average
- for(auto e: aCopy) {
- average += e;
- }
- average = (average / aCopy.size());
- cout << "Average: ";
- cout << average << endl;
- //Most Frequent Number
- sort(aCopy.begin(), aCopy.end());
- for(auto e: aCopy) {
- if(e > currentNum) {
- numFreq = 0;
- currentNum = e;
- }
- if(currentNum == e) {
- numFreq++;
- }
- if(numFreq > maxFreq) {
- maxFreq = numFreq;
- mostFreq = currentNum;
- }
- }
- cout << "Most frequent number: ";
- cout << mostFreq << endl;
- //Highest and Smallest Value
- min = *(min_element(aCopy.begin(), aCopy.end()));
- max = *(max_element(aCopy.begin(), aCopy.end()));
- cout << "Smallest Value: ";
- cout << min << endl;
- cout << "Highest Value: ";
- cout << max << endl;
- //Number Search
- cout << "--------" << endl;
- cout << "Enter a number between 1-100 to search for: ";
- cin >> searchNum;
- find(aCopy.begin(), aCopy.end(),searchNum);
- for(auto e: aCopy) {
- if(e == searchNum) {
- hit++;
- }
- }
- cout << "Number of occurrences of '" << searchNum << "' are: ";
- cout << hit << endl;
- // Print
- char answer;
- cout << "--------" << endl;
- while(answer != 'y' || answer != 'n') {
- cout << "Print list? [y/n]: ";
- cin >> answer;
- cout << "--------" << endl;
- if (answer == 'y') {
- for (auto e: aCopy) {
- cout << e << endl;
- }
- }
- if (answer == 'n') {
- cout << "Okay, goodbye!" << endl;
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment