Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions P1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>

int main()
{
using namespace std;
cout<<"Enter the number n: ";
int n;
cin>>n;
double* p=new double[n];
for (int i=0;i<n;i++)
p[i]=1.0/n;
for (int j=0;j<n;j++)
cout<<"p["<<j<<"] : "<<p[j]<<endl;
cout<<endl;
delete []p;
return 0;
}
79 changes: 79 additions & 0 deletions P10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// P10.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
const int nRow = 4;
const int nCol = 5;
const double pStart = 0.7;
const double pSenseCorrect = 0.7;

int main()
{
using namespace std;
double p[nRow][nCol];
string Measurements[nRow][nCol];
string world[nRow][nCol] = {{ "red","green","green","red","red" },
{ "red","red","green","red","red" },
{ "red","red","green","green","red" },
{ "red","red","red","red","red" }, };
double prior_value = (1 - pStart) / (nRow * nCol - 1);
for (int i = 0; i < nRow; i++)
for (int j = 0; j < nCol; j++)
p[i][j] = prior_value;
p[2][1] = pStart;
for (int i = 0; i < nRow; i++)
for (int j = 0; j < nCol; j++)
Measurements[i][j] = "green";
int hit;
double sum = 0;
double q[nRow][nCol];
for (int ii = 0; ii < nRow; ii++)
for (int jj =0; jj< nCol; jj++)
{
if (Measurements[ii][jj] == world[ii][jj])
hit = 1;
else
hit = 0;
q[ii][jj] = p[ii][jj] * (hit * pSenseCorrect + (1 - hit) * (1 - pSenseCorrect));
sum = sum + q[ii][jj];
}
for (int i =0; i < nRow; i++)
for (int j = 0; j < nCol; j++)
q[i][j] = q[i][j] / sum;
double q_max = q[1][1];
int max_row =0;
int max_col = 0;
for (int i = 0; i < nRow; i++)
for (int j = 1; j < nCol; j++)
if (q[i][j] > q_max)
{
q_max = q[i][j];
max_row = i;
max_col = j;
}
cout<<"The Prior: "<<endl;
for (int i = 0; i < nRow; i++)
for (int j = 0; j < nCol; j++)
{
cout << setiosflags(ios::fixed) << setprecision(7) << "p[" << i << "][" << j << "] : " << p[i][j] << "\t";
if (j == nCol)
cout <<endl;
}
cout << endl << endl;
cout<<"The probability after sensing : "<<endl;
for (int i =0; i < nRow; i++)
for (int j =0; j < nCol; j++)
{
cout << setiosflags(ios::fixed) << setprecision(7) << "q[" << i << "][" << j << "] : " << q[i][j] << "\t";
if (j == nCol)
cout << endl;
}
cout << endl;
cout << "The largest probability is q[" << max_row << "]" << "[" << max_col << "] : " << q[max_row][max_col] << endl;
}



35 changes: 35 additions & 0 deletions P12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<iostream>
#include<string>
const double pCan = 0.001;
const double pNon = 0.999;
const double pPosCan = 0.8;
const double pPosNon = 0.1;

int main()
{
using namespace std;
string z;
double p[2];
double sum;
cout<<"Enter the result of test(positive or negative): ";
cin>>z;
if(z=="positive")
{
p[0]=pPosCan * pCan;
p[1]=pPosNon * pNon;
}
else
{
p[0]=(1 - pPosCan) * pCan;
p[1]=(1 - pPosNon) * pNon;
}
sum=p[0]+p[1];
p[0]=p[0]/sum;
p[1]=p[1]/sum;
cout<<"The probability of having cancer given the "<<z<<" test: "<<endl;
cout<<p[0]<<endl;
cout<<"The probability of cancer free given the "<<z<<" test: "<<endl;
cout<<p[1]<<endl;
return 0;
}

58 changes: 58 additions & 0 deletions P14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<iostream>
const int a=2;
const int b=4;

int main()
{
using namespace std;
cout<<"Enter the row of matrix p : ";// ����������
int row;
cin>>row;
cout<<"Enter the col of matrix p : ";
int col;
cin>>col;
double** p=new double* [row];
for (int i=0;i<row;i++)
p[i]=new double[col];
for (int i=0;i<row;i++) //�������p��Ԫ��
for (int j=0;j<col;j++)
cin>>p[i][j];
double sum1=0.0;
for (int ii=0;ii<row;ii++) //���
for (int jj=0;jj<col;jj++)
sum1=sum1+p[ii][jj];
double mean1;
mean1=sum1/(row*col); //��������
double** q=new double* [row];
for (int i=0;i<row;i++)
q[i]=new double[col];
for (int i=0;i<row;i++) //��a*p+b�Ľ����ֵ������q
for (int j=0;j<col;j++)
{
q[i][j]=a*p[i][j]+b;
}
double sum2=0.0;
for (int ii=0;ii<row;ii++) //���
for (int jj=0;jj<col;jj++)
sum2=sum2+q[ii][jj];
double mean2;
mean2=sum2/(row*col); //�������q����
cout<<"The result of a plus expectation of matrix p plus b: "<<a*mean1+b<<endl;
cout<<"The expectation of matrix q: "<<mean2<<endl;
for (int i=0;i<row;i++)
{
delete [] p[i];
p[i]=NULL;
}
delete [] p;
p=NULL;
for (int i=0;i<row;i++)
{
delete [] q[i];
q[i]=NULL;
}
delete [] q;
q=NULL;
return 0;
}

