× Home
Next Lec → ← Previous Lec

Array reversal exercise

Reverse the array if arr[]={1,2,3,4} then the output should be {4,3,2,1}

I was just messing around with the code and accidently my code works here it is

                    

                            #include <stdio.h>
                            void arrayRev(int size, int arr[size])
                            {
                                // copying reverse of an array to another array
                                int store = size - 1;
                                int arr2[size];
                                for (int i = 0; i < size; i++)
                                {
                                    arr2[store] = arr[i];
                                    store--;
                                }
                                for (int i = 0; i < size; i++)
                                {
                                    printf("%d,", arr2[i]);
                                }
                            
                                // int arr2[];
                            }
                            int main()
                            {
                                int arr[] = {1, 2, 3, 4, 5, 6, 67};
                                int size = sizeof(arr) / sizeof(int);
                                // printf("%d", size);
                                arrayRev(size, arr);
                                return 0;
                            }
                    
                
                


                        #include <stdio.h>
                        void arrayRev(int arr[])
                        {
                            // we will do swaping- will swap 0 index with last index then so on
                            int temp;
                            for (int i = 0; i < 7 / 2; i++)
                            {
                                // swap item arr[i] with item arr[6-i]
                                temp = arr[i];
                                arr[i] = arr[6 - i];
                                arr[6 - i] = temp;
                                // if this runs for whole then it won't change anything cuse it will redo it.
                                // so we have to stop it int middle for that we do i<7/2
                            }
                        }
                        void printArr(int arr[])
                        {
                            for (int i = 0; i <<7; i++)
                            {
                                printf("The value of element %d is %d\n", i, arr[i]);
                            }
                        }
                        int main()
                        {
                            int arr[] = {1, 2, 3, 4, 5, 6, 67};
                            printf("\n Before reversing the array\n");
                            printArr(arr);
                            arrayRev(arr);
                            printf("\n After reversing the array\n");
                            printArr(arr);
                        
                            return 0;
                        }
                        
                
               

In my program I have used two arrays but here harry have used only one array and have done swapping