Make a matrix from a textfile.. Cant figure how!

This is a discussion on Make a matrix from a textfile.. Cant figure how! within the C Programming forums, part of the General Programming Boards category; Greetings. I am pretty new to C-programming since I just started 2 weeks ago. I though think its very fun ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    29

    Make a matrix from a textfile.. Cant figure how!

    Greetings.

    I am pretty new to C-programming since I just started 2 weeks ago. I though think its very fun and like to be better at it.

    I found something hard to prgram, since I have no idea how it should be written.

    Ill try to explain what I did untill now and what I cant get to work.

    I have a textfile temperatur.txt with 31 temperatures looking like this:

    1 2 5 3 -2 0 2 1 0 4 3 2 0 1...

    Another textfile with downpour.txt

    -2 2 3 4 1 2 0 0 0 3 5 2 1 0 0 2...

    Both files with 31 numbers in.

    I loaded both files in with FILE*prt and prt..... scanf.... etc. This is working.

    I need to find where there aint no downpour, and find the dates and temperatur for this.
    So im setting downpour09[i] == 0, then I know the is is the dates and temptabel09[i] is the temperatur those days.

    this is the program:

    Code:
        for (i = 0; i < 31; i++)
            if(downpour09[i] == 0) //find where there aint no downpour.
            {
                printf("Date[%d]Temperatur[%.f ]\n",i,temptabel09[i]); 
            }
    The output is:

    Date[2] Temperatur[-2]
    Date[1] Temperatur[-3]
    Date[-2] Temperatur[-4]
    Date[4] Temperatur[-1]
    Date[1] Temperatur[0]

    But I would like the output to be like a Matrix.

    Date 2 temperatur -2
    Date 1 temperatur -3
    Date -2 temperatur -4
    Date 4 temperatur -1
    Date 1 temperatur -0

    you see my point?

    I tried with a simple matrix:

    Code:
    void main()
    {
    int matrix[3][2];
    int i,j;
       for (i = 0 ; i < 3; i++)
       {
          for(j = 0;j < 2;j++)
         {
          matrix[i][j]=tal;
          tal++;
          printf("%d",tal);
          }
       }
    }
    the output is: 123456

    But I want the output to be
    1 2
    3 4
    5 6

    How?

    I hope I explained it right and its not to long.

    Cheers.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The output you get is related to how you use printf in this case. You need an extra space in there, then add a new line printf in the outer loop.

    Code:
    for(i = 0; i < 3; i++) 
    {
    
         for(j = 0; j < 2; j++) 
         {
              var++;
              printf("%d ", var);
         }
         printf("\n");
    }
    Last edited by Subsonics; 09-17-2011 at 08:17 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovelace View Post
    Code:
        for (i = 0; i < 31; i++)
            if(downpour09[i] == 0) //find where there aint no downpour.
            {
                printf("Date[%d]Temperatur[%.f ]\n",i,temptabel09[i]); 
            }
    I hope I explained it right and its not to long.

    Cheers.

    So... take the square brackets out and add a newline at the end of the formatting string in your printf() statement.

    Look up printf() in your C Library Documentation, it will give you all the details on how printf() works.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Additionally, void main is wrong. Read How to define main-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    29
    Quote Originally Posted by Subsonics View Post
    The output you get is related to how you use printf in this case. You need an extra space in there, then add a new line printf in the outer loop.

    Code:
    for(i = 0; i < 3; i++) 
    {
    
         for(j = 0; j < 2; j++) 
         {
              var++;
              printf("%d ", var);
         }
         printf("\n");
    }
    Cheers for figuring that out for me Ofcourse I needed a new line outside the loop! That helped alot!

    Quote Originally Posted by CommonTater View Post
    So... take the square brackets out and add a newline at the end of the formatting string in your printf() statement.

    Look up printf() in your C Library Documentation, it will give you all the details on how printf() works.
    And after I saw the first answer I realised this was exactly the problem.

    Ofcourse I needed a newline after the loop!

    Quote Originally Posted by AndrewHunter View Post
    Additionally, void main is wrong. Read How to define main-FAQ
    Just noticed it with void main(), realised it should be int main() instead.

    Cheers for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cin to textfile
    By Charlin in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2008, 12:52 PM
  2. Delete string from textfile
    By beon in forum C Programming
    Replies: 1
    Last Post: 11-15-2006, 05:10 AM
  3. textfile questions
    By ReLiEnThAwK in forum C++ Programming
    Replies: 11
    Last Post: 08-07-2005, 06:48 PM
  4. textfile 2
    By davidnj732 in forum C++ Programming
    Replies: 13
    Last Post: 03-03-2003, 04:49 AM
  5. Textfile Pointers
    By Bazza in forum C Programming
    Replies: 16
    Last Post: 07-11-2002, 04:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21