C++排序程序中"Stray 1 in program"错误



这是我在C++年实现的计数排序代码:

#include<iostream>
#include<stdlib.h>
using namespace std;
void counting_sort(int [], int, int);
main()
{
    int n,k = 0, a[15];
    cout << "Enter the number of input: ";
    cin >> n;
    cout << "nEnter the elements to be sorted: n";
    for ( int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(a[i] > k)
        {
            k = a[i];
        }
    }
    counting_sort(a, k, n);
    system("pause");
    //getch();
}
void counting_sort(int a[], int k, int n)
{
    int i, j;
    int b[15], c[100];
    for(i = 0; i <= k; i++)
        c[i] = 0;
    for(j =1; j <= n; j++)
        c[a[j]] = c[a[j]] + 1;
    for(i = 1; i <= k; i++)
        c[i] = c[i] + c[i-1];
    for(j = n; j >= 1; j--)
    {
        b[c[a[j]]] = a[j];
        c[a[j]] = c[a[j]] - 1;
    }
    cout << "nThe Sorted array is: ";
    for(i = 1; i <= n; i++)
    cout << b[i] << " " ;
}

编译时出现错误,在第 3 行 Col 1 中指出"程序中杂散 \1"。我尝试了Dev-C++和Ideone。两者都显示相同的错误。我还尝试将代码复制到新文件,但徒劳无功。我该如何纠正它?

您的代码中有一个(隐藏的)无效字符(第 3 行),该字符与 http://ideone.com/ALbZbr 上的代码一起复制。

尝试编辑此代码。您将在第三行看到一个红点(无效字符)。

#include<iostream>
#include<stdlib.h>
using namespace std; . <--
void counting_sort(int [], int, int);
main()
{

删除此无效字符,您的代码最终将运行。

最新更新