Implementing algorithms for Randomly generated array - c++

So I have the following code to generate an array comprised of random numbers from 1-20000 and the array can have different sizes (100, 500, 1000 etc).
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void insertSort(int input[]);
/*
void quickSort(int input[], int, int);
void mergeSort(int input[], int, int);
void merge(int input[], int, int, int);
void percolate(int numbers[], int, int);
void heapSort(int numbers[], int);
*/
int SIZE; //global Size variable
int main()
{
int i;
//int size;
int random_once[10000]; //maximum array size
srand(time(0));
cout << "How big would you like the list to be?" << "\n\n";
cout << "\t Enter any of the following: 100, 500, 1000, 2000, 5000, 8000, or 10000" << "\n";
cin >> SIZE;
for (int i=0; i<SIZE; i++)
{
random_once[i]=rand() % 20000; //generating a random number between 1 - 20000
// generate unique random number only once so no duplicates
for(int j=0; j<i; j++) if (random_once[j]==random_once[i]) i--;
}
cout<< " " << i << "\n\n\n ";
for(i=0; i<SIZE; i++) cout << " " << random_once[i] << "\t"; //outputting array to console
return 0;
}
//insert sort algorithm
void insertSort(int input[]) {
int i, j, temp;
for (i = 0; i < SIZE; i++) {
temp = input[i];
for (j = i; j > 0 && temp < input[j - 1]; j--) {
input[j] = input[j - 1];
}
input[j] = temp;
}
return;
}
I was wondering how I can have the insertion sort be printed out so that it prints out the sorted array after it used the insertion sort algorithm.
I was also wondering how I can print out the time complexity of the algorithm before being sorted and after so that I can compare it with other sorting algorithms.
EDIT (with added template)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <string>
using namespace std;
void insertSort(int input[]);
/*
void quickSort(int input[], int, int);
void mergeSort(int input[], int, int);
void merging(int input[], int, int, int);
void percolate(int numbers[], int, int);
void heapSort(int numbers[], int);
*/
int SIZE; //global Size variable
template <typename F, typename... Args>
std::string callTime(F func, int array[], Args... args) {
using namespace chrono;
auto t1 = high_resolution_clock::now();
func(array, args...);
auto t2 = high_resolution_clock::now();
return to_string(duration_cast<milliseconds>(t2 - t1).count()) + "ms\n";
}
int main()
{
int i;
//int size;
int random_once[10000]; //maximum array size
srand(time(0));
//asking user for the size of the list
cout << "How big would you like the list to be?" << "\n\n";
cout << "\t Enter any of the following: 100, 500, 1000, 2000, 5000, 8000, or 10000" << "\n";
cin >> SIZE;
//for loop to generate numbers and nested for loop to handle duplicates
for (int i=0; i<SIZE; i++)
{
random_once[i]=rand() % 20000; //generating a random number between 1 - 20000
// generate unique random number only once so no duplicates
for(int j=0; j<i; j++) if (random_once[j]==random_once[i]) i--;
}//end generating for loop
cout<< " " << i << "\n\n\n ";
for(i=0; i<SIZE; i++) cout << " " << random_once[i] << "\t"; //outputting array to console
cout << "insertSort(): " << callTime(&insertSort, random_once);
return 0;
} //end main
//insert sort algorithm
void insertSort(int input[]) {
int i, j, temp;
for (i = 0; i < SIZE; i++) {
temp = input[i];
for (j = i; j > 0 && temp < input[j - 1]; j--) {
input[j] = input[j - 1];
}
input[j] = temp;
}
}

I am not sure if I get you right, but what you can do is to measure time before and after call of sorting functions. For time measurement C++ has various tools for example std::high_resolution_clock which I am using in code bellow. You can then observe how sorting algorithms behave on arrays of different sizes. There I wrote you template function which should be usable with every sort you plan to implement(don't forget to include chrono header):
#include <chrono>
template <typename F, typename... Args>
std::string callTime(F&& func, int array[], Args&&... args) {
using namespace std::chrono;
auto t1 = high_resolution_clock::now();
func(array, std::forward<Args>(args)...);
auto t2 = high_resolution_clock::now();
return std::to_string(duration_cast<milliseconds>(t2 - t1).count()) + "ms\n";
}
You can call it like this :
int main() {
// what you have before
cout << "insertSort(): " << callTime(insertSort, random_once);
//...
cout << "merge(): " << callTime&merge, random_once, /* other params */);
//...
return 0;
}
Note that you can change precision of duration cast:
duration_cast<microseconds>(t2 - t1).count() // now returns microseconds passed

Related

Repeating Array elements

I am running a code for finding repeating array elements.
I am doing it using 2 functions, however when I run the code my application immedietaly crashes despite assigning it to random numbers from 1 to 99.
Here is the code. Thank you..
#include <ctime>
#include <iostream>
using namespace std;
int UniqueArray(int arr[], int notunique);
void printarray(int arr[]);
int main() {
int arr[20];
int dup = 0;
printarray(arr);
for (int i = 0; i < 20; ++i) {
UniqueArray(arr, dup);
}
}
int UniqueArray(int arr[], int notunique) {
notunique = 0;
int i, j;
int size = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
notunique++;
cout << "Array has duplicates: " << arr[i] << " ";
}
}
}
return notunique;
cout << "There were " << notunique << " Repeated elements";
}
void printarray(int arr[]) {
int size = sizeof(arr) / sizeof(arr[0]);
srand(time(0));
arr[20] = rand () % +100;
for (int i = 0; i < 20; ++i) {
cout << arr[i] << " ";
}
}
This line:
arr[20] = rand () % +100;
does not fill an array of size 10 with random values. It indexes the 20th position, which is UB.
You could fill the array with random numbers, using std::generate, like this:
std::generate(arr, arr + 20, [] { return rand() % 100; });
Also, when finding the size of the array, you'll need to deduce the size:
template <size_t N>
void printarray(int (&arr)[N]) {
// ... use N which is the size of arr
or even better, use std::array, which does this for you.
Some minor issues:
Don't use using namespace std;.
In this snippet:
return notunique;
cout << "There were " << notunique << " Repeated elements";
the statement after the return will never get executed.
In this line:
arr[20] = rand () % +100;
you don't need the + operator.

C++ Turning arrays into functions?

I have been attempting this for hours to no avail, as you can see in my code I have separate functions, they were all together in main, but I am required to turn each into a separate function. However when I try anything I get errors, even when I try to pass parameters. Can someone point me in the right direction?
#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray();
void average();
void largestnumber();
using namespace std;
int main()
{
printarray();
average();
largestnumber();
}
void printarray() {
srand(time(0));
int n[10], tot = 0;
for (int i = 0; i < 10; i++)
{
n[i] = (1 + rand() % 100);
cout << n[i] << endl;
}
}
void average() {
int j, tot = 0, n[10];
for (j = 0; j < 10; j++)
{
tot += n[j];
}
cout << "The average of the numbers in the array are " << tot / j << endl;
}
void largestnumber() {
int w = 1, int n[10];
int temp = n[0];
while (w < 10)
{
if (temp < n[w])
temp = n[w];
w++;
}
cout << "The largest number in the array is " << temp << endl;
}
The array you are working with needs to be passed in to each function, so the same array is used everywhere. It is a good idea to pass the size as well, just for flexibility reasons.
Now your functions pretty much work as you wrote them.
#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray(int n[], size_t size);
void average(int n[], size_t size);
void largestnumber(int n[], size_t size);
using namespace std;
int main()
{
const size_t arr_size = 10;
int n[arr_size];
printarray(n, arr_size);
average(n, arr_size);
largestnumber(n, arr_size);
}
void printarray(int n[], size_t size) {
srand((unsigned int)time(0));
int tot = 0;
for (size_t i = 0; i < size; i++)
{
n[i] = (1 + rand() % 100);
cout << n[i] << endl;
}
}
void average(int n[], size_t size) {
size_t j;
int tot = 0;
for (j = 0; j < size; j++)
{
tot += n[j];
}
cout << "The average of the numbers in the array are " << tot / j << endl;
}
void largestnumber(int n[], size_t size) {
size_t w = 1;
int temp = n[0];
while (w < size)
{
if (temp < n[w])
temp = n[w];
w++;
}
cout << "The largest number in the array is " << temp << endl;
}
One simple improvement is to break the printarray out into an initarray function that fills the array and printarray that prints the content.
It would also be a good idea to do some checking for things like an empty array (functions assume n[0] exists, for instance).
The next obvious step is to put all this in a class. Also, if you are allowed to, the c array should be replaced with a vector, as that does a great job of keeping all the resource information together.

Array sorting, Array input, Array Output

I'm having some problems with this program. It is meant to input random numbers into an array, change its dimensions, sort them, the output the sorted array. For some reason, the array will only fill with one number (-858993460) and I cannot figure out why. Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void InputArray(int[][5], int, int);
void OutputArray(int[], int);
void SelectionSort(int[], int);
void CopyArray(int[][5], int, int, int[], int);
int main()
{
int sample_1[80];
int sample_2[16][5];
InputArray(sample_2, 16, 5);
CopyArray(sample_2, 16, 5, sample_1, 80);
cout << "Before sorting, contents of the array:" << endl << "----------------------" << endl;
OutputArray(sample_1, 80);
SelectionSort(sample_1, 80);
cout << "After sorting, contents of the array:" << endl << "----------------------" << endl;
OutputArray(sample_1, 80);
return 0;
}
//generate random numbers for a two dimensional array
void InputArray(int array[][5], int m, int n)
{
int i, j;
srand(time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
array[i][j] = rand() % 1000;
}
}
}
//display values in a one-dimensional array
void OutputArray(int array[], int number)
{
int i;
for (i = 0; i < number; i++)
{
cout << array[i] << "\t";
}
}
// selection sort of a one-dimensional array
void SelectionSort(int numbers[], int array_size)
{
int i, j, a;
for (i = 0; i < array_size; ++i) {
for (j = i + 1; j < array_size; ++j) {
if (numbers[i] > numbers[j]) {
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
return;
}
//x and y and two dimensions of array_2d; n is the dimension of array_1d
//copy values from array_2d[][] to array_1d[]
//assume x*y equals n
void CopyArray(int array_2d[][5], int x, int y, int array_1d[], int n)
{
memcpy(array_2d, array_1d, sizeof(array_1d));
return;
}
void CopyArray(int array_2d[][5], int x, int y, int array_1d[], int n)
{
memcpy(array_2d, array_1d, sizeof(array_1d));
}
That's your problem right there. The size of the array_1d is unspecified here. The sizeof() operator does not know the size of the array that's being copied.
In fact, I'm surprised that this even compiles, although I'm too lazy to test it with gcc.
What you need to do is calculate the size of the array yourself, multiply it by sizeof(int), and use that instead of the existing sizeof() operator.

How to optimize random sort algorithm?

Here is some random sort program I wrote in C++. It works pretty fine for 10 elements or so. But for 15 elements it works so slow I can't even wait enough to get the result. Is there some way to optimize random sort algorithm?
Here's my code:
// randomsort.h
#ifndef RANDOMSORT_H
#define RANDOMSORT_H
#include <stdlib.h>
#include <time.h>
class RandomSort
{
private:
template <class T>
static bool isOrdered(T*, int);
public:
template <class T>
static int sort(T*, int);
};
template <class T>
bool RandomSort::isOrdered(T* arr, int size)
{
for(int i = 1; i < size; i++)
{
if(arr[i-1] > arr[i])
{
return false;
}
}
return true;
}
template <class T>
int RandomSort::sort(T* arr, int size)
{
int stepAmount = 0;
srand(time(NULL));
while(!isOrdered(arr, size))
{
int i = rand() % size;
int j = rand() % size;
std::swap(arr[i], arr[j]);
stepAmount++;
}
return stepAmount;
}
#endif // RANDOMSORT_H
And main.cpp file
// main.cpp
#include <iostream>
#include "randomsort.h"
int main()
{
int size;
std::cout << "Enter amount of elements to sort: ";
std::cin >> size;
std::cout << std::endl;
int arr[size];
for(int i = 0; i < size; i++)
{
arr[i] = (rand() % (size * 10));
}
std::cout << "Input array: " << std::endl;
for(int i = 0; i < size; i++)
std::cout << arr[i] << ' ';
std::cout << std::endl << std::endl;
int stepAmount = RandomSort::sort(arr, size);
std::cout << "Output array: " << std::endl;
for(int i = 0; i < size; i++)
std::cout << arr[i] << ' ';
std::cout << std::endl << std::endl;
std::cout << "Number of steps: " << stepAmount;
return 0;
}
Any suggestions?
Your code is completely random. So it can swap when it should not. An easy fix would be to swap only if you need it.
int i = rand() % size;
int j = rand() % size;
// to know which should be first
if (i > j)
std::swap(i, j);
if (arr[i] > arr[j])
std::swap(arr[i], arr[j]);
Your array probably will not be sorted immediately, so you could also test if it is sorted only every five steps (for example) instead of every step.
But i think the most important is, you should not expect good performances from such an algorithm.

C++ unable to find multiple modes

My assignment asks me to write a function that takes an array and the size of that array as a parameter, and to find the mode. If there are multiple modes, I am to find them all, and place them in a vector and print said vector in an ascending order.
For example, if I input the following integers:
3, 4, 2, 1, 2, 3
Then the output should display
2, 3
If I input the following integers:
1, 2, 3, 4
Then the output should display:
1, 2, 3, 4.
However, my program somehow only finds the first mode and displays it in a really awkward manner.
Here was my input:
3, 4, 2, 3, 2, 1
And this was the output:
3
3
3
3
3
Here is my code. Any help would be greatly appreciated. Thank you all for your time!
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int size; //array size
int* array; //array of ints
int arraycount; //counter for array loop
void findMode(int array[], int size); //function prototype
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
array = new int[size];
for (arraycount = 0; arraycount < size;
arraycount++)
cin >> array[arraycount];
//call function
findMode(array, size);
return 0;
}
void findMode(int array[], int size) {
int counter = 1;
int max = 0;
int mode = array[0];
int count;
vector <int> results;
//find modes
for(int pass = 0; pass < size - 1; pass++) {
if(array[pass] == array[pass+1]) {
counter++;
if(counter > max) {
max = counter;
mode = array[pass];
}
}
else {
counter = 1;
}
}
//push results to vector
for (count=0; count < size - 1; count++) {
if(counter == max) {
std::cin >> mode;
results.push_back(mode);
}
}
//sort vector and print
std::sort(results.begin(), results.end());
for (count=0; count < size - 1; count++) {
cout << mode << endl;
}
}
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main() {
int size; //array size
int* array; //array of ints
std::vector<int> vecInput;
int arraycount; //counter for array loop
void findMode(std::vector<int> vec); //function prototype
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
array = new int[size];
for (arraycount = 0; arraycount < size; arraycount++)
{
int num;
cin >> num;
vecInput.push_back(num);
}
//call function
findMode(vecInput);
return 0;
}
void findMode(std::vector<int> vec)
{
std::sort(vec.begin(), vec.end());
std::map<int, int> modMap;
std::vector<int>::iterator iter = vec.begin();
std::vector<int> results;
int prev = *iter;
int maxCount = 1;
modMap.insert(std::pair <int,int>(*iter, 1));
iter++;
for (; iter!= vec.end(); iter++)
{
if (prev == *iter)
{
std::map<int, int>::iterator mapiter = modMap.find(*iter);
if ( mapiter == modMap.end())
{
modMap.insert(std::pair <int,int>(*iter, 1));
}
else
{
mapiter->second++;
if (mapiter->second > maxCount)
{
maxCount = mapiter->second;
}
}
}
else
{
modMap.insert(std::pair <int,int>(*iter, 1));
}
prev = *iter;
}
std::map<int, int>::iterator mapIter = modMap.begin();
for (; mapIter != modMap.end(); mapIter++)
{
if (mapIter->second == maxCount)
{
results.push_back(mapIter->first);
}
}
cout << "mod values are " <<endl;
std::vector<int>::iterator vecIter = results.begin();
for (; vecIter != results.end(); vecIter++)
cout<<*vecIter<<endl;
}
Thank you all for the help, I was able to get the code to work. Turns out the problem was the cin in my vector portion of the function. Here is my revised code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>
#include <vector>
using namespace std;
#define N 100
void findMode(int x[], int size); //function prototype
vector<int> results; //vector
int main(void) {
int* x;
int size=0;
int arraycount;
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
x = new int[size];
for (arraycount = 0; arraycount < size;
arraycount++)
cin >> x[arraycount];
//send array and size to function
findMode(x, size);
return 0;
}
//findMode function
void findMode(int x[], int size) {
int y[N]={0};
int i, j, k, m, cnt, count, max=0;
int mode_cnt=0;
int num;
int v;
vector<int> results;
vector<int>::iterator pos;
//loop to count an array from left to right
for(k=0; k<size; k++) {
cnt=0;
num=x[k]; //num will equal the value of x[k]
for(i=k; i<size; i++) {
if(num==x[i])
cnt++;
}
y[k]=cnt; //
}
//find highest number in array
for(j=0; j<size; j++) {
if(y[j]>max)
max=y[j];
}
//find how many modes there are
for(m=0; m<size; m++) {
if(max==y[m])
mode_cnt++;
}
//push results to vector
for (m=0; m < size; m++) {
if(max == y[m]) {
//after taking out this line the code works properly
// std::cin >> x[m];
results.push_back(x[m]);
}
}
//sort vector and print
std::sort(results.begin(), results.end());
cout << "The mode(s) is/are: ";
for (pos=results.begin(); pos!=results.end(); ++pos) {
cout << *pos << " ";
}
}