# Could someone explain how the below c++ code works....??

Printable View

• May 28th, 2012, 10:41 PM
mrexp21
Could someone explain how the below c++ code works....??
Code to determine mode .

double mode (double numbers [], int i)
{
int* Repetition = new int[n];
for (int i = 0; i < n; ++i)
{
Repetition[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (numbers[i] != numbers[j]))
{
if (numbers[i] != numbers[j])
{
++j;
}
}
++(Repetition[j]);
}
int mode = 0;
for (int i = 1; i < n; ++i)
{
if (Repetition[i] > Repetition[mode])
{
mode= i;

}
}
cout << "Mode\t\t\t\t"<< numbers[mode]<<endl<<endl;
return 0;
}
• May 28th, 2012, 11:47 PM
Paul McKenzie
Re: Could someone explain how the below c++ code works....??
Quote:

Originally Posted by mrexp21
Code to determine mode .

1) Your question is not specific to Visual C++. It should have been posted in the non-Visual C++ forum.

2) Use code tags when posting code. The code you posted is unformatted and almost unreadable. Look here:
Code:

```double mode (double numbers [], int i) {         int* Repetition = new int[n];         for (int i = 0; i < n; ++i)         {                 Repetition[i] = 0;                 int j = 0;                 bool bFound = false;                 while ((j < i) && (numbers[i] != numbers[j]))                 {                         if (numbers[i] != numbers[j])                         {                             ++j;                         }                 }               ++(Repetition[j]);         }         int mode = 0;         for (int i = 1; i < n; ++i)         {                 if (Repetition[i] > Repetition[mode])                 {                       mode= i;                 }         }         cout << "Mode\t\t\t\t"<< numbers[mode]<<endl<<endl;         return 0; }```
See how that looks?

3) The person that wrote this code needs to learn proper C++ handling of dynamically allocated memory. The function leaks memory as there is no call to delete[].

4) Where did the "n" come from here?
Code:

`int* Repetition = new int[n];`
It isn't declared anywhere. So no one can tell you how the function works, since it can't be compiled due to syntax errors.

Did you mean this?
Code:

`double mode (double numbers [], int i)  // should the "i" be really "n"?`
If so, then you must have attempted to type in your code when you posted your message. Don't type in your code -- copy and paste the code from your editor into the message.

Regards,

Paul McKenzie
• May 28th, 2012, 11:57 PM
mrexp21
Re: Could someone explain how the below c++ code works....??
This is my full code....

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int n;
double mean (double numbers[],int );
double variance_stddeviatian (double numbers[], int );
double median (double numbers [], int );
double mode (double numbers [], int );

void main()
{
double numbers[50];
int i=0;

cout<< "Please enter the total number of data (n) : ";
cin>>n;
cout<<endl;

ifstream inputFile("data.txt");

if(!inputFile)
{
cerr<<"File could not be opened\n";
exit(1);
}

while(inputFile>>numbers[i])
{
cout<<numbers[i]<<" ";
i++;
}
cout<<"\n________________________________________________________________________________"<<endl;
cout<< "\n\nCentral Numerical Measure\t Value"<<endl;
cout<<"**************************\t********"<<endl;

mean(numbers,n);
median (numbers,n);
variance_stddeviatian (numbers, n);
mode (numbers,n);

}

double mean (double numbers[],int i )
{
double sum = 0;
for (int i=0; i<n; i++)
{
sum += numbers [i];
}
cout << "\n\nMean\t\t\t\t"<<sum/n<<endl<<endl;
return 0;
}

double variance_stddeviatian (double numbers[], int i)
{
double sum = 0;
double variance=0;
for (int i=0; i<n; i++)
{
sum += numbers [i];
variance += pow(numbers[i],2);
}
cout << "Variance\t\t\t"<<((variance)-(pow (sum,2)/n))/(n-1)<<endl<<endl;
cout << "Standard Deviation\t\t"<<sqrt(((variance)-(pow (sum,2)/n))/(n-1))<<endl<<endl;
return 0;
}

double median (double numbers [], int i)
{
int x, y;
double temp;

for (x=0; x<n; x++)
for (y=1; y<n; y++)
if (numbers [x] > numbers [y])
{
temp= numbers [x];
numbers [x] = numbers [y];
numbers [y]= temp;
}

double median = 0;
if ((n % 2) == 0)
{
median = (numbers[n/2] + numbers[(n/2) + 1])/2;
}
else
{
median = numbers[n/2];
}
cout << "Median\t\t\t\t"<<median<<endl<<endl;
return 0;
}

double mode (double numbers [], int i)
{
int* Repetition = new int[n];
for (int i = 0; i < n; ++i)
{
Repetition[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (numbers[i] != numbers[j]))
{
if (numbers[i] != numbers[j])
{
++j;
}
}
++(Repetition[j]);
}
int mode = 0;
for (int i = 1; i < n; ++i)
{
if (Repetition[i] > Repetition[mode])
{
mode= i;

}
}
cout << "Mode\t\t\t\t"<< numbers[mode]<<endl<<endl;
return 0;
}
• May 28th, 2012, 11:59 PM
Paul McKenzie
Re: Could someone explain how the below c++ code works....??
Quote:

Originally Posted by mrexp21
This is my full code....

You still did not use code tags when posting code. It looks even worse -- please go back and fix your post so that you include code tags.

And why are you asking us what it does? Did you write the code? If you did, then you don't know what you wrote?

Regards,

Paul McKenzie