Thread: Writing C

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Windsor, ON
    Posts
    5

    Writing C

    Hi,

    I am new to C programming and MATLAB but I have a class and an assignment to write a C program for following:

    a. read a data file (filename.txt) which has 2 arrays A and B
    - A is 5x4 and B is 4x5 matrix
    b. calculate F=AxB and solution should be 5x5 matrix
    c. print F into a filename.txt

    This is what I have as shown by prof in a way...is this correct and how do I do b. and c.? Thnx

    Code:
    #include "stdio.h"
    main()
    
    FILE *fin;
    fopen(fin)
    fscanf(fin, '%d %d', &row, &column);
    for (i=0; i<row; i++);
        {
            fscanf(fin, '%f %f %f %f', &a, %b, %c, %d);
        ArrayA[i][0]=a;
        ArrayB[i][1]=b;
        ArrayC[i][2]=c;
        ArrayD[i][3]=d;
        }
    for (i=0; i<column; i++);
        {
            fscanf(fin, '%f %f %f %f %f', &a, &b, &c, &d, &e);
        ArrayA[i][0]=a;
        ArrayB[i][1]=b;
        ArrayC[i][2]=c;
        ArrayD[i][3]=d;
        ArrayE[i][4]=e;
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does it work? No. Then it's not the right way to do it.

    1. main returns an int, and so you need a return 0; at the bottom of your main function.

    2. main, if it is to take no argumetns, should be main( void ).

    3. Functions needs a pair of braces to enclose their contents, you don't have any.

    4. You don't assign the return value of fopen so you can't do anything with the file in the event it actually opens.

    5. You haven't actually declared any arrays. Even if you did, you'd be declaring way too many.

    6. You didn't declare i either. (or a, b, c, ...)

    7. You didn't even try to compile this, because if you had, your compiler would be having a fit.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Windsor, ON
    Posts
    5
    Quote Originally Posted by quzah View Post
    Does it work? No. Then it's not the right way to do it.

    1. main returns an int, and so you need a return 0; at the bottom of your main function.

    2. main, if it is to take no argumetns, should be main( void ).

    3. Functions needs a pair of braces to enclose their contents, you don't have any.

    4. You don't assign the return value of fopen so you can't do anything with the file in the event it actually opens.

    5. You haven't actually declared any arrays. Even if you did, you'd be declaring way too many.

    6. You didn't declare i either. (or a, b, c, ...)

    7. You didn't even try to compile this, because if you had, your compiler would be having a fit.


    Quzah.
    Thank you for reply.
    I know it doesn't work but I it is all confusing to me as I never did this, total newbie. I'm searching how to read a file which has matrix in it and calculating it into another file. Everybody has some different ways of writing these codes; makes no sense to me.

    This guy that teaches us doesn't really give us much besides already done examples and nothing on how to begin to write a program.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Windsor, ON
    Posts
    5
    I have written this now. Any inputs on how to continue on with the problem? Thanks

    Code:
    #include "mex.h"
    #include "stdio.h"
    
    //void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    main(void)
    
    {
        File *fin;
        int i, j;
        float A[5],B[4];
        float a,b;
        
        fin = fopen("et7450hw1inA.txt","r");
        
        fscanf(fin, "%d %d", &row, &column);
        for(i=0; i<row; i++)
        {
            fscanf(fin, "%f %f %f %f", &a, &b, &c, &d);
            ArrayA[i][0]=a;
            ArrayB[i][1]=b;
        }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should ever check the return-value from fscanf.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Zvezda View Post
    This guy that teaches us doesn't really give us much besides already done examples and nothing on how to begin to write a program.
    You're best choices...

    1) Get a different teacher.
    2) Get an actual textbook and study it.
    3) Do both.


    I'm beginning to think that a lot of the people out there teaching C have never actually programmed in C... they're following a curriculum and spouting crap from some ancient textbook... Not exactly the best start to a programming career... (Which is why I've always thought that programming should be learned by apprentiship...)

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Windsor, ON
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    You're best choices...

    1) Get a different teacher.
    2) Get an actual textbook and study it.
    3) Do both.


    I'm beginning to think that a lot of the people out there teaching C have never actually programmed in C... they're following a curriculum and spouting crap from some ancient textbook... Not exactly the best start to a programming career... (Which is why I've always thought that programming should be learned by apprentiship...)
    I think that too but we can't do much about it. We don't have a book for the class; just what he gives us. Anyhow, I don't have time to learn this thing from scratch. I did read some stuff of net but it's confusing to put everything together.
    I'm not in programming business but it's my masters class. I don't plan on working with this or anything; just need to pass and go on.

  8. #8
    Registered User ChipS's Avatar
    Join Date
    Oct 2011
    Posts
    16
    I agree with CommonTater, I've recently graduated and I have to say that my programming professors at university were just shy of horrible. My community college professors with actual work experience were much better. I'm in the process of reteaching myself C and C++ in order to fill the many gaps my formal education has left me with.

    I recommend that you definitely pick up one of the recommended texts as soon as you are able to, read it front to back and do as many problems as humanly possible.

    As far as some bugs you need to work out off the top of my head..
    1. Main has no return type/value or closing bracket
    2. Consider checking if file opens successfully
    3. You don't close files you have opened (may error check this too)
    4. Is arrayA / arrayB defined somewhere?.. I see a 2-d array when only a 1-d is defined (and different names).

    I'm sure there are more things to correct and all-in-all you just need to read the text just like I am currently. I'm reading through file I/O right this moment so my input won't be the most complete.
    Last edited by ChipS; 10-02-2011 at 04:27 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Zvezda View Post
    I think that too but we can't do much about it. We don't have a book for the class; just what he gives us. Anyhow, I don't have time to learn this thing from scratch. I did read some stuff of net but it's confusing to put everything together.
    I'm not in programming business but it's my masters class. I don't plan on working with this or anything; just need to pass and go on.
    How much is your future worth to you? Amazon.com: C Programming Language

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ChipS View Post
    I agree with CommonTater, I've recently graduated and I have to say that my programming professors at university were just shy of horrible. My community college professors with actual work experience were much better. I'm in the process of reteaching myself C and C++ in order to fill the many gaps my formal education has left me with.
    And even then you need to realize that texts such as K&R's C Programming Language do not cover the entirety of the language or it's capabilities. They are excellent introductions but there is at least equal value in compiler related documentation which is generally a full disclosure of what the compiler and it's libraries can do for you... Contary to common belief programmers don't memorize everything, or even try to, it's all about "looking stuff up" when you need it.

  11. #11
    Registered User ChipS's Avatar
    Join Date
    Oct 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    And even then you need to realize that texts such as K&R's C Programming Language do not cover the entirety of the language or it's capabilities. They are excellent introductions but there is at least equal value in compiler related documentation which is generally a full disclosure of what the compiler and it's libraries can do for you... Contary to common belief programmers don't memorize everything, or even try to, it's all about "looking stuff up" when you need it.
    Agreed. I forgot to mention that I also have a C reference book that I never plan on reading cover to cover and I'm sure I'll need to add to my reference collection throughout my career. I feel that introductory texts are extremely helpful though.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    Windsor, ON
    Posts
    5
    Thank you very much for info guys. I appreciate it.

    Anyhow, here is my latest code, which still needs a lot code added to but it could be a good start.
    Again, this code needs to run a program which does:
    a. read a data file (filename.txt) which has 2 arrays A and B
    - A is 5x4 and B is 4x5 matrix
    b. calculate F=AxB and solution should be 5x5 matrix
    c. print F into a filename.txt


    Code:
    #include "stdio.h"
    
    main(void)
    {
        FILE *Fin, *Fout;
        int row, column, i, j;
        float ArrayA[5][4];
        float ArrayB[4][5];
        
        Fin = fopen("et7450hw1inA.txt","r");
        fscanf(Fin, '%d %d', &row, &column);
        for (i=0; i<row; i++);
        {
            if ArrayA[1][2]==0
            {
                break;
            }
        }
        {   
    fscanf(fin, "%f %f %f %f", &a, &b, &c, &d);
    ArrayA[i][0]=a;
    ArrayB[i][1]=b;
    ArrayC[i][2]=c;
    ArrayD[i][3]=d;
        
        fclose(Fin);
        return 0;
        }
    }
    Question: At the end, I'm not sure if I have to use fclose or return?
    Also, when I try to run it, it says Undefined function or variable?? Why?

    Help much appreciated.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ChipS View Post
    Agreed. I forgot to mention that I also have a C reference book that I never plan on reading cover to cover and I'm sure I'll need to add to my reference collection throughout my career. I feel that introductory texts are extremely helpful though.
    Trust me... a deliberate study --cover to cover-- is an enormous benfit, even if you already think you're pretty good.
    You just never know what you might actually learn...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Zvezda View Post
    Code:
        float ArrayA[5][4];
        float ArrayB[4][5];
        
        Fin = fopen("et7450hw1inA.txt","r");
        fscanf(Fin, '%d %d', &row, &column);
        for (i=0; i<row; i++);
        {
            if ArrayA[1][2]==0
            {
                break;
            }
        }
        {   
    fscanf(fin, "%f %f %f %f", &a, &b, &c, &d);
    ArrayA[i][0]=a;
    ArrayB[i][1]=b;
    ArrayC[i][2]=c;
    ArrayD[i][3]=d;
    Question: At the end, I'm not sure if I have to use fclose or return?
    Also, when I try to run it, it says Undefined function or variable?? Why?

    Help much appreciated.
    Simple rules:

    Always close what you open.
    Always free what you allocate.
    Keep variable scope to a minimum.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Zvezda View Post
    Thank you very much for info guys. I appreciate it.

    Anyhow, here is my latest code
    Which still doesn't get close to addressing the requirement in the assignment. For example, you are calling fscanf() just once and then assigning the same values to the entire array...

    Also you are not checking *any* return values... you don't know if the file opened correctly, you don't know if fscanf() is decoding the file correctly.

    Question: At the end, I'm not sure if I have to use fclose or return?
    You open it... you close it.
    You allocate it ... you free it.
    You ate it... you poop it.

    It's not a difficult concept.

    Also, when I try to run it, it says Undefined function or variable?? Why?
    You don't have an arrayc and arrayd to assign anything to.




    And yes you have to do both close and return...
    Last edited by CommonTater; 10-02-2011 at 07:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  2. Writing a UDF in C++
    By kripatel in forum Linux Programming
    Replies: 0
    Last Post: 03-22-2008, 04:14 PM
  3. hex writing
    By gamer in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2005, 01:00 PM
  4. writing a TSR
    By moi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-06-2002, 11:00 AM
  5. Writing to a TXT
    By Akilla in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2002, 01:51 PM