Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Laboration 5, Assignment_1A.cpp
- // Fabian Tjernström (fatj1700) 2018-12-12
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- using namespace std;
- int main() {
- const int aSize = 600;
- int a[aSize], aCopy[aSize];
- int average = 0, median = 0, hit = 0, numFreq = 0, maxFreq = 0;
- int mostFreq, searchNum, currentNum;
- //Randomize Array
- srand(time(NULL));
- for(int i = 0; i <= (aSize -1); i++) {
- a[i] = rand() % 100 + 1;
- }
- //Copy Array
- for(int i = 0; i <= (aSize - 1); i++) {
- aCopy[i] = a[i];
- }
- //Average
- for(int i = 0; i <= (aSize - 1); i++) {
- average += aCopy[i];
- }
- average = (average / aSize);
- cout << "Average: ";
- cout << average << endl;
- //Median
- for(int i = 0; i <= (aSize -1); i++) {
- for (int o = 0; o <= (aSize - 1); o++) {
- if (aCopy[o] > aCopy[o + 1]) {
- int temp = aCopy[o];
- aCopy[o] = aCopy[o + 1];
- aCopy[o + 1] = temp;
- }
- }
- }
- median = ((aCopy[298] + aCopy[299]) / 2);
- cout << "Median: ";
- cout << median << endl;
- //Most Frequent Number
- for(int i = 0; i <= (aSize -1); i++) {
- if(aCopy[i] > currentNum) {
- numFreq = 0;
- currentNum = aCopy[i];
- }
- if(currentNum == aCopy[i]) {
- numFreq++;
- }
- if(numFreq > maxFreq) {
- maxFreq = numFreq;
- mostFreq = currentNum;
- }
- }
- cout << "Most frequent number: ";
- cout << mostFreq << endl;
- //Highest and Smallest Value
- int minVal = aCopy[0];
- int maxVal = aCopy[aSize - 1];
- cout << "Smallest Value: ";
- cout << minVal << endl;
- cout << "Highest Value: ";
- cout << maxVal << endl;
- //Number Search
- cout << "--------" << endl;
- cout << "Enter a number between 1-100 to search for: ";
- cin >> searchNum;
- for(int i = 0; i <= (aSize -1); i++) {
- if(aCopy[i] == 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 (int i = 0; i <= (aSize - 1); i++) {
- cout << aCopy[i] << endl;
- }
- }
- if (answer == 'n') {
- cout << "Okay, goodbye!" << endl;
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment