c++ - 0's coming out as -85993460 instead of 0' 的数组

标签 c++ windows

我有这段代码可以从 vector.txt 文件中打印出 vector (1 2 3) 但是每当我运行代码将 vector 转换为 matrix 填充 matrix0 结果为 -85993460。请帮忙

#include<iostream>
#include<conio.h>
#include <stdio.h>

using namespace std;

void main()
{
    cout << "Loading vector.txt" << endl;
    FILE *file = fopen("vector.txt", "r");
    int vector[3];
    int b = 0;

    do{
        fscanf(file, "%i", &vector[b]);
        b++;
    }while(feof(file) == 0);


    //clear the screen.
    fclose(file);

    cout << "Loading matrix" << endl;

    int a[3][3],i,j;
    for(int i=0;i<3;i++)
    {
        for(b=0; b<3;b++);
        a[0][i] = vector[i];
    }

    cout << " Vector rotation" << endl;


//Display the original matrix
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
    {
        cout<<a[i][j]<<" ";
    }
        cout<<endl;
    }
        cout<<"Transpose of above matrix is: "<<endl;
//Display the transpose matrix
    for(j=0;j<3;j++)
    {
        for(i=0;i<3;i++)
        {
        cout<<a[i][j]<<" ";
        }
    cout<<endl;
    }
//get character
    getch();
}

最佳答案

使用 ={} 对您的矩阵进行 0 初始化:

int a[3][3] = {},i,j;

关于c++ - 0's coming out as -85993460 instead of 0' 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570923/

相关文章:

c++ - 如何在 Visual Studio 操作中输出新行?

c++ - 与 `std::chrono` 类型相比,为什么使用 `float` 进行时差测量会给出更多有效数字的 `double` 类型?

c++ - 在 Qt 应用程序中保护私钥

c++ - boost.filesystem create_directories 抛出 std::bad_alloc

不能在 Windows 中使用 tcgetpgrp()?

c++ - usleep vs std::this_thread::sleep_for,在 linux 串行端口上写入/读取时

c# - Windows Mobile 5 SDK 是否适用于 Windows 7.5 芒果操作系统手机

windows - 具有 NDIS 的用户模式驱动程序

c# - 根据格式信息对wave文件列表进行排序

c++ - 模板类的静态成员初始化