C++的实验记录(1)
#include <iostream>
using namespace std;
int *swap(int a[],int _size);//声明函数
int main()
{
int i,N;
cout << "please input the size of the array: " << endl;
cin >> N;//输入要输入的个数
int a[N];
cout << "Input array elements:" << endl;
for(i=0;i<N;i++)//用for循环来输入
{
cin >> a[i];
}
swap(a,N);//调用函数
cout << "the result of array sorting:" << endl;
for(int i=0;i<N;i++)//输出
{
cout << a[i] <<"\t";
}
return0;
}
int *swap(int a[],int _size)//函数定义
{
int i,j,temp;
for(j=0;j<_size-1;j++)
for(i=1;i<_size;i++)
{
if(a[i]<a[i-1])
{
temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
}
}
return a;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int n;
int main()
{
srand(time(NULL));
cout << "what do u want for the size of n?" << endl;
cin >> n;//
cout << "--------------------------------------------------------------------------------" ;
/* */
int **p,i,j;
p = newint*[n];
for(int i=0;i!=n;i++)
{
p[i] = newint[n];
}
for(i=0;i<n;i++)//
{
for(j=0;j<n;j++)
{
p[i][j] = rand()%100;//
}
}
for(i=0;i<n;i++)//
{
for(j=0;j<n;j++)
{
cout << p[i][j] << "\t";
}
}
cout << "--------------------------------------------------------------------------------" ;
int MAX =0;//
int x1,y1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(MAX<p[i][j])
{
MAX = p[i][j];
x1 = i;
y1 = j;
}
}
}
cout << "the largest one:" << endl;
cout << "[" << x1 <<"]" << "[" << y1 <<"]" << endl;
cout << endl;
int MIN =100;//
int x2,y2;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(MIN>p[i][j])
{
MIN = p[i][j];
x2 = i;
y2 = j;
}
}
}
cout << "the smallest one:" << endl;
cout << "[" << x2 <<"]" << "[" << y2 <<"]" << endl;
cout << endl;
int sum1 =0;//
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum1 += p[i][i];
}
}
cout << "÷˜∂‘Ω«œfl‘™Àÿ÷Æ∫Õ£∫" << sum1 << endl;
int sum2 =0;//
for(i=0;i<n;i++)
{
for(j=n-1;j>=0;j--)
{
if(i+j==n-1)
sum2 += p[i][j];
}
}
cout << "∏±∂‘Ω«œfl‘™Àÿ÷Æ∫Õ£∫" << sum2 << endl;
delete p;
return0;
}