在c ++中对数组进行排序时,第一个元素变为0

时间:2022-04-15 07:43:07

I was sorting my array in c++ and 0 is printed on first index and highest value is ignored. where is my fault? here is my code. is there any problem with my logic? I am new to c++ so sorry if i did something wrong.

我在c ++中对数组进行排序,在第一个索引上打印0,忽略最高值。我的错在哪里?这是我的代码。我的逻辑有什么问题吗?我是c ++的新手,很抱歉,如果我做错了什么。

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
    int *x, size, temp;
    cout << "Enter the size of array\n";
    cin >> size;
    x = new int[size];
    for (int i = 0; i < size; i++)
    {
        cin >> x[i];
    }
    cout << "\nData before sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (x[j] > x[j + 1])
            {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
    cout << "\nData after sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
}

2 个解决方案

#1


0  

the working code with changes is

带有变化的工作代码是

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
    int *x, size, temp;
    cout << "Enter the size of array\n";
    cin >> size;
    x = new int[size];
    for (int i = 0; i < size; i++)
    {
        cin >> x[i];
    }
    cout << "\nData before sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size-1; j++)
        {
            if (x[j] > x[j + 1])
            {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
    cout << "\nData after sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
    cin >> temp;
}

changed "size" to "size-1" in loop statement to avoid access to un-allocated array members

在循环语句中将“size”更改为“size-1”以避免访问未分配的数组成员

#2


0  

The for loop should be corrected so that j runs till size - 1 & i runs till size. Else you gobble up one number in an unnecessary comparison with non-existent numbers thus displaying zero or garbage number in some cases.

应该纠正for循环,使j运行直到大小 - 1和i运行直到大小。否则,你会在与不存在的数字进行不必要的比较时吞噬一个数字,从而在某些情况下显示零或垃圾数。

Following for loop replaced into your program fixes your issue.

以下for循环替换为您的程序修复您的问题。

for(int i=0;i<size;i++)
{
    for(int j=0;j<size-1;j++)
    {
        if(x[j]>x[j+1])
        {
            temp=x[j];
            x[j]=x[j+1];
            x[j+1]=temp;
        }
    }
}

#1


0  

the working code with changes is

带有变化的工作代码是

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
    int *x, size, temp;
    cout << "Enter the size of array\n";
    cin >> size;
    x = new int[size];
    for (int i = 0; i < size; i++)
    {
        cin >> x[i];
    }
    cout << "\nData before sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size-1; j++)
        {
            if (x[j] > x[j + 1])
            {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
    cout << "\nData after sorting: ";
    for (int j = 0; j < size; j++)
    {
        cout << x[j] << ' ';
    }
    cin >> temp;
}

changed "size" to "size-1" in loop statement to avoid access to un-allocated array members

在循环语句中将“size”更改为“size-1”以避免访问未分配的数组成员

#2


0  

The for loop should be corrected so that j runs till size - 1 & i runs till size. Else you gobble up one number in an unnecessary comparison with non-existent numbers thus displaying zero or garbage number in some cases.

应该纠正for循环,使j运行直到大小 - 1和i运行直到大小。否则,你会在与不存在的数字进行不必要的比较时吞噬一个数字,从而在某些情况下显示零或垃圾数。

Following for loop replaced into your program fixes your issue.

以下for循环替换为您的程序修复您的问题。

for(int i=0;i<size;i++)
{
    for(int j=0;j<size-1;j++)
    {
        if(x[j]>x[j+1])
        {
            temp=x[j];
            x[j]=x[j+1];
            x[j+1]=temp;
        }
    }
}