Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-11-2009
Member
 
Join Date: Mar 2009
Posts: 175
Problem reading C++ matrix

I am having problem reading my matrix.

Abstract:
three functions:

readingmatr => reads the matrix from a text file of the form and returns the size of 3

1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

viewmatr => displays the matrix (which does not work)

and the main

I am having only part of the code that does not work. My compiler gives be the largest source of error while computing. Here's the code:

Code:
int readingmatr (char * num, double ** matr)
{
    double val, * cour1, * cour2;
    int i, size, j,cpt;
    FILE * fic;
    fic = fopen(num,"r" );
    if ( fic != NULL)
    {
        fscanf(fic, "%d",&size);
        matr = malloc (sizeof(double *) * size);
        for (cpt = 0; cpt < size;cpt++)
        {
            matr[cpt] = calloc ( size, sizeof(double));
            for (i = 0; i < size;i++)
            {
                fscanf(fic,"%lf",&matr[cpt][i]);
                printf("%f   ", matr[cpt][i]);
            }
            printf("\n" );
        }
    }
    else
    {
        printf("Error opening" );
    }
    return size;
}
void viewmatr (int size, double * matr)
{
    int i, j;
    for (i=0; i < size; i++)
    {
        for (j=0 ; j < size; j++)
        {
            printf("%10.4f\n", *(matr + i * size + j));
        }
        printf("\n\n" );
    }
}
int main(int argc, char * argv[])
{
    /*char * numfic1 = argv[1], * numfic2 = argv[2], * numfic3 = argv[3], * numfic4 = argv[4];*/
    char * numfic1 = "vector1.txt", * numfic2="vector2.txt", * numfic3= "matr.txt";
    double * tab1, * tab2, ** matr;
    int size, psc;
    /* **************************************************************** */
    //                             PART 2                            // 
    /* **************************************************************** */
    printf("\n PART 2 \n" );
    size = readingmatr(numfic3, matr);
    printf("\nthe size of the matrix is: %d\n", size);
    viewmatr(size, *matr);
    return 0;
}
Reply With Quote
  #2  
Old 14-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Problem reading C++ matrix

Your matrix is not allocated contiguously, your display does not work.

* (matr + size * i + j) is false;

You must write it as matr[i][j].

The signature of the function must be void viewmatr (int size, double ** matr) and you have to write viewmatr (size, matr);
Reply With Quote
  #3  
Old 14-11-2009
Member
 
Join Date: Mar 2009
Posts: 175
Re: Problem reading C++ matrix

Thank you for your help. So I corrected as advised but it does not always display the matrix:

(bug with the line: printf ( "% 10.4f \ n", matr [i] [j]); initially it was working)
even if I put in the hand (eg printf ( "% f", matr [0] [0]); that do not work)

I inserted the libraries: "stdio" and "stdlib";
I have no compilation error;
I use codeblocks;

at the bug, I get the message vista classic "tp3.exe stopped working ..."

corrected code with bookcase

Code:
#include <stdio.h>
#include <stdlib.h>
//void view(double * tab); 
void viewmatr(int size, double ** matr);
int readingmatr(char * num, double ** matr);
//void readingvect(double ** tab, char * num); 
//double prod_scal( double * vect1, double * vect2); 
int readingmatr(char * num, double ** matr)
{
    double val, * cour1, * cour2;
    int i, size, j,cpt;
    FILE * fic;
    fic = fopen(num,"r" );
    if (fic!= NULL)
    {
        fscanf(fic, "%d",&size);
        matr = malloc (sizeof(double *) * size);
        for (cpt = 0; cpt < size;cpt++)
        {
            matr[cpt] = calloc ( size, sizeof(double));
            for (i = 0; i < size;i++)
            {
                fscanf(fic,"%lf", &matr[cpt][i]);
                printf("%f   ", matr[cpt][i]);
            }
            printf("\n" );
        }
    }
    else
    {
        printf("Error opening" );
    }
    return size;
}
void viewmatr(int size, double ** matr)
{
    int i, j;
    for (i=0; i < size; i++)
    {
        for (j=0 ; j < size; j++)
        {
            printf("%10.4f\n", matr[i][j]);
        }
        printf("\n\n" );
    }
}
int main(int argc, char * argv[])
{
    /*char * numfic1 = argv[1], * numfic2 = argv[2], * numfic3 = argv[3], * numfic4 = argv[4];*/
    char * numfic1 = "vector1.txt", * numfic2="vector2.txt", * numfic3= "matr.txt";
    double * tab1, * tab2, ** matr;
    int size, psc;
    /* **************************************************************** */
    //                             PART 1                              // 
    /* **************************************************************** */
    /* 
    printf(" PART 1 \n" ); 
    readingvect(&tab1, numfic1); 
    readingvect(&tab2, numfic2); 
    view(tab1); 
    view(tab2); 
    psc = prod_scal(tab1,tab2); 
    */
    /* **************************************************************** */
    //                             PART 2                            // 
    /* **************************************************************** */
    printf("\n PART 2 \n" );
    size = readingmatr(numfic3, matr);
    printf("\nthe size of the matrix is: %d\n", size);
    viewmatr(size, matr);
    return 0;
}
Reply With Quote
  #4  
Old 14-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Problem reading C++ matrix

Yes, I have to test with me, he must pass the address of matr to int readingmatr (char * num, double ** matr) for the changes to take effect on matr in main.

The signing of readingmatr is int readingmatr (char * num, double *** matr)
Reply With Quote
  #5  
Old 14-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Problem reading C++ matrix

Its because the [] has precedence over *. So I would suggest you to correct it everywhere in your code and see if that works, for example:

Code:
(*adrmatr)[cpt] = calloc (size, sizeof ( double ));
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem reading C++ matrix"
Thread Thread Starter Forum Replies Last Post
New Matrix Movie (Matrix ReEntered) Vicious Off Topic Chat 8 08-02-2011 08:24 PM
Any fastest way to compute 3x3 Matrix inverse and Matrix multiplication? Logan.B Software Development 4 25-09-2010 05:11 PM
Matrix RAID problem - offline member disks Jaida Hardware Peripherals 6 22-08-2010 04:17 AM
Problem with Intel Matrix Storage HiSpeed Hardware Peripherals 3 19-10-2009 11:52 AM
Problem recovery matrix in C Rilex Software Development 3 07-05-2009 11:53 PM


All times are GMT +5.5. The time now is 01:35 AM.