Saturday, 25 February 2012

Different oops with arrays with functions

#include<iostream>
using namespace std;

// prototypes

void array_sum(int arr[],int size,int *ptrsum);
void array_max(int arr[],int size,int *ptrmax_index);
void array_min(int arr[],int size,int *ptrmin_index);
void array_sumeven(int arr[],int size,int *ptrsumeven);
void array_sumodd(int arr[],int size,int *ptrsumodd);
void array_reverse(int arr[],int size,int *ptrreverse);

int main()
{
int sum=0;
int max_index=0;
int min_index=0;
int sumeven=0;
int sumodd=0;
int reverse=0;
int const SIZE=10;
int array[SIZE];
cout<<"Enter the values in array : "<<endl;
for(int i=0;i<SIZE;i++)
{
cout<<"Enter number At "<<i<<" th index :";
cin>>array[i];
}
array_sum(array,SIZE,&sum);
array_max(array,SIZE,&max_index);
array_min(array,SIZE,&min_index);
array_sumeven(array,SIZE,&sumeven);
array_sumodd(array,SIZE,&sumodd);
array_reverse(array,SIZE,&reverse);
return 0;
}
// definition
// sum
void array_sum(int arr_sum[],int arr_size,int *ptrsum)
{
for(int j=0;j<=arr_size;j++)
{
*ptrsum = *ptrsum + arr_sum[j];
}
cout<<"Sum is = "<<*ptrsum<<endl;
}

// max
void array_max(int arr_max[],int arr_size,int *ptrmax_index)
{
for(int index=0;index<arr_size;index++)
{
if(arr_max[*ptrmax_index]<arr_max[index])
*ptrmax_index=index;
}
cout<<"Max Number is at = "<<*ptrmax_index<<" index"<<endl;
}
//min
void array_min(int arr_min[],int arr_size,int *ptrmin_index)
{
for(int index=0;index<arr_size;index++)
{
if(arr_min[*ptrmin_index]>arr_min[index])
*ptrmin_index=index;
}
cout<<"Min Number is at = "<<*ptrmin_index<<" index"<<endl;
}
//sumeven
void array_sumeven(int arr_sumeven[],int arr_size,int *ptrsumeven)
{
for(int i=0;i<arr_size;i++)
{
if(arr_sumeven[i]%2==0)
*ptrsumeven=*ptrsumeven + arr_sumeven[i];
}
cout<<"The Sum of Even numbers is = "<<*ptrsumeven<<endl;
}
//sumodd
void array_sumodd(int arr_sumodd[],int arr_size,int *ptrsumodd)
{
for(int i=0;i<arr_size;i++)
{
if(arr_sumodd[i]%2!=0)
*ptrsumodd=*ptrsumodd + arr_sumodd[i];
}
cout<<"The Sum of Odd numbers is = "<<*ptrsumodd<<endl;
}
//reverse
void array_reverse(int arr_reverse[],int arr_size,int *ptrreverse)
{
int temp;
int high = arr_size - 1;
for(int i=0;i<=arr_size/2;i++)
{
temp=arr_reverse[i];
arr_reverse[i]=arr_reverse[high];
arr_reverse[high]=temp;
high--;
}
cout<<"Reverse Array "<<endl;
for(i=0;i<arr_size;i++)
{
cout<<"The value at "<<i<<" th index is "<<arr_reverse[i]<<" "<<endl;
}
}

No comments:

Post a Comment