gpt4 book ai didi

c++ - 排序数组的问题

转载 作者:行者123 更新时间:2023-11-30 01:54:54 24 4
gpt4 key购买 nike

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>

using namespace std;
const int AS = 6;
void FillingRandomly(int [AS][AS]);
void printing(int[AS][AS]);
void forsorting(int[][AS], int);
int c;

int main()

{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));

FillingRandomly(Array);

cout << "The unsorted array is" << endl << endl;

printing(Array);

cout << "The sorted array is" << endl << endl;

forsorting(Array, funny);
printing(Array);

system("PAUSE");

return 0;

}

void FillingRandomly(int Array[AS][AS])
{
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
Array[i][j] = rand()%87 +12;
}
}

void printing(int Array[AS][AS])
{
int counter = 0;
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
{
cout << setw(5) << Array[i][j];
counter++;
if (counter%AS == 0)
cout << endl << endl;
}
}
}

void forsorting(int Array[AS][AS], int funny)
{

int w=0;
int dice = 0;
int Brray[AS*AS];
int super = 0;
int space=0;

//Transofrming Array[][] into Brray[]
for (int i = 0; i < AS; i++)
{
for (int k = 0; k < AS; k++)
{
Brray[space] = Array[k][i];
space++;
}
}


//Bubble sorting in Brray[]

for (int passer = 0; passer < AS-1; passer++)
{
for (int timpa = 0; timpa < AS-1; timpa++)
{
if (Brray[timpa]>Brray[timpa + 1])
{
super = Brray[timpa];
Brray[timpa] = Brray[timpa + 1];
Brray[timpa + 1] = super;
}
}
}

//Transforming Brray[] into sorted Array[][]

for (int j=0;j<AS;j++)
for (int i=0;i<AS;i++)
{
Brray[w]=Array[i][j];
}

w++;

}

好的,这是我的代码。我需要做的只是排序部分,我写了冒泡排序技术,我仔细检查了我的类(class),这是相同的逻辑。所以我想知道的是,为什么我的数组在屏幕上打印出来时没有排序。

谢谢你的帮助

最佳答案

这个

Brray[w]=Array[i][j];

是错误的方法。

你永远不会真正写入数组。

关于c++ - 排序数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com