I have taken interest an little in c++ some in my freetime. If more than one num
ID: 674930 • Letter: I
Question
I have taken interest an little in c++ some in my freetime.
If more than one number appears as the mode how would you get it
to return those as well.
//Mode Function
//In statistics, the mode of a set of values is the value that occurs
//most often or with the grearest frequency.
//Write a function that accepts
//as arguments the following :
//A) An array of integers
//B) An integer that indicates the number of elements in the array
//The function should determine the mode of the array.
//That is, it should determine which value in the array occurs most
//often.The mode is the value the function should return.If the
//array has no mode(none of the values occur more than once), the
//function should return -1. (Assume the array will always contain
//nonnegative values.)
//Demonstrate your pointer
//prowess by using pointer notation instead of array notation in
//this function.
#include
using namespace std;
int *getArray(int);
int getModeValue(int*, int);
void displayArray(int*, int);
int main()
{
int* arrayPointer;
int modeValue;
int integers;
//Prompt the user for the number of integers
cout << "Enter the number of elements ";
cin >> integers;
arrayPointer = getArray(integers);
cout << endl;
displayArray(arrayPointer, integers);
modeValue = getModeValue(arrayPointer, integers);
cout << " ";
if (modeValue == -1)
{
cout << "None of the values appear more than once " << endl;
}
else
cout << "The following occurs the most " << modeValue << endl;
//delete the dynamic array
delete[]arrayPointer;
system("PAUSE");
return 0;
}
int *getArray(int numOfIntgrs)
{
int*arrayPtr = new int[numOfIntgrs];
for (int i = 0; i < numOfIntgrs; i++)
{
cout << "What is element " << i + 1 << " " << endl;
cin >> *(arrayPtr + i);
}
return arrayPtr;
}
int getModeValue(int*arrayPtr, int numOfIntgrs)
{
int mode = -1;
int current;
int count;
//Outer loop to get each integer from the array
for (int i = 0; i < numOfIntgrs; i++)
{ //get each integer from the array
current = *(arrayPtr + i);
count = -1
//Inner loop to count the number of occurences
for (int j = 0; j < numOfIntgrs; j++)
{
//compare with courrent element
if (current == *(arrayPtr + j))//arrayPtr[j]
count++;
}
if (count>1 && count>mode)
mode = current;
}
return mode;
}
void displayArray(int*arrayPtr, int numOfIntgrs)
{
for (int i = 0; i < numOfIntgrs; i++)
{
cout << *(arrayPtr + i) << endl;
}
}
Explanation / Answer
#include<iostream>
using namespace std;
int *getArray(int);
int getModeValue(int*, int);
void displayArray(int*, int);
int main()
{
int* arrayPointer;
int modeValue;
int integers;
//Prompt the user for the number of integers
cout << "Enter the number of elements ";
cin >> integers;
arrayPointer = getArray(integers);
cout << endl;
displayArray(arrayPointer, integers);
modeValue = getModeValue(arrayPointer, integers);
cout << " ";
if (modeValue == -1)
{
cout << "None of the values appear more than once " << endl;
}
else
cout << "The following occurs the most " << modeValue << endl;
//delete the dynamic array
delete[]arrayPointer;
system("PAUSE");
return 0;
}
int *getArray(int numOfIntgrs)
{
int*arrayPtr = new int[numOfIntgrs];
for (int i = 0; i < numOfIntgrs; i++)
{
cout << "What is element " << i + 1 << " " << endl;
cin >> *(arrayPtr + i);
}
return arrayPtr;
}
int getModeValue(int*arrayPtr, int numOfIntgrs)
{
int prev_mode = 0;
int mode = -1;
int current;
int count;
//Outer loop to get each integer from the array
for (int i = 0; i < numOfIntgrs; i++)
{ //get each integer from the array
current = *(arrayPtr + i);
count = 0;
//Inner loop to count the number of occurences
for (int j = 0; j < numOfIntgrs; j++)
{
//compare with courrent element
if (current == *(arrayPtr + j))//arrayPtr[j]
count++;
}
if (count>1 && count>prev_mode)
{
mode = current;
prev_mode = count;
}
}
return mode;
}
void displayArray(int*arrayPtr, int numOfIntgrs)
{
for (int i = 0; i < numOfIntgrs; i++)
{
cout << *(arrayPtr + i) << endl;
}
}