⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q1.cpp

📁 a simple code to implement to find mean mode &median.......
💻 CPP
字号:
//to find mean mode &median.......

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

#define MAX 50

int i,j,pos,n,arr[MAX],count[MAX];
void display();
void median();
void mode();
float mean();

void main()
{
	int op;
	float men;//mean
	char ans;
//	clrscr();
	cout<<"\nhow many numbers<50 you want to enter in the list\n";
	cin>>n;
	cout<<"\nenter the nos\n";
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	do
	{
		cout<<"\nselect task\n1=median\n2=mode\n3=display\n4=mean";
		cin>>op;
		switch(op)
		{
		case 1:
			median();
			//cout<<"median of the list entered is \t<<med<<endl;
			break;
		case 2:
			mode();
			//cout<<"mode of the list entered is \t<<mod<<endl;
			break;
		//case 3:
			//display();
			break;
		case 4:
			men=mean();
			cout<<"mean of the list entered is \t"<<men<<endl;
			break;
		default:
			cout<<"\nplease enter the right case\n";
		}
		cout<<"\ndo you want to continue 'y'\n";
		//fflush(stdin);
		cin>>ans;
	}while((ans=='y')||(ans=='Y'));
//	getch();
}
void mode()
{
	int arr1[50],cn,cut;
	cn=n;
	for(i=0;i<n;i++)
		arr1[i]=arr[i];
	
	for(i=0;i<cn;i++)
	{
		count[i]=1;//each count associated with its array
		for(j=i+1;j<cn;j++)
		{
			if(arr1[i]==arr1[j])
			{
				cut=j;
				count[i]++;
				for(;cut<cn-1;cut++)
					arr1[cut]=arr1[cut+1];
				
				j--;
				cn--;
			}
		}
		cout<<arr[i]<<" is repeated "<<count[i]<<" times";
	}
}
/*void display()
{
	printf("\n\n");
	for(i=0;i<n;i++)
		printf("%d element =\t%d\n",(i+1),arr[i]);
}*/
float mean()
{
	float sum=0;
	for(i=0;i<n;i++)
		sum=sum+arr[i];
	return(sum/n);
}
void median()
{
	int med;
	if(n%2==0)
	{
		med=n/2;
		cout<<"\nmedian of the list entered are \t"<<arr[med]<<" and "<<arr[med+1]<<endl;
	}
	else
	{
		med=(n+1)/2;
		cout<<"median of the list entered is \t"<<arr[med]<<endl;
	}
}

void sd()
{
	for(int i=0;i<n;i++)
	{
		var=var+(arr[1]*count[i]*count[i]);
	}
	for(int i=0;i<n;i++)
	{
		var2=var2+(count[i]*count[i]);
	}
	return(var/var2);
}//varience=sqrt sd;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -