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

C语言二维数组赋值异常及参数频率表实现技术求助

Troubleshooting Your C Array Issue & Completing the Frequency Table

Let’s break down the problems in your code and fix them step by step, then finish implementing the frequency table you’re working on.

1. Critical Array Declaration Error

Your original array line double newTable[argc*1][0]; is invalid in standard C:

  • Arrays can’t have a dimension of 0 (unless using a flexible array member in a struct, which this isn’t). This causes undefined behavior—which explains why you’re seeing all values overwritten to the last one (memory corruption from writing to an incorrectly sized array).

To fix this, we need an array where each row stores a unique value and its frequency. Update the declaration to:

double newTable[argc][2]; // Each row holds [unique_value, frequency]

Since we have argc-1 input numbers, argc rows give us enough space for all possible unique values.

2. Complete Frequency Table Implementation

Here’s the full corrected code with the frequency logic added, plus explanations for key parts:

#include <stdio.h>
#include <stdlib.h>
#include <math.h> // For safe double comparison

#define EPSILON 1e-9 // Small threshold to handle floating-point precision

int main(int argc, char **argv) {
    // Handle case where no numbers are provided
    if (argc < 2) {
        printf("Usage: %s <number1> <number2> ... <numberN>\n", argv[0]);
        return 1;
    }

    double newTable[argc][2]; // [value, frequency]
    int newTableCounter = 0;
    int counter = 1; // Start at 1 (argv[0] is the program name)
    double min = 999999999.9;
    double max = -9999999999999999.9;
    double temp = 0.0;
    int isListed = 0;

    /* Calculate Min/Max and build the frequency table */
    while (counter < argc) {
        temp = atof(argv[counter]);

        // Update min and max values
        if (temp > max) max = temp;
        if (temp < min) min = temp;

        // Check if the current value is already in the table
        isListed = 0;
        for (int i = 0; i < newTableCounter; i++) {
            // Compare doubles safely (avoids precision issues with ==)
            if (fabs(newTable[i][0] - temp) < EPSILON) {
                newTable[i][1]++; // Increment frequency
                isListed = 1;
                break;
            }
        }

        // If not found, add the value to the table
        if (!isListed) {
            newTable[newTableCounter][0] = temp;
            newTable[newTableCounter][1] = 1.0; // Initial frequency is 1
            newTableCounter++;
        }

        counter++;
    }

    /* Print final results */
    printf("Min value: %.2f\n", min);
    printf("Max value: %.2f\n", max);
    printf("\nFrequency Table:\n");
    printf("Value | Frequency\n");
    printf("-----------------\n");
    for (int i = 0; i < newTableCounter; i++) {
        printf("%.2f  | %.0f\n", newTable[i][0], newTable[i][1]);
    }

    return 0;
}

Key Fixes & Improvements

  • Safe Double Comparison: Instead of using == (which can fail due to floating-point precision), we use fabs(a - b) < EPSILON to check if two values are practically equal.
  • Input Validation: Added a check to handle cases where no numbers are passed as arguments.
  • Proper Frequency Tracking: For each input number, we check if it’s already in the table—if yes, increment its count; if no, add it as a new entry.
  • Valid Array Sizing: The array now has a valid second dimension (2) to store both the value and its frequency.

Why You Saw "All Values Set to Last One"

The invalid [0] dimension in your original array meant you were writing to memory outside the array’s bounds. This corrupted adjacent memory locations, leading to all entries appearing as the last value you wrote. Fixing the array size eliminates this undefined behavior.

内容的提问来源于stack exchange,提问作者Raine Nuutinen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:21:20