31 changes: 31 additions & 0 deletions P4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
int main()
{
using namespace std;
cout<<"Enter the number of array p: ";//��������p�ĸ���
int n;
cin>>n;
cout<<"Enter the moving steps u : ";//�����ƶ��ij���u
int u;
cin>>u;
double* p=new double[n];
double* q=new double[n];
for (int i=0;i<n;i++)
{
cout<<"p["<<i<<"] : ";
cin>>p[i];
}
cout<<"before moving : "<<endl;//����ƶ�ǰ������p
for (int j=0;j<n;j++)
cout<<"p["<<j<<"] : "<<p[j]<<endl;
for (int jj=0;jj<n;jj++)//����p���ƶ�����
q[(jj+u)%n]=p[jj];
cout<<"after moving : "<<endl;//����ƶ��������q
for (int ii=0;ii<n;ii++)
cout<<"q["<<ii<<"] : "<<q[ii]<<endl;
delete []p;
delete []q;
return 0;
}


55 changes: 55 additions & 0 deletions P5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
#include<vector>
#include<cmath>
double MOD(double,double);
const double pExact=0.8;
const double pOvershoot = 0.1;
const double pUndershoot = 0.1;

int main()
{
using namespace std;
cout<<"Enter the number of array p : ";
int n;
cin>>n;
double* p=new double[n];
double* q=new double[n];
for (int i=0;i<n;i++)
{
cout<<"p["<<i<<"] : ";
cin>>p[i];
}
cout<<"Enter the moving steps u : ";
int u;
cin>>u;
cout<<"before moving : "<<endl;
for (int j=0;j<n;j++)
cout<<"p["<<j<<"] : "<<p[j]<<endl;
for (int ii=0;ii<n;ii++)
{
q[ii]=p[int(MOD(ii-u,n))]*pExact;
q[ii]=q[ii]+p[int(MOD(ii-(u-1),n))]*pUndershoot ;
q[ii]=q[ii]+p[int(MOD(ii-(u+1),n))]*pOvershoot;
}
cout<<"after moving : "<<endl;
for (int jj=0;jj<n;jj++)
cout<<"q["<<jj<<"] : "<<q[jj]<<endl;
delete []p;
delete []q;
return 0;
}
double MOD(double x,double y)
{
long n;
double f;
if((x<0.000000000000001 && x>-0.000000000000001) || (y<0.000000000000001 && y>-0.000000000000001))return x;
f = x/y;
n = (long)f;
if(n>f) n=n-1;
f =x-n*y;
return f;

}



2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ The codes and slides for the first year graduate student course, the Fundamental

## 学生更新记录:

- 霍煜豪 邮箱:591144810@qq.com 学号:201730310153。刚开始学python,改的比较慢到目前才改了16个,而且改的比较烂。
- 姓名:罗响   学号:201730310128   邮箱:834943826@qq.com   改了P1、P4、P5、P10、P12、P13、P14这7个matlab例子为C++
43 changes: 43 additions & 0 deletions p13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "stdafx.h"
#include<iostream>

int main()
{
using namespace std;
cout << "Enter the row of matrix p : ";// ����������
int row;
cin >> row;
cout << "Enter the col of matrix p : ";
int col;
cin >> col;
double** p = new double*[row];
for (int i = 0; i<row; i++)
p[i] = new double[col];
for (int i = 0; i<row; i++) //�������P��Ԫ��
for (int j = 0; j<col; j++)
cin >> p[i][j];
double sum = 0.0;
for (int ii = 0; ii<row; ii++) //���
for (int jj = 0; jj<col; jj++)
sum = sum + p[ii][jj];
double mean;
mean = sum / (row*col); //��������
double m = 0.0;
double variance;
for (int i = 0; i<row; i++) //���㷽��
for (int j = 0; j<col; j++)
m = m + (p[i][j] - mean)*(p[i][j] - mean);
variance = m / (row*col);
cout << "The Expectation of matrix p: " << mean << endl;
cout << "The Variance of matrix p: " << variance << endl;
cout << "The Standard Deviation of matrix p: " << sqrt(variance) << endl;
for (int i = 0; i<row; i++)
{
delete[] p[i];
p[i] = NULL;
}
delete[] p;
p = NULL;
return 0;
}