Array Problems? (Im a beginner)

This is a discussion on Array Problems? (Im a beginner) within the C Programming forums, part of the General Programming Boards category; Code: array[x][y]; Ok Im able to read in a double array of numbers, and print them out just fine with ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Array Problems? (Im a beginner)

    Code:
     
    array[x][y];
    Ok Im able to read in a double array of numbers, and print them out just fine with this code:

    Code:
        for (i=0; i<x; i++){
            for(j=0; j<y; j++){
                    array[i][j] = array[i][j];
                    printf("%d\n", array[i][j]);
                }
    
            j=0;
        }
    But when I try to flip the array horizontally, it doesnt work.. this is the code i use:

    Code:
        for (i=0; i<x; i++){
            for(j=y-1; j>0; j--){
                    array[i][j] = array[i][j];
                    printf("%d\n", array[i][j]);
                }
    
            j=y-1;
        }

    what should i do to make it flip horizontally?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am adding some variables to your code and assuming some things about what you are wanting to do.

    Code:
        for (i=0; i<x; i++){
            for(j=y-1, k = 0; j>=0; j--, k++){
                    temp = array[i][j];
                    array[i][j] = array[i][k];
                    array[i][k] = temp;
                    printf("&#37;d\n", array[i][j]);
                }
        }
    Last edited by master5001; 09-19-2008 at 03:29 PM.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I should have probably asked... What do you mean by flip them horizontally? Like reverse them along the y-axis?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    array[i][j] = array[i][j];
    this line does absolutely nothing (it moves to and from the same place in the array).

    Your line that sets "j = ..." in both of your code-samples is also meaningless.

    I'm not sure what you meany "not working", as your code seems to be correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by master5001 View Post
    I should have probably asked... What do you mean by flip them horizontally? Like reverse them along the y-axis?
    yes along the y axis..

    so :

    1 2 3
    4 5 6

    would be

    3 2 1
    6 5 4

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok well the code I posted does that.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do you just want to print them back to front, or change the content around so that the content is stored the other way around?

    If the latter, then master5001 has answered this (you need a temporary variable to store the value when swapping the locations). Although I think you should only iterate over half of the content, as otherwise you'd end up swapping everything back again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    its working now..

    it needed to be

    j >= 0;

    ugh duh

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I bolded that part, I wasn't sure if it was noticeable to you. I usually put it in red, but that was not the only problem that I spotted..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create name array help beginner
    By moscoworbust in forum C Programming
    Replies: 2
    Last Post: 06-08-2009, 07:04 AM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. casting problems warnings segmentation fault.
    By BSmith4740 in forum C Programming
    Replies: 13
    Last Post: 07-03-2008, 01:13 PM
  4. Replies: 17
    Last Post: 03-03-2005, 06:47 PM
  5. beginner problems with array elements...
    By grnphrog in forum C Programming
    Replies: 4
    Last Post: 01-27-2003, 02:38 PM

Tags for this Thread


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