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

求助:生成随机二维方阵后无法正常打印输出的问题

Hey there! Let's walk through the issues in your code and fix them step by step so you can see your matrix output properly.

1. Critical: Uninitialized cols is breaking your matrix dimensions

Looking at this part of your code:

scanf("%d", &rows);
rows=cols;

Here, cols hasn’t been assigned any value yet—it holds random garbage data from memory. You’re overwriting the valid rows value you just read with this garbage, which means your 2D array A[rows][cols] ends up with invalid, unpredictable dimensions. This is almost certainly why you’re seeing no (or broken) output.

Fix: Assign cols = rows; instead, since you want a square matrix where rows equal columns:

scanf("%d", &rows);
cols = rows; // Match cols to the input row count

2. Random numbers will repeat every run (quick quality fix)

Right now, rand() will generate the exact same sequence of numbers every time you run the program. To fix this, seed the random number generator with the current time at the start of main():

#include <time.h> // Add this header at the top of your file

int main() {
    srand(time(NULL)); // Seed the random generator
    // Rest of your code...
}

3. Output is unreadable (numbers are squished together)

Your current printf dumps all numbers without spaces or newlines, so it looks like a jumbled mess instead of a matrix. Add spaces between values and a newline at the end of each row:

for (r=0; r<rows; r++){
    for (c=0; c<cols; c++){
        printf("%d ", A[r][c]); // Add space after each number
    }
    printf("\n"); // Newline to end the row
}

4. Optional: Validate user input (defensive programming)

It’s a good habit to make sure the user enters a valid positive integer. Add a check to catch bad input:

printf("Enter the dimension of your square matrix: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
    printf("Please enter a positive whole number!\n");
    return 1; // Exit with an error code if input is invalid
}
cols = rows;

Full Corrected Code

Here’s the complete fixed version with all these changes:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main () {
    int rows, cols, r, c;

    printf("Enter the dimension of your square matrix: ");
    // Validate input
    if (scanf("%d", &rows) != 1 || rows <= 0) {
        printf("Invalid input! Please enter a positive integer.\n");
        return 1;
    }
    cols = rows;

    int A[rows][cols];

    // Seed random number generator
    srand(time(NULL));

    // Fill matrix with random numbers 0-49 (use %51 for 0-50)
    for (r=0; r<rows; r++){
        for (c=0; c<cols; c++){
            A[r][c] = rand() % 50;
        }
    }

    // Print matrix in readable format
    printf("\nYour square matrix:\n");
    for (r=0; r<rows; r++){
        for (c=0; c<cols; c++){
            printf("%d ", A[r][c]);
        }
        printf("\n");
    }

    return 0;
}

Quick Note

  • rand() % 50 generates numbers from 0 to 49 (since modulo 50 gives remainders 0-49). If you want numbers from 0 to 50 inclusive, use rand() % 51 instead.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:49:35