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

我编的一个程序,是“插入排序(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
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 

说实话现在很少用这种东西了,懒得都用模板了,不过确实方便(话说回来做作业的除外)

都不晓得什么是插入排序了,晕啊


2004-11-11 00:08
玩具兵
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-8-17
收藏
得分:0 

哈哈,修改成功了, kappa314,你的这个程序编的挺棒,代码如下:

#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; 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); system("pause"); return 0; }//main

[此贴子已经被作者于2004-11-11 14:09:29编辑过]

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

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

玩具兵兄:

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

是不是改变输出格式啊?

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

2004-11-12 20:40
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

#include<iostream> #include<iomanip> 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--; } list[location+1] = new_elem; } cout<<"The new list is:\n";

for(i=1; i<n; i++) { cout<<setw(2)<<list[i]<<" "; if(i%10==0) cout<<endl; } }

void main() { int a[101]; a[0]=1;

for(int i=1; i<101; i++) { a[i]=rand()%100; a[0]++; } cout<<"\nThe original elements are:\n"; for(i=1; i<101; i++) { cout<<setw(2)<<a[i]<<" "; if(i%10==0) cout<<endl; } cout<<endl<<endl; InsertionSort(a,101); }

2004-11-12 21:26
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
你的代码注释太多,或者说,多余的注释太多,而变量名字,一般不是用类的话,不要写下划线,这个“_”,也最好不要写太长的名字,短一点,看懂就好,该写的地方你没有写注释,看傻我了,而有两行写的英文也太有深度,下次请写中文,我爱国啊!
2004-11-12 21:29
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
玩具兵
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-8-17
收藏
得分:0 

kappa314:

谢谢老兄的建议。哈哈,互相吹捧,共同进步。

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



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

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