Class Recursive Array - c++

My problem here is that I do not get why I cannot get the desired sum of the array's random numbers. Could anyone help me to figure out the error, please?
#include <iostream>
using namespace std;
class Recursion{
int max_size;
double sum;
int index;
double* arr;
public:
Recursion(int);
void fill_array();
void sum_array();
};
Recursion::Recursion(int size){//from main
max_size = size;
sum = 0;
index = 0;
arr = new double[max_size];
}
void Recursion::fill_array(){
if (index == max_size){
cout << "Array is Full." << endl;
//stop array
}
else{
arr[index] = rand() % 10+1;
cout << arr[index] << endl;
index++;
fill_array();
}
}
void Recursion::sum_array(){
if (index == max_size){
cout << "Sum is: "<< sum << "!"<< endl;
}
else{
sum = sum + arr[index];
index++;
sum_array();
}
}
int main(){
Recursion connect(5);
connect.fill_array();
connect.sum_array();
return 0;
}
The output is:
8
10
4
9
1
Array is Full.
Sum is: 0!

It is most unusual to use object fields for recursion.. variables like index are usually passed as parameters:
double Recursion::sum_array(int index) {
if (index >= max_size) {
return 0;
} else {
return arr[index] + sum_array(index + 1);
}
}
int main() {
// ...
cout << "Sum is: "<< sum_array(0) << "!"<< endl;
// ...
}
Otherwise, like other answers say, in your original code you forgot to reset the index (that's exactly why it's weird to have it stored in the class).

After this call:
connect.fill_array();
index is equal to max_size. You want to re-initialize it to 0, when you are done filling the array (so that it's available for the other function), like this:
if (index == max_size){
cout << "Array is Full." << endl;
index =0;
//stop array
}
Now the output is:
4
7
8
6
4
Array is Full.
Sum is: 29!
Personal opinion:
Making an index a data memebr of class, in order to share it between two functions, but with no need of sharing (I mean it's not that the one uses the current value in an intermediate step of the other), is kind of odd, and can lead to mistakes, as you experienced already.
The index, i.e. the counter for looping over the array should be local-scoped to the function that loops the array at that time, so I propose to discard index from your class, as a data member, and pass it as a parameter in your functions. Moreover, you could prodive a default value for that parameter, since you want to loop from the start of the array.
Putting everything together, we get:
#include <iostream>
using namespace std;
class Recursion{
int max_size;
double sum;
double* arr;
public:
Recursion(int);
void fill_array(int index);
void sum_array(int index);
};
Recursion::Recursion(int size){//from main
max_size = size;
sum = 0;
arr = new double[max_size];
}
void Recursion::fill_array(int index = 0){
if (index == max_size){
cout << "Array is Full." << endl;
//stop array
}
else{
arr[index] = rand() % 10+1;
cout << arr[index] << endl;
index++;
fill_array(index);
}
}
void Recursion::sum_array(int index = 0){
if (index == max_size){
cout << "Sum is: "<< sum << "!"<< endl;
}
else{
sum = sum + arr[index];
index++;
sum_array(index);
}
}
int main(){
Recursion connect(5);
connect.fill_array();
connect.sum_array();
return 0;
}
That ! in the final print scared me a bit, you might want to remove it (replace it with a dot for example), since it might confuse the user, with factorials.

When you call sum_array index is equal to max_size, you should clear it in fill_array method.
void Recursion::fill_array(){
if (index == max_size){
cout << "Array is Full." << endl;
//stop array
index = 0;
}

After fill_array() index is set to max_size. You have to reset index to 0 before calling sum_array()

Related

How is the pointer (*) making changes in the following code?

The following program is intended to check if a given element is in a given array, indices of array where the element occurs and number of times the element occurs. But, it doesn't give right results. I tried to replace poscount in seqsearch function with *poscount and did further changes for this pointer data type. Then the code works well. Why this is so?
#include <iostream>
using namespace std;
const int SIZE = 100;
void seqsearch(int[], int, int, int[], short);
int main() {
int array[SIZE], indices[SIZE];
int num, value;
short count = 0;
cerr << " Give number of elements in array : ";
cin >> num;
cerr << " Key in the array elements ";
for(int i = 0; i < num; i++) cin >> array[i];
cout << endl;
cerr << " Give the value to be searched : " << endl;
cin >> value;
cout << endl;
seqsearch(array, num, value, indices, count); // void function
if(count >= 0) {
cout << value << " found in array " << count << " times"
<< " at index positions " << endl;
for(int i = 0; i < count; i++) cout << indices[i] << " ";
cout << endl;
} else
cout << value << " not found in array " << endl;
return 0;
}
void seqsearch(int arr[], int size, int elm, int pos[], short poscount) {
int i, item;
poscount = 0;
for(i = 0; i < size; i++) {
if(arr[i] == elm) {
pos[poscount] = i;
poscount = poscount + 1;
}
}
return;
}
The function seqsearch is supposed to return the result in pos and poscount, but the function takes poscount by-value which means that any changes you make to poscount inside the function, will be local to the function and not visible from the call site.
If you change the function to take the argument by-reference, the changes you make inside the function will actually be made to the variable used in the call to the function. Like this:
seqsearch(int arr[], int size, int elm, int pos[], short& poscount) // note short&
The int pos[] does not have the same problem because arrays decay into pointers, so it could have been int* pos instead - and that pointer points at the same array that you passed in at the call site.
Also note that the check after the call will make the program display "found in array" even if it isn't found in the array because the condition checks if count is zero or greater than zero.
if(count >= 0) { // should be if(count > 0) {
Suggestions unrelated to the problem in your question:
When the number of elements is not known at the time you compile your program, prefer to use a container which can grow dynamically, like a std::vector<int>. In your program you have a hardcoded limit of SIZE number of elements, but:
You will rarely use all of them.
You do not check if the user wants to enter more than SIZE elements and your program will gladly try to write out of bounds - which would cause undefined behavior.
Divide the program's subtasks into functions. It'll be easier to search for bugs if you can test each individual function separately.
Check that extracting values from std::cin actually succeeds.
int number;
if(std::cin >> number) { /* success */ } else { /* failure */ }
Check that the values entered makes sense too.
int wanted_container_elements;
if(std::cin >> wanted_container_elements && wanted_container_elements > 0) {
/* success */
} else {
/* failure */
}
poscount (or count in the context of the caller) in your code seems to be expected to be an output parameter.
To modify the passed value you must either have its address (a pointer) or a reference to the value.
Currently you are using "pass-by-value", meaning that poscount is a copy of count.
The original count stays untouched.
My personal favorite would be to return the value instead of using an out-parameter:
short seqsearch(int arr[], int size, int elm, int pos[]) {
int i, item;
short poscount = 0;
for(i = 0; i < size; i++) {
if(arr[i] == elm) {
pos[poscount] = i;
poscount = poscount + 1;
}
}
return poscount;
}
count = seqsearch(array, num, value, indices);
Alternatively you can use a reference to manipulate the out-parameter:
void seqsearch(int arr[], int size, int elm, int pos[], short& poscount) {
int i, item;
poscount = 0;
for(i = 0; i < size; i++) {
if(arr[i] == elm) {
pos[poscount] = i;
poscount = poscount + 1;
}
}
return;
}
seqsearch(array, num, value, indices, count);
And, as you already tried, you can also solve this by passing a pointer to the value:
void seqsearch(int arr[], int size, int elm, int pos[], short* poscount) {
int i, item;
*poscount = 0;
for(i = 0; i < size; i++) {
if(arr[i] == elm) {
pos[*poscount] = i;
*poscount = *poscount + 1;
}
}
return;
}
seqsearch(array, num, value, indices, &count);
When you pass your posscount argument, you pass a copy to the count variable in main, not the variable itself. That's why it works, when you pass it by pointer. You can also pass by reference. https://www.includehelp.com/cpp-tutorial/argument-passing-with-its-types.aspx

Problem creating and returning jagged array (error std::bad_array_new_length)

For this homework problem, we need to create a new jagged array with the code provided by our professor, print the array, and calculate the max, min, and sum of the array's contents. We are only allowed to edit the createAndReturnJaggedArray() and printAndThenFindMaxMinSum(int**,int*,int*,int*) functions, as the rest of the code was provided for us so we could check that we get the correct output.
I'm able to get the program to run, however after printing an initial string it terminates the program giving me the error terminate called after throwing an instance of 'std::bad_array_new_length' what(): std::bad_array_new_length. I believe the problem is in my creation of the jagged array and my allocation of memory for the columns part of the array, however I used the notes we were given as reference and have no idea where the problem is coming from. The entire program is provided below. Thanks for any help!
EDIT/NOTE: We haven't learned vectors yet so we're not allowed to use them.
#include <iostream>
#include <climits>
using namespace std;
class JaggedArray {
public:
int numRows;
int *numColumnsInEachRow;
JaggedArray() {
numRows = 11;
numColumnsInEachRow = new int[numRows];
for (int i = 0; i < numRows; i++) {
if (i <= numRows / 2) {
numColumnsInEachRow[i] = i + 1;
} else {
numColumnsInEachRow[i] = numRows - i;
}
}
readComputeWrite();
}
int **createAndReturnJaggedArray() { // COMPLETE THIS FUNCTION
int **A = new int*[numRows];
for(int i=0;i<numRows;i++){ //allocate columns in each row
A[i] = new int[numColumnsInEachRow[i]];
for(int j=0;j<numColumnsInEachRow[i];j++){
if(i <= numRows/2)
A[i][j] = (i + j);
else
A[i][j] = -1 * (i+j);
}
}
return A;
}
void printAndThenFindMinMaxSum(int **A, int *maxPtr, int *minPtr, int *sumPtr) { // COMPLETE THIS FUNCTION
maxPtr = new int[INT_MIN];
minPtr = new int[INT_MAX];
sumPtr = 0;
for(int i=0;i<numRows;i++){
for(int j=0;j<numColumnsInEachRow[i];j++){
//1. print array
if (j == (numColumnsInEachRow[i]-1))
cout << A[i][j] << endl;
else
cout << A[i][j] << " ";
//2. compute max, min, and sum
sumPtr += A[i][j];
if (A[i][j] > *maxPtr)
maxPtr = new int[A[i][j]];
if (A[i][j] < *minPtr)
minPtr = new int[A[i][j]];
}
}
}
void print(int max, int min, int sum) {
cout << endl;
cout << "Max is " << max << "\n";
cout << "Min is " << min << "\n";
cout << "Sum is " << sum << "\n";
}
void readComputeWrite() {
int max, min, sum;
int **A = createAndReturnJaggedArray();
cout << "*** Jagged Array ***" << endl;
printAndThenFindMinMaxSum(A, &max, &min, &sum);
print(max, min, sum);
}
};
int main() {
JaggedArray jaf;
return 0;
}
As #user4581301 hints at, your problem is in printAndThenFindMinMaxSum. Simply changing it to the below solves your problem:
void printAndThenFindMinMaxSum(int **A, int &maxPtr, int &minPtr, int &sumPtr) { // COMPLETE THIS FUNCTION
maxPtr = INT_MIN;
minPtr = INT_MAX;
sumPtr = 0;
.
.
.
sumPtr += A[i][j];
if (A[i][j] > maxPtr)
maxPtr = A[i][j];
if (A[i][j] < minPtr)
minPtr = A[i][j];
}
}
}
We also need to change readComputeWrite to:
void readComputeWrite() {
int max, min, sum;
int **A = createAndReturnJaggedArray();
cout << "*** Jagged Array ***" << endl;
printAndThenFindMinMaxSum(A, max, min, sum);
print(max, min, sum);
}
I would also recommend changing the name minPtr, maxPtr, and sumPtr to something more appropriate, as they aren't pointer at this point and represent primitive values.
You will note, that I changed pointers to references as this is a more natural adaptation for this type of operation. Essentially, passing by reference allow the user to operate on the passed value in a straightforward manner without the tedious task of making sure you dereference things at the appropriate time. It also allows one to operate in a less error prone manner.
Again, as #user4581301 shrewdly points out, the intent of this assignment was probably to deal with pointers. As such, there are a few things that need to be changed if the OP cannot use references. Observe:
void printAndThenFindMinMaxSum(int **A, int *maxPtr, int *minPtr, int *sumPtr) { // COMPLETE THIS FUNCTION
*maxPtr = INT_MIN; // Make sure to deference before assigning
*minPtr = INT_MAX; // Make sure to deference before assigning
*sumPtr = 0; // Make sure to deference before assigning
for(int i=0;i<numRows;i++){
for(int j=0;j<numColumnsInEachRow[i];j++){
//1. print array
if (j == (numColumnsInEachRow[i]-1))
cout << A[i][j] << endl;
else
cout << A[i][j] << " ";
//2. compute max, min, and sum
*sumPtr += A[i][j]; // Make sure to deference before assigning
if (A[i][j] > *maxPtr) // Make sure to deference before comparing
*maxPtr = A[i][j]; // Make sure to deference before assigning
if (A[i][j] < *minPtr) // Make sure to deference before comparing
*minPtr = A[i][j]; // Make sure to deference before assigning
}
}
}
And the readComputeWrite can stay unaltered from the OP's original attempt.
In the OP's code, they are mainly forgetting to deference before assigning/comparing.

Incorrect Variable output with Vector Class C++

My output for the call to the temporary array size wont correctly output. It resizes as according, but I can't get the MAX to display the new value of the new array. My error is within the Resize function within the class.
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <ctime>
using namespace std;
class VectorClass {
private:
int * Vector;//This will be our resizeable array
int Size; //Keep track of vector current size
int MAX=10;
int growth = 5;
int num;
int Resize(int growth, int MAX);
public:
VectorClass(int growth, int Size);
~VectorClass();
int AddItem(int num);
void RemoveItem();
void Print(void);
};
VectorClass::VectorClass(int growth, int Size)
{
Size = 10;
growth = 5;
Vector = new int[Size];
}
VectorClass::~VectorClass()
{
cout << "Destructor was called." << endl;
}
//Will insert num into the vector at the current open position
int VectorClass::AddItem(int num)
{
Vector[Size] = num;
Size++; //Indicate that there isnt as much free space
if (Size == MAX)
{
Resize(Size, MAX);
}
Print();
return num;
}
//Get rid of the most recently added item
void VectorClass::RemoveItem()
{
Size--; //Tricks the vector into one fewer elements in it it currently does
Print();
}
int VectorClass::Resize(int growth, int MAX)
{
cout << "Array is full! Resizing the Array!" << endl;
//Step 1: make a copy
int * temp = new int[MAX]; //Make a new array, same size as exiting array
//loop that copies the original into the copy
for (int i = 0; i<MAX; i++)
{
temp[i] = Vector[i];
}
//Step 2: Delete the original
delete[] Vector; //Deletes all elements in the array Vector from the Heap
//Step 3: Make a bigger vector
Vector = new int[MAX + growth];
//Step 4: Reverse the copy and record the size change
for (int i = 0; i<MAX; i++)
{
Vector[i] = temp[i];
}
MAX = MAX + growth;
//Step 5: Delete the copy
delete[] temp;
cout << "Resize was called.\n" << endl;
return MAX;
}
void VectorClass::Print()
{
cout << "*******************************************************" << endl;
for (int i = 0; i< Size; i++)
{
cout << Vector[i] << endl;
}
cout << "Size = " << Size << "\tMAX = " << MAX << "\t Growth = " << growth << endl << endl;
cout << "*******************************************************" << endl;
}
int main(void)
{
VectorClass V(5,10);
for (int i = 0; i <= 4; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector #1
V.Print();
//Delete 2 Items
V.RemoveItem();
V.RemoveItem();
//Add 9 random Numbers
for (int i = 0; i <= 8; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector
V.Print();
system("pause");
return 0;
}
Several things are wrong with you code. The first one, probably not the one you care about most, is that you never free the memory. You should do it in your destructor, or even better use a std::unique_ptr to handle your memory.
Now, i believe you are yourself confused about your own variables. I see that you possess a variable member named num that you never use. Even worse, you have a parameter in AddItem with the same name. Are you sure it does what you want? The same is true for growth. I would advise you to name your member variable differently, so that you know what they are quickly. I prefixe them with "m_" for example, but you can do as you wish.
You do not need to declare your function parameters inside your class. Only in the function prototype.
Then, in your AddItem function, you use your variable Size to determine where to add the new element, but you initialize your array with it too, which means that not only you do not add your elements at the beginning of your array, you try to write them in memory you do not own!
I could continue for a long time. I am sorry but it only appears to me that you do not know C++ at all. You should go learn the basics again, and maybe start with an easier project to begin your C++ learning.
Good luck :-)

C++ array, counting repeats in a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've been learning c++ recently (coming from java) and I am attempting to count the number of repeating values in an array. For some reason the array is not being properly passed to my counter function.
#include <iostream>
#include <time.h>
using namespace std;
//quicksort for int arrays, left should be left index (0), right is right index(last one)
void quSort(int input[], int left, int right);
//binary search will return the index of the target or -1 if not found
int biSearch(int input[], int target, int iLeft, int iRight);
//count reapeats in the array with biSearch
int countRepeats(int input[], int target);
int main()
{
srand((unsigned int) time(0));
int test[1000];
//generate 100 random numbers under 1000
for(int i = 0; i < 1000; i++)
test[i] = rand()%1000;
//output test original
cout << "orig: ";
for(int i = 0; i < sizeof(test)/sizeof(*test); i++)
{
cout << test[i] << " ";
}
cout << endl << endl;
//sorting
quSort(test,0,( (sizeof(test)/sizeof(*test))-1));
cout << "sorted: ";
for(int i = 0; i < sizeof(test)/sizeof(*test); i++)
{
cout << test[i] << " ";
}
//binary search test
int target;
int iTarget;
cout << "\nenter target: ";
cin >> target;
iTarget = biSearch(test,target,0,sizeof(test)/sizeof(*test));
cout << "\n the target is at index: " << iTarget << " :: test[" << iTarget << "] = " << test[iTarget];
//count repeats
cout << "\nWith " << countRepeats(test,target) << " repeats";
system("pause");
return 0;
}
//quicksort function; effiecent array sorter; important for furture array analysis!!!
void quSort(int input[], int left, int right)
{
int pivot = input[(left+right)/2];
int l = left;//to control loop
int r = right;
while(l <= r)//will get smaller over iterations
{
int placeHold;// for use in swap, temp number
//finds value higher than the pivot from left
while(input[l] < pivot)
l++;
//find value lower than pivot on right
while(input[r] > pivot)
r--;
//swapper
if(l <= r)
{
//if the value greater than pivot is to the left of the value
//lessser than pivot
placeHold = input[l];
input[l] = input[r];
input[r] = placeHold;
l++;
r--;
}
//recursion to sort whole array until l=r
if(left<r)
quSort(input, left, r);
if(l < right)
quSort(input, l , right);
}
}
//binary search function; array MUST be sorted
int biSearch(int input[], int target, int iLeft, int iRight)
{
if(iLeft > iRight)
return -1;
else
{
int iMid = ((iLeft+iRight)/2);
if(input[iMid] > target)
return biSearch(input, target, iLeft, iMid-1);
else if(input[iMid] < target)
return biSearch(input, target, iMid+1, iRight);
else
return iMid;//target found
}
}
//Must be sorted
int countRepeats(int *input, int target)
{
int holder[sizeof(input)/sizeof(*input)];
int biSResult;
int counter = 0;
biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input));
//bug test
cout<<"c++" << biSResult << "c++";
//
while(biSResult != -1)
{
holder[biSResult] = target;
counter++;
input[biSResult] = 0;
quSort(input,0,( (sizeof(input)/sizeof(*input))-1));
biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input));
}
biSResult = biSearch(holder,target,0,sizeof(holder)/sizeof(*holder));
while(biSResult != -1)
{
input[biSResult] = target;
holder[biSResult] = 0;
quSort(holder,0,( (sizeof(holder)/sizeof(*holder))-1));
biSResult = biSearch(input,target,0,sizeof(holder)/sizeof(*holder));
}
return counter;
}
If anyone knows why this is happening it would be a great help
There are several issues in countRepeats():
(1) as already mentionned in other answers, there is an error in the way parameters are passed. There is no way to calculate the size of the array in the function whether you use int* or int[]. So best pass arguments to this function as you do in quSort() by giving not only the array address but also a start and an end counter.
(2) your programme will crash if the user would asks for a target of 0 : your while(biSResult != -1) would loop for ever.
(3) this function sorts again and again the array. This seems to be pretty bad for performance. Why don't you make use of the fact that the array is already sorted ? You could start counting from the found index . Just think that you'd have to count before and after this position, because you're not sure that iTarget is the very first occurence. This could look like:
int countRepeats(int input[], int pos, int start, int end )
{
if (pos<start || pos>=end) // you never know !
return 0;
int counter = 1;
for (int i=pos-1; i>=start && input[i]==input[pos]; i--)
counter++;
for (int i=pos+1; i<end && input[i]==input[pos]; i++)
counter++;
return counter;
}
By the way, I've tested it and it works. You just have to adapt the prototype and call it in your main function with
cout << "\nWith " << countRepeats(test, iTarget, 0,
sizeof(test)/sizeof(*test) ) << " repeats";
The first parameter of function countRepeats declared as
int countRepeats(int *input, int target)
that is it has type int *
So
sizeof(input)/sizeof(*input)
is equivalent to
sizeof( int * )/sizeof( int )
If for example sizeof( int * ) is equal to 4 and sizeof( int ) also is equal to 4 then the expression will be equal to 1. That is the value of the expression does not depend on how many elements the array that was passed to the function as argument has.
You should pass the size of the array explicitly as an argument of the function. So th function should be declared as
int countRepeats(int *input, int n, int target);
Or you could declare the first parameter of the function as a reference to array.
You can't calculate the size of an array inside a function that received a pointer to the array. This is because the value of sizeof(input) inside your counting function is just going to return the size of a pointer to an int. So sizeof(input)/sizeof(*input) will always be 1.
If you instead calculate the size of the array and store it as an int in main, then pass that to your counting function it should work. So changing your counting function call to:
cout << "\nWith " << countRepeats(test,target,sizeof(test)/sizeof(*test)) << " repeats";
And your countRepeats declaration to:
int countRepeats(int input[], int target, int size);
Then inside your countRepeats definition, everywhere you had (sizeof(input)/sizeof(*input)) you can just say size:
int countRepeats(int *input, int target, int size)
{
int holder[size];
int biSResult;
int counter = 0;
biSResult = biSearch(input,target,0,size);
//bug test
cout<<"c++" << biSResult << "c++";
//
while(biSResult != -1)
{
holder[biSResult] = target;
counter++;
input[biSResult] = 0;
quSort(input,0,(size-1));
biSResult = biSearch(input,target,0,size);
}
biSResult = biSearch(holder,target,0,size);
while(biSResult != -1)
{
input[biSResult] = target;
holder[biSResult] = 0;
quSort(holder,0,(size-1));
biSResult = biSearch(input,target,0,size);
}
return counter;
}
But you should really just use std::vector instead. Could have had std::vector<int> test (1000); and since your countRepeats changes your array through calls to quSort you can pass the vector as a reference (just as efficient as passing a pointer, allows changes to affect the original): int countRepeats(std::vector<int>&, int target); and you can always find its size by test.size()

