Hey guys
I am trying to calculate statistics for an array
such as min,max,median... etc

and am not getting any answers or results although the program runs well.

here's the full code so far...

//Mostafa Abdel Moez 111606

//Calculating some common statistics for an array of integers.  

#include <iostream>

using namespace std;
const int arrSize = 10;
int arr[arrSize];


int arrMin(int , int)
{ 
   int min=arr[0]; 
   for(int i=1;i<arrSize;i++)
	{
		if(min>arr[i])
		{
			min=arr[i];
		
		}
			return min;
    
    }
}
int arrMax(int, int)
{ 
   int max=arr[0]; 
   for(int i=1;i<arrSize;i++)
	{
		if(max<arr[i])
		{
			max=arr[i];
		}
        return max;
  }
}
double arrMean(int, int)
{
    double sum = 0;
	double avr = 0;
	for (int i = 0; i < arrSize; i++)
	{
     	sum+=arr[i];
	   avr = sum/arrSize;
    }
    return avr;
}

void bubbleSort(int , int)
{
    int i, j, temp;
    for ( i = 0; i < arrSize; i++ )    // controls passes through the list 
    {
		for ( j = 0; j < arrSize - 1; j++ )   // performs adjacent comparisons 
		{
			if ( arr[ j ] > arr[ j+1 ] )   // determines if a swap should occur 
			{
				temp = arr[ j ];       // swap is performed 
				arr[ j ] = arr[ j + 1 ];
				arr[ j+1 ] = temp;
			}
		}
	}
}

double arrMedian(int, int)
{
      int middle;
      double medavr;
      double medavrB;
      middle = arrSize / 2;
      if ( middle % 2)
     {
     medavr = ((arr[middle]) + (arr[middle + 1])) / 2;
      return medavr;
      }
      else
      {
      medavrB = arr[middle + (1/2)];
      return medavrB;
      }
}
int arrMode(int, int)
{
int count[arrSize];
int mode=0;
for (int i=0; i<arrSize; i++) 
{
if (count[i] > mode)
mode = arr[i];
}
      
}


int main()
{
   cout<<"Mostafa Abdel Moez 111606\n\n";
   cout<<"               Calculating some common statistics for an array of integers.\n\n";
   arr[arrSize]=(1,2,3,4,5,6,7,8,9,10);
   cout<<"Array Min "<<endl;
   arrMin(arr[arrSize], arrSize);
    cout<<"Array Max "<<endl;
   arrMax(arr[arrSize], arrSize);
    cout<<"Array Mean "<<endl;
   arrMean(arr[arrSize], arrSize);
   bubbleSort (arr[arrSize], arrSize);
    cout<<"Array Median "<<endl; 
   arrMedian(arr[arrSize], arrSize);
    cout<<"Array Mode "<<endl;
   arrMode(arr[arrSize], arrSize);
   system("PAUSE");
return 0;
     
     
}

.

First, why is arr global? Does your assignment require it to be? You really should move arr to the local scope of main().
Second, your functions aren't defined properly. They are defined to work on a single element of the array, not the entire array. You will have to make them compatible with arrays.

Your individual algorithms seem okay, but you need to fix your scoping and your function definitions/implementations.

Unfortunately, the a pile of little but important numerical/coding errors.

In arrMean, you don't actually get the wrong answer (I think), but you don't need to calculate avr until after the loop which adds up sum.

arrMedium: if (middle % 2) implies that the remainder of middle / 2 is not zero. i.e. middle is odd. In that case the medium is just the value at middle,
however, you have ALSO calculated middle without regard to the fact that the test should be for the arrSize, e.g. if you have say 11 items, then the middle is 5. The
test should be on arrSize.

Additionally, this is strange medavrB = arr[middle + (1/2)]; It actually works, but I guess you don't know that 1/2 is evaluated to zero, since it is integer arithmatic.

arrMode is simply wrong, you are going to have to take a piece of paper and work out that the mode is the most frequent number. Run your algorithm on paper and you will hopefully see what you can do.

