![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 58
| Array Problems? (Im a beginner) Code: array[x][y]; 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;
}
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 | |
| | #2 |
| Banned 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 | |
| | #3 |
| Banned 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 | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: array[i][j] = array[i][j]; 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 | |
| | #5 |
| Registered User Join Date: Sep 2008
Posts: 58
| |
| scarlet00014 is offline | |
| | #6 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Ok well the code I posted does that. |
| master5001 is offline | |
| | #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 | |
| | #8 |
| Registered User Join Date: Sep 2008
Posts: 58
| its working now.. it needed to be j >= 0; ugh duh |
| scarlet00014 is offline | |
| | #9 |
| Banned 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 | |
![]() |
| Tags |
| array, arrays, beginner |
| Thread Tools | |
| Display Modes | |
|
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 |