I am building up to searching and sorting so I thought a little practice with swapping numbers would be relevant. I started with the most simple of problems swapping two integer variables then decided to have some fun and see if I could apply that too an array. So I set one up and flipped it backward.
I know it's not all that impressive but hey I am just starting off ;P
Here is my code
I am sure some of you are wondering why I am posting so frequently. Well that is because I am learning on my own. I don't have the feed back that you would get studying this at school and it's been super valuable to hear the opinions of those who know what they are doing. So please be patient with me.Code:#include <stdio.h> void SwapNumb( int* a, int* b); void PrintArray(int size, int* ptr); int main(void){ int fnumber, snumber, i ; int* pf, ps; int anumbers[] = {0,1,2,3,4,5,6,7,8,9}; pf = &fnumber; ps = &snumber; *pf = 5; *ps = 6; printf("\n\n"); printf("Here are numbers %d and %d \n", fnumber,snumber); SwapNumb(pf,ps); printf("Here they are after the swap %d and %d \n\n", fnumber,snumber); printf("Here is an array\n"); PrintArray(10,anumbers); printf("\n\n"); pf = anumbers; ps = anumbers + 9; for ( i = 0 ; i < 5 ; i++){ SwapNumb(pf,ps); pf ++; ps --; } printf("Here is an array backwards\n"); PrintArray(10,anumbers); printf("\n\n"); return 0; } /*--------------------------------------------------------------------------- * SwapNumb * parameters: * int* a: is a pointer to the location of the first number to be swapped * int* b: is a pointer to the location of the second number to be swapped * prints: nothing * returns; nothing *------------------------------------------------------------------------------*/ void SwapNumb( int* a, int* b){ int holder; holder = *a; *a = *b; *b = holder; } /*------------------------------------------------------------------------------- * PrintArray * parameters: * int size: is the size of the array to print * int* ptr is a pointer to the array to print * prints: an array * returns: nothing *------------------------------------------------------------------------------*/ void PrintArray(int size, int* ptr){ int i; for ( i = 0 ; i < size ; i++ ){ printf("%d,", *(ptr + i)); } }