Jeremy Gonzales
Jeremy Gonzales

Reputation: 11

array of 0's coming out as -85993460 instead of 0's

I have this code to print out the vector (1 2 3) from vector.txt file but whenever I run the code to turn the vector into a matrix the 0's that fill up the matrix comes out as -85993460. Please help

#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();
}

Upvotes: 0

Views: 159

Answers (3)

rcgldr
rcgldr

Reputation: 28828

I'm guessing you have a typo, it's more likely that your uninitialized values are showing up as -858993460, which is hex 0xCCCCCCCC, a common value used to fill uninitialized memory when compiling in debug mode.

Upvotes: 0

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48447

Use ={} to 0-initialize your matrix:

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

Upvotes: 2

Alex Kleiman
Alex Kleiman

Reputation: 709

You are not initializing the matrix. The for loop for(b=0; b<3;b++); does nothing, and the line below it only gets executed once. Remove the semi-colon at the end of this for loop if you want to execute the next line, or anything at all, in this loop.

Because the matrix is not initialized in the loop, you are reading garbage values. -85993460 is just whatever was on the stack when the matrix was created.

Upvotes: 1

Related Questions