You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从文件读取200个随机数并分奇偶排序的代码报错求助

Fixing Your Odd/Even Separation & Memory Access Issues

Let's break down exactly what's causing your two problems and walk through fixing them:

1. Why You're Getting Duplicate Numbers

Your current code has a critical mistake in how it reads numbers into the arr array:

while(File >> x) {
    for (i = 0; i < 200; i++) {
        arr[i] = x;
    }
    // ... rest of processing
}

Every time you read a single number x from the file, you overwrite all 200 elements of arr with that same x. Then you process the entire array (200 copies of x) and add them to your even/odd lists. This is why you're seeing massive duplication—you're adding the same number 200 times per file read, and repeating this for every number in the file.

2. Why the "Write Access Violation" Happens

Since you're adding 200 elements to either even or odd every time you read a single number, your evncnt and oddcnt counters quickly exceed the size of the even and odd arrays (which are only sized for 200 elements). Writing past the end of an array is undefined behavior, which triggers the access violation error at odd[oddcnt++] = arr[i];.

Step-by-Step Fixes

Here's how to rewrite your code to fix both issues and meet your requirement of sorting the results:

a. Correctly Read 200 Numbers into the Array

Instead of overwriting the entire array each time, use a counter to fill arr with up to 200 unique numbers from the file:

int idx = 0;
while (File >> x && idx < 200) { // Stop after 200 numbers or end of file
    arr[idx++] = x;
}

b. Separate Odd/Even After Reading All Numbers

Move your parity-check loop outside the file-reading loop so you only process the filled array once, not once per number read.

c. Add Sorting for Ascending Order

Include the <algorithm> header to use the sort() function, which will sort your even and odd arrays before printing.

d. Clean Up Output Formatting

Add spaces between numbers so they're readable, and add basic error handling for missing files.

Fixed Full Code

#include <iostream>
#include <fstream>
#include <algorithm> // Required for sort()
using namespace std;

class Random {
private:
    int arr[200], even[200], odd[200];
    int evncnt = 0, oddcnt = 0;
public:
    void readFile();
};

void Random::readFile() {
    fstream File("Random.txt");
    if (File.is_open()) {
        int x, idx = 0;
        // Read up to 200 numbers into arr
        while (File >> x && idx < 200) {
            arr[idx++] = x;
        }

        // Separate even and odd numbers
        for (int i = 0; i < idx; i++) { // Use idx to only process filled elements
            if (arr[i] % 2 == 0) {
                even[evncnt++] = arr[i];
            } else {
                odd[oddcnt++] = arr[i];
            }
        }

        // Sort both arrays in ascending order
        sort(even, even + evncnt);
        sort(odd, odd + oddcnt);

        // Print even numbers
        cout << "\nThe even numbers are: ";
        for (int i = 0; i < evncnt; i++) {
            cout << even[i] << " ";
        }

        // Print odd numbers
        cout << "\nThe odd numbers are: ";
        for (int i = 0; i < oddcnt; i++) {
            cout << odd[i] << " ";
        }

        File.close();
    } else {
        cout << "Error opening file!" << endl; // Handle missing file case
    }
}

int main() {
    Random file;
    file.readFile();
    system("pause");
    return 0;
}

Key Improvements Explained

  • We only read up to 200 numbers, filling arr sequentially instead of overwriting it.
  • Parity separation happens once, so evncnt and oddcnt never exceed 200 (safe for your array sizes).
  • Added sorting with sort() to meet the "从小到大" (ascending order) requirement.
  • Added basic error handling if the file can't be opened.
  • Cleaned up output to make numbers easier to read.

内容的提问来源于stack exchange,提问作者GianMarco Alegre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:42:46