Mode Function: How does it work? [duplicate]

This question already has answers here:
C++: Mean Median and Mode
(3 answers)
Closed 9 years ago.
I've recently created a C++ program to find the mean median and mode of an array of values.
I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100% sure of how to wrap my head around what is actually happening within the function.
A better understanding of what is happening in the mode function would be greatly appreciated.
This is my code so far:
#include <iostream>
using namespace std;
void mode(int[], int);
void mean(int[], int);
void sort(int[], int);
void median(int[], int);
int main()
{
int array[15];
float total, mode;
int n = 15;//number of elements in array
//fill in the value of array
for(int i=0; i<n; i++){
cout << "fill in the "<< i+1 << " number. :";
cin >> array[i];
}
sort(array, n);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mean(int new_array[], int num){
//GET TOTAL & CALCULATE MEAN
float total = 0;
for(int i=0;i<num; i++){
total += new_array[i];
}
cout << "The mean is " << total/num << endl;
mode(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void median(int new_array[], int num){
//CALCULATE THE MEDIAN (middle number)
if(num % 2 != 0){// is the # of elements odd?
int temp = ((num+1)/2)-1;
cout << "The median is " << new_array[temp] << endl;
}
else{// then it's even! :)
cout << "The median is "<< new_array[(num/2)-1] << " and " << new_array[num/2] << endl;
}
mean(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mode(int new_array[], int num) {
int* ipRepetition = new int[num];
// alocate a new array in memory of the same size (round about way of defining number of elements by a variable)
for (int i = 0; i < num; i++) {
ipRepetition[i] = 0;//initialize each element to 0
int j = 0;//
while ((j < i) && (new_array[i] != new_array[j])) {
if (new_array[i] != new_array[j]) {
j++;
}
}
(ipRepetition[j])++;
}
int iMaxRepeat = 0;
for (int i = 1; i < num; i++) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
}
}
cout<< "The mode is " << new_array[iMaxRepeat] << endl;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void sort(int new_array[], int num){
//ARRANGE VALUES
for(int x=0; x<num; x++){
for(int y=0; y<num-1; y++){
if(new_array[y]>new_array[y+1]){
int temp = new_array[y+1];
new_array[y+1] = new_array[y];
new_array[y] = temp;
}
}
}
cout << "List: ";
for(int i =0; i<num; i++){
cout << new_array[i] << " ";
}
cout << "\n";
median(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
At a very high level, first it leaks memory.
int* ipRepetition = new int[num];
allocates a new array and nothing calls delete[] afterwards.
Second, it fills the new array with zeros by walking through the size of the original array of data one at a time up to the current place it has got toi, if (new_array[i] != new_array[j]) (which it checks twice just to be sure) it increments j.
If it finds a match or gets to the end of the elements it has filled so far it adds one to the ipRepetition array in position j.
This is trying to keep track of how often the number in new_array at index i is used.
The next for loop then walks through these numbers to find the index i largest value.
It then prints the value in the original array at this index.
It might be more useful if the function were changed to return the value. Since it is C++ you could use a vector instead to avoid the memory leak.
You have two parallel arrays: one for the numbers and one to count the repetitions.
The way repetitions are counted is by iterating through the list up to the current number, stopping at the first match and incrementing its repetition count. Say you have the following array:
5 5 2
On the first iteration, you set the first value of the parallel array to 0, then end up breaking out of the inner loop immediately and incrementing it, leaving you with:
1 ? ?
in the parallel array. In the second iteration, the loop will again break on the first item, because new_array[1] == new_array[0] == 5. So you'll be left with:
2 0 ?
...and of course in the third iteration the third value will end up set to 1.
If you still have difficulty understanding, you can think of it like giving one "point" to each number in the original list, then moving the points backwards to the first instance of each number. You could try this on paper even.