| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1145 人关注过本帖
标题:可以帮我看看我的程序吗?老是不对啊!
取消只看楼主 加入收藏
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
 问题点数:0 回复次数:3 
可以帮我看看我的程序吗?老是不对啊!

我编的一个程序,是“插入排序(Insertion Sort)”

错误如下:1。error C2065: 'a' : undeclared identifier

2。error C2109: subscript requires array or pointer type

程序如下:

#include"iostream" #include"stdlib.h" using namespace std; void InsertionSort(int list[],int n){ //list the elements to be put into order //n the number of elements in the list int i,new_elem,location; for(i=2;i<=n;i++){ new_elem=list[i]; location=i-1; while(location>=1&&list[location]>new_elem){ list[location+1]=list[location]; location=location-1; }//while list[location+1]=new_elem; }//for cout<<"The new list is:"; for(i=1;i<=n;i++){ cout<<a[i]; cout<<"\n"; }//for }//InsertionSort void main(){ int a[101],i; a[0]=0; for(i=1;i<101;i++){//使用随机函数产生100个数字,分别赋给a[i] a[i]=rand()%100; a[0]++; }//for cout<<"\nThe original elements are:"; for(i=1;i<101;i++){ cout<<a[i]; cout<<"\n"; }//for InsertionSort(a,a[0]); }//main

搜索更多相关主题的帖子: list int elements 
2004-11-10 22:58
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 
为什么老是编译不对呢?
2004-11-10 23:02
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

我的程序是我的专业课上的算法啊,自己想改写为可执行的代码啊。

玩具兵兄:

#include <iomanip> //使用setw(2)设置输出格式

是不是改变输出格式啊?

但是程序运行到最后还是有问题啊。会出现一个很大的数字啊

2004-11-12 20:40
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

对我的程序的一点修改啊,呵呵,谢谢大家这么关心啊.

#include <iostream>

#include <iomanip> //使用setw(2)设置输出格式

using namespace std;

void InsertionSort(int list[], int n)

{

//list the elements to be put into order

//n the number of elements in the list

int i, new_elem, location;

for(i = 2; i <= n; i++)

{

new_elem = list[i];

location = i-1;

while(location >= 1 && list[location] > new_elem)

{

list[location+1] = list[location];

location = location-1;

}//while

list[location+1] = new_elem;

}//for

cout << "The new list is:\n";

for(i = 1; i <= n; i++)

{

cout << setw(2) << list[i] << " "; //改成list

if(i % 10 == 0)

cout << "\n";//没想到,换行输出语句的编写这么简单^_^

}//for

}//InsertionSort

int main() //c++建议使用int作为返回类型

{

int a[101],i;

a[0] = 0;

for(i = 1; i < 101; i++)

{

a[i] = rand() % 100;//原来C++中可以不用include”stdlib.h”

a[0]++;

}//for

cout << "\nThe original elements are:\n";

for(i = 1; i < 101; i++)

{

cout << setw(2) << a[i] << " ";

if(i % 10 == 0)

cout << "\n";//^_^,好简单,自己怎么没想到呢?

}//for

cout << endl << endl;

InsertionSort(a, 101);//这里应该是100,只有100个元素被Sort

system("pause");//这句不要好像也可以的

return 0;

}//main

谢谢啊

2004-11-12 22:13
快速回复:可以帮我看看我的程序吗?老是不对啊!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031286 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved