求牛人改下程序,C++的

来源:百度知道 编辑:UC知道 时间:2024/05/24 03:15:05
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int temp[100],a=0,i=0;
void main()
{ int temp[100],a=0,i=0;
for(i=0;i<100;i++)
temp[100]=rand()%100;

for(i=0;i<99;i++)
{
for(int j=0;j<99-i;j++)
{
if(temp[j]>temp[j+1])
{
a=temp[j];
temp[j]=temp[j+1];
temp[j+1]=a;
}
}
cout<<temp[i]<<" ";
}
temp[100]; int k,c;
{
do{
cout<<"please input a number:";
cin>>k;
if((k>=temp[1])&&(k<=temp[100]))
for(c=1;k!=temp[c];c++)
if(c>100)
{
cout<<"not found"<<endl;}
else
if(k=temp[c])
cout<<"found"<<k<<endl;
else
cout<<"not found"<<endl;
e

首先我看了一下你的程序.
错误比较多
首先运行你的程序,输出一大堆-88789678什么的东西
这个原因是因为你程序的第9排编写错误
temp[100]=rand()%100;
你应该是想用循环语句给temp数组里面的每个数赋值
那么就应该写
temp[i]=rand()%100;
之后再运行虽然有值了,
但还是正确不能排序
你的排序程序是对的
但你的输出错了
细心看一下
for(i=0;i<99;i++)
{
for(int j=0;j<99-i;j++)
{
if(temp[j]>temp[j+1])
{
a=temp[j];
temp[j]=temp[j+1];
temp[j+1]=a;
}
}
cout<<temp[i]<<" ";
}
你的cout是在循环的内部,也就是这个程序一边循环排序
一边输出,肯定不对
说通俗点,别人电脑排序还没排完,你就要求别人输出,这肯定会出错
所以我给你改成
for(i=0;i<99;i++)
{
for(int j=0;j<99-i;j++)
{
if(temp[j]>temp[j+1])
{
a=temp[j];
temp[j]=temp[j+1];
temp[j+1]=a;
}
}
}
for(i = 0;i<99;i++)
cout<<temp[i]<<endl;
在程序循环排序完毕后再输出结果
这样就正确赋值和排序了

但是根据你所说你还要做个寻找程序,
我试着输入了一个数字,果然你写的那个寻找程序
不能正确找到