找出数组中最后的最大的一个数的位置。
找出匹配的字符串的位置
找出小于n的似有素数
自己先在机子上编下,一会给结果
#include <iostream.h>
void main()
{
int a[5]={2,4,5,78,9};
int temp=a[0]; //用来暂时存储最大的,先赋值成第一个
for(int i=1;i<5;i++)
{
if(temp>a[i])
temp;
else
temp=a[i];
} //与其它的相比较(第一个外),大的话就让贤,否则保留,保留那
//可以再改下?
cout<<temp<<endl;
}第一个程序,还有不足处,想用动态数组,输入数据,然后求最大,还有就是感觉程序整体架构不行,for语句中的if else中不行。
#include <iostream.h>
void main()
{
int n;
cout<<"n is ";
cin>>n; //要定义的数组的大小,注:cin中不能加>>endl;
int *a=new int[n]; //动态分配数组时,注意等式左侧数组定义要用指针型
cout<<"please enter the array"<<endl; //
for(int j=0;j<n;j++)
{
cin>>a[j];
} //由输入的数据来组成数组(可以有空格)
int temp=a[0];
for(int i=1;i<n;i++)
{
if(temp>a[i])
temp;
else
temp=a[i];
} //比较大小,和上一个程序一样
cout<<temp<<endl;
delete [] a;
}这个加入了动态分配,想用动态数组着,但是钱能的书中没有,所以就用了动态分配。可以自己输入数组中元素个数n,然后动态输入数组元素
两个程序相比较,只是前面的部分相变化,后面的只是将第一个程序移植了过来。
不足之处,就是没有写注释,这个要记得写。好像写程序注释也是必须的呢!
才发现这个程序错误,要返回的是最大的数组元素下标,还要再改
#include <iostream.h>
void main()
{
int n;
cout<<"n is ";
cin>>n; //数组中元素个数
int *a=new int[4];
cout<<"please enter the array"<<endl;
for(int j=0;j<n;j++)
{
cin>>a[j];
} //输入数组中元素
int temp=a[0];
int k=0;
for(int i=1;i<n;i++)
{
if(temp>a[i])
temp;//如果大的话,不变
else
{
temp=a[i];
k=i;//如果小的话,变化,并且保存k
}
}
cout<<temp<<endl;
cout<<k<<endl;//输出k
delete [] a;//清除a
}问题已经解决了,输出的是下标,不过还有一问题,在运行的时候老出现一个提示框,有如下说明:
Debug error!
program:D\.........|p1.exe DAMAGE: after Normal block (#48) at 0x00441860
(Press Retry to debug the application)
为什么?(n太大时会有这个问题)
评论