Mean, Median and Mode c++

#include<iostream.h>
#include<conio.h>
int main()
{
int array[30],sum, n,median1,median2, mode,count[30];
int pos1,pos2;
sum=0;
float mean=0.0;
cout<<“enter n”;
cin>>n;
for(int a=0;a<n;a++)
{
cout<<“enter”;
cin>>array[a];
count[a]=0;
}
for(int b=0;b<n;b++)
{
sum=sum+array[b];
}
cout<<“sum is”<<sum;
int small,pos,temp;

for(int i=0;i<n-1;i++)
{
small=array[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(small>array[j])
{
small=array[j];
pos=j;
}
}
temp=array[pos];
array[pos]=array[i];
array[i]=temp;
}
mean=sum/n;
cout<<“\n mean is”<<mean;
if(n%2!=0)
{
pos1=pos2=(n+1)/2;
}
else
{
pos1=n/2;
pos2=n/2 +1;
}
median1=array[pos1];
median2=array[pos2];
cout<<” median is “<<median1<<“\t”<<median2;
for(int c=0;c<n; c++)
{
for(int d=0;d<n;d++)
{
if(array[c]==array[d])
{
count[c]++;
}
}
}
int modepos,max=count[0];
for(int e=1;e<n;e++)
{
if(max<count[e])
{
max=count[e];
modepos=e;
}
}
mode=array[modepos];
cout<<“mode is”<<mode;
getch();
return 0;
}

Linked List (menu driven)

#include<iostream.h>
#include<conio.h>

struct node
{
int data;
node *next;
}

void insert();
void deleterec();
void count();

void main()
{
clrscr();
int choice;
int n;
node *first,*last,*ptr;

first=new(node);
cout<<“enter the first data”;
cin>>first->data;
first->next=NULL;
ptr=first;

cout<<“enter the number of recors”;
cin>>n;

for(int a=0;a<n-1;a++)
{
last=new(node);
cout<<“\n enter data”;
cin>>last->data;
last-next=NULL;
first->next=last;
first=last;
}

do
{
cout<<” enter 1 for insertion
enter 2 for deletion
enter 3 for counting
enter 4 for exit”;
cin>>choice;

switch(choice)
{
case 1: insert();
break;
case 2: deleterec();
break;
case 3: count();
break;

default:cout<<“enter corrct choice”;
}

}while(choice!=4)

getch();
}

void insert(node *ptr)
{
int d;
cout<<“enter the data”;
cin>>d;
node *x;

if(d<ptr->data);
{
x=new(node);
x->data=d;
x->next=ptr;
}

else
{
while(d>ptr->data)
{
ptr=ptr->next;
}
x=new(node);
x->data=d;
x->next=ptr->next;
ptr=x;
}
}

void deleterec(node *ptr)
{
node *last;
last=NULL;
int temp,dlt;
cout<<“enter data to be deleted”;
cin>>dlt;

if(dlt==ptr->data)
{
temp=ptr->data;
ptr=ptr->next;\\delete statement to be used or not
}

else
{
while(dlt>ptr->data)
{
ptr=ptr->next;
last=ptr;
}
dlt=ptr->data;
last->next=ptr->next;
}
}

void count(node *ptr)
{
int count;
while(ptr!=NULL)
{
ptr=ptr->next;
count++;
}
cout<<“the number of entries is “<<count;
}

Hello world!

Welcome to WordPress.com! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!

Calendar

June 2012
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

Archives