Thread: looked at tutorial and still confused

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    looked at tutorial and still confused

    i have file pointer and i think an array, i just dnt know what number to use in the brackets.
    the i have to filepoint this file to my program.
    inputf.dat which is the following matrix or set of numbers

    1.0 1.0 -4.0 -0.5
    -2.0 1.5 1.0 2.1
    -1.1 0.7 -2.5 4.2
    2.4 0.3 0.4 -1.9
    0.8 1.2 -0.9 -0.5

    input_1=fopen("inputf.dat", "r")
    if(input_1==NULL) exit(1);

    int m=0; j, i, n;
    char line[?]; <-this is my question. what number do i type here?

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    by the way if this is what im suppose to do.
    By using a pointer to pointer **A and the function calloc()
    allocate the memory for the 4x4 matrix A[][].
    By using a pointer *x and the function malloc()
    allocate the memory for the 4-dimensional vector x[].
    Read the components of A and x from the given input file inputf.dat
    (which is in the public directory; you need to copy it into your directory).
    The last line of the input file contains the components of x. The rows of
    matrix A are the first four lines of the input file. Print the components
    of A[][] and x[]

    all i need to know is what number belongs in the bracket

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The first thing you need to do is read the data from the file.

    What is the purpose of:
    Code:
    char line[?];
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    the purpose is to input the data into my program right?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Well, what do you suppose the maximum length of a line will be from your file? That array has to be large enough to hold that length.

    And if you're asking these questions, this assignment probably exceeds your capabilities.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rafacnsoccer View Post
    the purpose is to input the data into my program right?
    That does not happen automatically. Using C++, you could do that with an ifstream and getline().

    getline - C++ Reference

    Or you could use fopen and fgets from <cstdio>

    fgets - C++ Reference

    which would use a C string such as char line[]. Then you have to extract the data from the line into the matrix.

    First, I would just write a short routine to open the file and print it all out line by line, then close it. When that works smoothly, you can decide how to extract the data.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    well this is the other part i didnt add hopefully this helps

    char line[?];
    double **A, *x;
    A=(double **)calloc(4,sizeof(double *));
    for(i=0;i<4;i++)
    A[i]=(double *)calloc(4 ,sizeof(double));
    x=(double *)malloc(4*sizeof(double));

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, so probably 64 bytes is enough for the line, so the next bit starts:

    Code:
    while (fgets(line,64,input_1)) cout << line;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    okay that helps thanks, i finished the rest but the thing is now, i do not know how to write the program for the transpose A_s. which the formula used ismatrix A_s=(A+A_t)/2

    the normal matrix i obtained with this
    insert
    Code:
     char line[64];
     double **A, *x;
     A=(double **)calloc(4,sizeof(double *));
     for(i=0;i<4;i++)
     A[i]=(double *)calloc(4 ,sizeof(double));
     x=(double *)malloc(4*sizeof(double));
     
     while(fgets(line,64,input_1,) != NULL
     {
      if(k==4) sscanf(line, "%lf %lf %lf %lf", &x[0],&x[1],&x[2],&x[3]);
      else
       {
       sscanf(line, "%lf %lf %lf %lf", &A[m][0],&A[m][1],&A[m][2],&A[m][3]);
       }
      m++;
     
     }
     
    printf("Matrix A is:");
    printf("\n\n");
    for(q=0;q<4;q++)
    printf("%6.3f ",A[0][q]);
    printf("\n");
    
    for(j=0;j<4;j++)
    printf("%6.3f ",A[1][q]);
    printf("\n");
    
    for(j=0;j<4;j++)
    printf("%6.3f ",A[2][q]);
    printf("\n");
    
    for(j=0;j<4;j++)
    printf("%6.3f ",A[3][q]);
    printf("\n\n");
    
    printf("Vector x is:");
    printf("\n\n");
    printf(" x[] =  ");
    for(j=0;j<4;j++)
    printf("%6.3f  ",x[q]);
    printf("\n\n");
    and then its

    smatrix(A,x,n);
    here is where i dnt know how to make it transpose
    am i just suppose to switch A[#][q] to [q]A[#]

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's make one thing clear: what are you using? C or C++? We can't have both language solutions floating around here...
    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.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    the class is called mae9 c/c++

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, what language are you using? What do you INTEND to use?
    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.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    we are using c++

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rafacnsoccer View Post
    the class is called mae9 c/c++
    Barney S. doesn't like the term "c/c++" If you are supposed to be using C++, you have mostly been using vanilla C so far. If that matters you will want to use an fstream instead of cstdio to read the file.

    Anyway, I'm not sure what you want to do now, you want to create a second matrix by applying some formula to the first one?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then I suggest you start writing C++ code or converting to C. This is all C.
    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

Tags for this Thread