首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

刚学习C++ 大家帮帮忙解决办法

2012-05-22 
刚学习C++ 大家帮帮忙C/C++ code#includeiostreamusing namespace stdint main(){int a[10] {5,22,78

刚学习C++ 大家帮帮忙

C/C++ code
#include<iostream>using namespace std;int main(){    int a[10] = {5,22,78,15,43,28,75,28,12,65};    int Num = 0;    for(Num = 0;Num <= 10;Num ++)    {        cout<<a[Num];    }    BubbliSort(T a[],int 10);    for(Num = 0;Num <= 10;Num ++)    {        cout<<a[Num];      }    return 0;}int swap(int ,int){    int temp;    temp = A[j];    A[j] = A[j+1];    A[j+1] = temp;}template<class T>void BubbliSort(T A[],int n){    int i,j;    int lastExchangeIndex;//最后一次交换时的下标值    i = n-1;//i为最后一个元素的下标    while(i>0)//继续处理直到不需要再交换    {        lastExchangeIndex = 0;//从lastExchangeIndex开始            for(j = 0;j < i;j + +)        {            if(A[j+1]<A[j])//交换相邻两个数据,并修改lastExchangeIndex            {                swap(A[j],A[j+1]);                lastExchangeIndex = j;            }        }        i = lastExchangeIndex;//i为最后一次发生交换的下标,继续对A[0]...A[i]进行排序    }}


[解决办法]
BubbliSort(T a[],int 10);

这行错误。声明变量和传入变量不同,应该是BubbliSort(a, 10);
[解决办法]
C/C++ code
#include<iostream>using namespace std;int swap(int ,int){    int temp;    temp = A[j];    A[j] = A[j+1];    A[j+1] = temp;}template<class T>void BubbliSort(T A[],int n){    int i,j;    int lastExchangeIndex;//最后一次交换时的下标值    i = n-1;//i为最后一个元素的下标    while(i>0)//继续处理直到不需要再交换    {        lastExchangeIndex = 0;//从lastExchangeIndex开始            for(j = 0;j < i;j + +)        {            if(A[j+1]<A[j])//交换相邻两个数据,并修改lastExchangeIndex            {                swap(A[j],A[j+1]);                lastExchangeIndex = j;            }        }        i = lastExchangeIndex;//i为最后一次发生交换的下标,继续对A[0]...A[i]进行排序    }}int main(){    int a[10] = {5,22,78,15,43,28,75,28,12,65};    int Num = 0;    for(Num = 0;Num <= 10;Num ++)    {        cout<<a[Num];    }    BubbliSort(a,10);    for(Num = 0;Num <= 10;Num ++)    {        cout<<a[Num];      }    return 0;}
[解决办法]
建议楼主先看书再写程序,循序渐进最好。。
共勉

热点排行