1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
#include <new>
using namespace std;
int reverse(int[], int size);
int expand(int *arr, int size, int *revArray);
void shiftright(int *arr, int size);
void calculateMode(int *arr, int size);
int main()
{
int n, size;
int *arr;
int *revArray;
cout << "How many numbers would you like to type? ";
cin >> size;
if (size <= 0)
{
cout << "Invalid Input! Please enter a value > 0:" << endl;
return 0;
}
arr= new (nothrow) int[size];
revArray= new (nothrow) int[size];
if (arr == nullptr)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<size; n++)
{
cout << "Enter number: ";
cin >> arr[n];
}
cout << "You have entered: ";
for (n=0; n<size; n++)
cout << arr[n] << ", ";
cout << "\n";
}
revArray = reverse(arr, size);
cout << "The contents of the reversed array are: ";
for(int i=0; i<size; i++)
{
cout<<revArray[i]<<" ";
}
delete[] arr;
return 0;
}
//***********************************************************************
//reverse function
//This function reverses the order of the elements in a array.
//
//Return Value
//----------------
//void
//
//Parameter
//----------------
//int *arr
//int size
//***********************************************************************
int* reverse(const int* arr, int size)
{
int* revArray = new int[size];
for (int i = 0; i < size; ++i)
revArray[i] = arr[size-i-1];
return revArray;
}
//***********************************************************************
//This function expands the original and reversed array into one array.
//
//Return Value
//----------------
//int
//
//Parameter
//----------------
//int *arr
//int size
//int *revArray
//***********************************************************************
int expand(int *arr, int size, int *revArray)
{
}
//***********************************************************************
//This function creates a new array that shifts each element to the right by 1.
//
//Return Value
//---------------
//void
//
//Parameter
//---------------
//int *arr
//int size
//***********************************************************************
void shiftright(int *arr, int size)
{
int temp;
int temp1;
for (int i=0; i<(size -1); i++)
{
temp = arr[size-1];
arr[size-1] = arr[i];
arr[i] = temp;
}
}
//***********************************************************************
//mode function
//This function find the mode of the array.
//
//Return Value
//----------------
//void
//
//Parameter
//----------------
//int *arr
//int size
//***********************************************************************
void calculateMode(int *arr, int size)
{
int counter = 1;
int max = 0;
int mode = arr[0];
for (int pass = 0; pass < size - 1; pass++)
{
if (arr[pass] == arr[pass+1])
{
counter++;
if (counter > max)
{
max = counter;
mode = arr[pass];
}
} else
counter = 1;
}
cout << "The mode is: " << mode << endl;
}
|