C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-19-2008, 02:19 PM   #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?
scarlet00014 is offline   Reply With Quote
Old 09-19-2008, 02:24 PM   #2
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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("%d\n", array[i][j]);
            }
    }

Last edited by master5001; 09-19-2008 at 02:29 PM.
master5001 is offline   Reply With Quote
Old 09-19-2008, 02:25 PM   #3
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
I should have probably asked... What do you mean by flip them horizontally? Like reverse them along the y-axis?
master5001 is offline   Reply With Quote
Old 09-19-2008, 02:26 PM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-19-2008, 02:41 PM   #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
scarlet00014 is offline   Reply With Quote
Old 09-19-2008, 02:52 PM   #6
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Ok well the code I posted does that.
master5001 is offline   Reply With Quote
Old 09-19-2008, 02:54 PM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-19-2008, 02:57 PM   #8
Registered User
 
Join Date: Sep 2008
Posts: 58
its working now..

it needed to be

j >= 0;

ugh duh
scarlet00014 is offline   Reply With Quote
Old 09-19-2008, 02:59 PM   #9
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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..
master5001 is offline   Reply With Quote
Reply

Tags
array, arrays, beginner

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
create name array help beginner moscoworbust C Programming 2 06-08-2009 06:04 AM
allocation and reallocation of memory dynamically of an integer array zamir C++ Programming 16 05-29-2009 07:25 PM
casting problems warnings segmentation fault. BSmith4740 C Programming 13 07-03-2008 12:13 PM
A couple of beginner problems, one regarding strcpy and another regarding pointers edd1986 C Programming 17 03-03-2005 06:47 PM
beginner problems with array elements... grnphrog C Programming 4 01-27-2003 02:38 PM


All times are GMT -6. The time now is 02:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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