我正在撰寫一個程式,它在堆上動態分配一個陣列,用 55-99 的亂數填充它并將它們列印成 10 行。我使用了 9 個以上的動態陣列來存盤這些行中的每一行。但是在運行程式時出現錯誤:正常塊后檢測到堆損壞。我很抱歉笨拙的代碼。
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
int numScores = 199;
int* randomTestScores = new int[numScores];
void fillArray() {
for (int i = 0; i < 199; i ) {
const int min_Value = 55;
const int max_Value = 99;
randomTestScores[i] = (rand() % (max_Value - min_Value 1)) min_Value;
}
}
void sortArray() { std::sort(randomTestScores, randomTestScores numScores); }
// Created better varibale names than a, b, c, etc.
int s5559 = 0, l5559 = 0;
int s6064 = 0, l6064 = 0;
int s6569 = 0, l6569 = 0;
int s7074 = 0, l7074 = 0;
int s7579 = 0, l7579 = 0;
int s8084 = 0, l8084 = 0;
int s8589 = 0, l8589 = 0;
int s9094 = 0, l9094 = 0;
int s9599 = 0, l9599 = 0;
int main() {
srand(time(0));
fillArray();
sortArray();
// Delete this later
for (int i = 0; i < numScores; i ) {
cout << randomTestScores[i] << endl;
}
//
for (int i = 0; i < numScores; i ) {
if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59) { s5559 ; }
if (randomTestScores[i] >= 60 && randomTestScores[i] <= 64) { s6064 ; }
if (randomTestScores[i] >= 65 && randomTestScores[i] <= 69) { s6569 ; }
if (randomTestScores[i] >= 70 && randomTestScores[i] <= 74) { s7074 ; }
if (randomTestScores[i] >= 75 && randomTestScores[i] <= 79) { s7579 ; }
if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84) { s8084 ; }
if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89) { s8589 ; }
if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94) { s9094 ; }
if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99) { s9599 ; }
}
int* testScores5559 = new int[s5559];
int* testScores6064 = new int[s6064];
int* testScores6569 = new int[s6569];
int* testScores7074 = new int[s7074];
int* testScores7579 = new int[s7579];
int* testScores8084 = new int[s8084];
int* testScores8589 = new int[s8589];
int* testScores9094 = new int[s9094];
int* testScores9599 = new int[s9599];
for (int i = 0; i < numScores; i ) {
if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59) {*(testScores5559 l5559) = randomTestScores[i]; l5559 ;}
if (randomTestScores[i] >= 60 && randomTestScores[i] <= 64) {*(testScores6064 l6064) = randomTestScores[i]; l6064 ;}
if (randomTestScores[i] >= 65 && randomTestScores[i] <= 69) {*(testScores6569 l6569) = randomTestScores[i]; l6569 ;}
if (randomTestScores[i] >= 70 && randomTestScores[i] <= 74) {*(testScores7074 l7074) = randomTestScores[i]; l7074 ;}
if (randomTestScores[i] >= 75 && randomTestScores[i] <= 79) {*(testScores7579 l7579) = randomTestScores[i]; l7579 ;}
if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84) {*(testScores7579 l8084) = randomTestScores[i]; l8084 ;}
if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89) {*(testScores7579 l8589) = randomTestScores[i]; l8589 ;}
if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94) {*(testScores7579 l9094) = randomTestScores[i]; l9094 ;}
if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99) {*(testScores7579 l9599) = randomTestScores[i]; l9599 ;}
}
delete []randomTestScores;
delete []testScores5559;
delete []testScores6064;
delete []testScores6569;
delete []testScores7074;
delete []testScores7579;
delete []testScores8084;
delete []testScores8589;
delete []testScores9094;
delete []testScores9599;
return 0;
/*
if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59) {
*(testScores5559 l5559) = randomTestScores[i];
cout << *(testScores5559 l5559) << endl;
l5559 ;
}
*/
}```
uj5u.com熱心網友回復:
你在這些陳述中有一個錯字
if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84) {*(testScores7579 l8084) = randomTestScores[i]; l8084 ;}
if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89) {*(testScores7579 l8589) = randomTestScores[i]; l8589 ;}
if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94) {*(testScores7579 l9094) = randomTestScores[i]; l9094 ;}
if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99) {*(testScores7579 l9599) = randomTestScores[i]; l9599 ;}
不同的索引使用相同的陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355587.html