Finally you need to fix your compile errors in main. The most important one is this arr[arrSize]=(1,2,3,4,5,6,7,8,9,10); is actually not what you think.
The comma operator evaluates from left to right and the round brackets are only there in a mathematical sense. It is equivalent of this: arr[arrSize]=10; As hopefully you can see that is wrong.

So use an array initializer or a loop.

Hi, I am busy right now,so con't see your code.
But I solve same type of problem,you cn see it.Hopefully it is very helpful to you.
Copy this code and run it.After That you see the code,then you want to take help by this program. Here i use class,but don't worry,it is not much different between class function and normal function.You can take just idea from the function logic.

Great Regards
Sohel Rana
Bangladesh

#include<iostream.h>
#include<conio.h>
class Sorting
{
	private:
		int i,j,temp,a[10],large,small;
	public:
		void num();
		void array();
		void largest();
		void smallest();
		void average();
};

void Sorting::num()
{
	cout<<"Please Enter the Number :";
	for(i=0;i<10;i++)
		{
			cin>>a[i];
		}
}
void Sorting::array()
{
	for(i=0;i<10;i++)
		{

		for(j=i+1;j<10;j++)
			{
				if(a[i]>a[j])
					{
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
					}
			
			}
		}

	cout<<"\n\nAfter Sorting we get Resule : ";
	for(i=0;i<10;i++)
		cout<<"\t"<<a[i];
	
}
void Sorting::largest()
{
	large=0;
	for(i=0;i<10;i++)
		{
			if(a[i]>large)
				large=a[i];
		}
	cout<<"\n\nThe Largest Number = "<<large;
}
void Sorting::smallest()
{
	small=32765;
	for(i=0;i<10;i++)
		{
			if(a[i]<small)
				small=a[i];
		}
	cout<<"\n\nThe Smllest Number = "<<small;
}
void Sorting::average()
{
	int sum=0,count=0;
	float ave=1;
	for(i=0;i<10;i++)
		{
			sum=sum+a[i];
			count++;
		}
	ave=(float)sum/count;
	cout<<"\n\nThe Total Number = " <<sum;
	cout<<"\n\nThe Average Number = " <<ave;
}
void main()
{
	clrscr();
	Sorting value;
	value.num();
	value.array();
	value.largest();
	value.smallest();
	value.average();
	getch();
}

Thank you guys for your positive replys

first of all i made my arr global because i think it would be better if i do so .. just to make it seen by the entire program to use it for another arrays such as rand()

and about the Median issue i made the equation to look like that thinking of if there is the an even number of values so, median is the average of the two middle values thus, i have tried making the half (1/2) to .5
but it gives an error changing the double to int

i did face a problem with defining and implementing arrays when we use them in functions

when i made it like that int arrMin(int arr[] , int arrSize)

>>first of all i made my arr global because i think it would be better if i do so
This is rarely true. In fact, due the way you defined your functions, you're not even using the global array anyway. You've masked it with a local one.

>>i did face a problem with defining and implementing arrays when we use them in functions.
when i made it like that int arrMin(int arr[] , int arrSize)

That is the proper way to do it. You probably had your call wrong. Don't worry, you're not alone. Most newbies don't know how to use an array as an argument properly, even though they can create the parameter properly.

int arrMin(int arr[], int arrSize);  //function prototype

int min = arrMin(array[someIndex], arraySize); //this call is incorrect syntax

int min = arrMin(array, arraySize); //this is the correct syntax

An array is essentially a pointer, when using an array as an argument/parameter you need to send the pointer, not an actual object, to the function. When you apply an index to an array, you are dereferencing the pointer to get the data stored at the memory address pointed.
In the incorrect version you are sending a single int because you are using an index on the array, which dereferences the pointer.

i need it to be fixed ... am working on it 2 days till now :S

YEAH !!!
finally, i got it ..
the problem was the decelerations and calling the functions at main
Like Fbody said

and seting the arr with rand()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.