如何用VB给二维数组排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用VB给二维数组排序相关的知识,希望对你有一定的参考价值。
我的一个作业题:5名同学4科成绩,各科最高成绩,各科不及格成绩,各科平均成绩,每个同学总分降序排列。下面是我的编程,就只有总分降序排列没有实现了,现在恳求各位大侠帮帮忙指导一下哈~~~~~
Private Sub Form_Click()
Dim a%(5, 5), b%(4, 5), xm$(5), i%, j%, t%
Dim max%, sum!(6), averange!(4)
Print "原始数据为:"
For i = 1 To 5
xm(i) = InputBox("输入学生姓名")
Print xm(i);
For j = 1 To 4
a(i, j) = InputBox("输入成绩")
Print a(i, j);
Next j
Print
Next i
For i = 1 To 5
For j = 1 To 4
If a(i, j) < 60 Then Print "不及格成绩="; a(i, j); ; i
Next j
Next i
For i = 1 To 4
averange(i) = 0
For j = 1 To 5
b(i, j) = a(j, i)
averange(i) = averange(i) + b(i, j)
Next j
averange(i) = averange(i) / 5
Print "各科平均成绩averange (" & i & ")"; averange(i)
Next i
For j = 1 To 4
max = a(1, j)
For i = 2 To 5
If a(i, j) > max Then max = a(i, j)
Next
Print "第" & j; "列最大值=" & max
Next
For i = 1 To 5
For j = 1 To 5
sum(i) = sum(i) + a(i, j)
Next j
If sum(i) < max Then
sum(i) = sum(i): sum(i) = max: max = sum(i)
End If
Print sum(i)
Next
End Sub
希望能在我的程序基础上改一改,就改动最后那一部分就行谢谢 可以多加分~~~
Option Explicit
Dim startTime As Double
Dim endTime As Double
Dim timeToSort As Double
Dim timeTaken As String
Sub Command1_Click()
Dim lMyArray(0 To 2000) As Long
Dim vTemp1 As Variant
Dim vTemp2 As Variant
Dim vTemp3 As Variant
Dim iLoop As Integer
Randomize
For iLoop = LBound(lMyArray) To UBound(lMyArray)
lMyArray(iLoop) = Int(Rnd * 100) + 1
Next iLoop
vTemp1 = lMyArray
vTemp2 = lMyArray
vTemp3 = lMyArray
Screen.MousePointer = vbHourglass
Call BubbleSortNumbers(vTemp1)
Call SelectionSortNumbers(vTemp2)
Call ShellSortNumbers(vTemp3)
Screen.MousePointer = vbDefault
MsgBox timeTaken
End Sub
Sub BubbleSortNumbers(iArray As Variant)
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
startTime = Time()
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
endTime = Time()
timeToSort = endTime - startTime
timeTaken = "Bubble Sort: " & timeToSort
End Sub
Sub SelectionSortNumbers(vArray As Variant)
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lMin As Long
Dim lTemp As Long
startTime = Time()
For lLoop1 = LBound(vArray) To UBound(vArray) - 1
lMin = lLoop1
For lLoop2 = lLoop1 + 1 To UBound(vArray)
If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
Next lLoop2
lTemp = vArray(lMin)
vArray(lMin) = vArray(lLoop1)
vArray(lLoop1) = lTemp
Next lLoop1
endTime = Time()
timeToSort = endTime - startTime
timeTaken = timeTaken & "; Selection Sort: " & timeToSort
End Sub
Sub ShellSortNumbers(vArray As Variant)
Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long
startTime = Time()
lHValue = LBound(vArray)
Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > UBound(vArray)
Do
lHValue = lHValue / 3
For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
lTemp = vArray(lLoop1)
lHold = lLoop1
Do While vArray(lHold - lHValue) > lTemp
vArray(lHold) = vArray(lHold - lHValue)
lHold = lHold - lHValue
If lHold < lHValue Then Exit Do
Loop
vArray(lHold) = lTemp
Next lLoop1
Loop Until lHValue = LBound(vArray)
endTime = Time()
timeToSort = endTime - startTime
timeTaken = timeTaken & "; Shell Sort: " & timeToSort
End Sub
参考资料:http://www.promiseforever.com/blog/show-445-1.html
在C语言中如何用把二维数组中的一行贼给一维数组?
参考技术A 按列遍历,并输出到一维数组即可.void covert(int a[M][N], b[M*N]) int i,j,n; for(i = n = 0; i < N; i++) for(j = 0; j < M; j ++) b[n++] = a[j][i]; 参考技术B int i,j,a[3][4],b[12];
int n=0;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
b[n++]=a[i][j];
或者用这个,memcpy(b , a[0],16); 后面的16表示4个整形,16个字节,但是我怕你搞错,还是走循环,嵌套赋值吧 参考技术C 一个一个复制
for ( int i = 0; i < 3; ++i )
b[i] = a[0][i];
//数组不支持直接拷贝。 不能整体赋值,这点不像结构体。
以上是关于如何用VB给二维数组排序的主要内容,如果未能解决你的问题,请参考以下文章