Thread: c/c++ help needed

  1. #1
    Registered User
    Join Date
    Mar 2011

    c/c++ help needed

    This code is supposed to read the first four lines of a .dat file that looks like this"

    -5.0 1.0 -4.0 -0.5
    3.0 1.5 -3.0 2.1
    -3.1 2.7 2.5 -4.2
    1.6 0.3 2.4 -2.9
    1.0 1.2 -1.0 -0.5

    after compiling my code its reads out the 2nd-5th line and says segmentation failed core dumped, what should i do ?? please help!!!

    this is just a function out of my code:
    void problem(void)
    FILE *Inf2;
    int i, j, rows=4, cols=4, dim, linecount=0;
    char line2[100];
    double sum, **a, *b; /*for normalizing*/
    if(cols > rows) dim = cols;
    Inf2 = fopen("matrix.dat", "r");
    while(fgets(line2,100,Inf2) !=NULL)
    //if(linecount <=4)
    //double **a;
    a=(double**)calloc(dim, sizeof(double *));
    for(i=1; i<rows; i++)
    { a[i]=(double *)calloc(dim, sizeof(double));}
    for(i=0;i<rows; i++)
    for(j=0;j<cols; j++)
    fscanf(Inf2, "%lf", a[i]+j);
    for(i=0;i<=rows; i++) /* it was i <rows originally*/
    for(j=0;j<cols; j++)
    printf("%7.2f \n",a[i][j]);
    if((j+1)%10 == 0) printf("\n");
    //printf("a[%d][]=%d", i, *(a[i]+j));
    }while (linecount != 5);
    for(rows=0; rows<4; rows++) free(a[rows]);
    Last edited by Salem; 03-06-2011 at 04:29 AM. Reason: Added code tag

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I added code tags, but it didn't improve matters. Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Join Date
    Oct 2007
    It's either C or C++. You can't have both, so pick one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Releasing a program - what is needed? A few Q's
    By ulillillia in forum Tech Board
    Replies: 9
    Last Post: 04-28-2007, 12:18 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Who's telling the truth??? Career Advice Needed Badly
    By Ican'tCjax,fl in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 05:16 PM