0的数组出现为-85993460而不是0' s

时间:2014-08-29 14:58:08

标签: c++ windows

我有这段代码打印出来自(1 2 3)文件的向量vector.txt,但每当我运行代码将向量转换为matrix 0时将matrix填写为-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();
}

3 个答案:

答案 0 :(得分:2)

使用={}来初始化矩阵:

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

答案 1 :(得分:1)

您没有初始化矩阵。 for循环for(b=0; b<3;b++);什么都不做,下面的行只执行一次。如果要在此循环中执行下一行或任何内容,请删除此for循环末尾的分号。

因为矩阵未在循环中初始化,所以您正在读取垃圾值。 -85993460就是创建矩阵时堆栈上的任何内容。

答案 2 :(得分:0)

我猜你有一个拼写错误,你的未初始化值更有可能显示为-858993460,这是十六进制0xCCCCCCCC,这是在调试模式下编译时用于填充未初始化内存的常用值