Reverse An Array | C Programming Example

  Рет қаралды 30,613

Portfolio Courses

Portfolio Courses

3 жыл бұрын

An example of how to reverse an array using C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 39
@xxyounn
@xxyounn 2 жыл бұрын
Thanks, really one of the most perfect explanation I've ever been taught.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you so much for the positive feedback, I’m really glad to hear you enjoyed it! :-)
@Alex.Shalda
@Alex.Shalda Жыл бұрын
you're a great help of passing my cs50 course
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad to hear that Alex, and good luck with the cs50 course! :-)
@rupeshkumaryadav7443
@rupeshkumaryadav7443 2 жыл бұрын
That was awesome..
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you!
@Alex.Shalda
@Alex.Shalda Жыл бұрын
Thanks a lot!!!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Alex! :-)
@bizarrebeats3583
@bizarrebeats3583 2 жыл бұрын
I thought C didn't support pass by reference, and that you needed pointers for this kind of function?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's true! Technically C has what's called pass-by-pointer, and we use pass-by-pointer to help us carry out "pass-by-reference". But as a practical matter, most people use the terminology pass-by-reference when referring to this technique. This video might help to sort out what's going on when we pass an array to a function.... Passing an Array to a Function - kzbin.info/www/bejne/pZaVk42Bn86KqMk. These videos might be helpful too: Introduction to Pointers - kzbin.info/www/bejne/aHinmot9areZhKc Pass By Reference - kzbin.info/www/bejne/iJbGqYSLiqqCpJY
@phanindraslvs7469
@phanindraslvs7469 Жыл бұрын
Nicee👍
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you liked it Phanindra! :-)
@bugrahankaramollaoglu
@bugrahankaramollaoglu 2 жыл бұрын
I couldn't really understand the length / 2 part, could someone briefly explain?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question Bugra! 🙂 So the way that integer division works in C is that the decimal portion is effectively 'chopped off' and we're just left with the whole number portion. So for example if we have length = 9, then 9 / 2 will be 4, instead of 4.5. So if we have an even number like 12 / 2 we'll get 6. With an odd number like 13 / 2 we'll get 6, instead of 6.5. And that's actually OK for this algorithm too... if we have an array like this with 7 elements: int myarray[] = {1,2,3,4,5,6,7} We would get length / 2 = 3 (instead of 3.5). And so therefore our loop would "stop" when i = 3. And that's actually OK... - when i is 0, we would swap 1 and 7 - when i is 1, we would swap 2 and 6 - when i is 2, we would swap 3 and 5 - when is is 3, we stop and we would have int myarray[] = {7,6,5,4,3,2,1} And we don't care that we never "swapped" 4 because as the middle element in an array with an odd number of elements, we want it to be in the same position in the reversed array. It's correct to *not* swap that middle element. Hopefully this helps! 😀
@knightwik
@knightwik 2 жыл бұрын
how about the case where the array has an odd integer of length? would you make another case for it?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question! :-D It will actually work out OK because of the way integer division works. So when we have: (length / 2). If length is say for example 7, 7 / 2 will give us 3 in C, as integer division means the result is rounded down to the nearest integer. So the loop will only execute for i = 0, i = 1, i = 2. But that's OK. We only need to swap the elements at these indexes with their corresponding elements on the other side of the array. There is no need to swap the "middle element" at index 3 because it will be in the same position in the "reverse" of the array as it is in the original array... reversing the array will leave that middle odd element in the same position as we want. :-)
@knightwik
@knightwik 2 жыл бұрын
@@PortfolioCourses oh wow that works out nicely. Thank you!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@sarker_sudi21
@sarker_sudi21 11 ай бұрын
Could you kindly do the same video except now you pass the array to the function and it returns a pointer to the reversed array we created. Thanks in advance
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
This video goes over one technique of allocating space for an array in memory and returning a pointer to that array, it might help you out: kzbin.info/www/bejne/q3qUe2ulZteMZ5I. :-)
@vedaajayakumar6617
@vedaajayakumar6617 28 күн бұрын
Why use void instead of int while declaring function? I'm just learning so pls reply even if this might be dumb question
@PortfolioCourses
@PortfolioCourses 28 күн бұрын
void is for a function that doesn’t return any value :-)
@justcurious1940
@justcurious1940 7 ай бұрын
Nice 🙃: void Reverse_array(int *array,int length){ int temp; int middle = length/2; for(int i = 0 ; i < middle ; i++){ temp = array[i]; array[i] = array[length-1-i]; array[length-1-i] = temp; } } void Print_array(int *array,int length){ for(int i = 0 ; i < length ; i++){ printf("%d\t",array[i]); } printf(" "); }
@comoyun
@comoyun 2 ай бұрын
using pascal case for function names is not common because that is traditionally used for class names, so i'd suggest adopting camel-case or snake-case habit.
@justcurious1940
@justcurious1940 2 ай бұрын
​@@comoyun I'm currently learning JS, and I'm paying more attention to my variable names, Thanks for the advice. I love C but I need to pay my bills too.
@comoyun
@comoyun 2 ай бұрын
@@justcurious1940 I use JS too. I wanted to level up my game :) also good luck.
@Psc_Aspirant124
@Psc_Aspirant124 Жыл бұрын
I couldn't understand (length-i-1)part. Could someone briefly explain?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Vivek! :-) Let's say we have an array like this: {12, 11, 10, 9, 8} it has length 5. We can reverse the array by swapping the first element (12) with the last element (8), the second element (11) with the second last element (9), and so on until we reach the middle of the array. We could use a loop with a counter variable i that goes from 0, to 1, to 2 and so on until we reach the middle. Then in the first iteration of the loop i would be 0, the index of the first element in the array. And we would want to swap it with the last element in the array. The index of the last element in the array is going to be "length - 1". Because arrays are indexed starting from 0, so in the case of the above array the length is 5 but that last element '8' is at the index 4.... i.e. length - 1 because 5 - 1 is 4. Now if the loop runs again and this time i is 1, we want to swap the second element (which will be at the index 1) with the second last element. To get the second last element, we would want length - 1 - 1, which would give us 3 in the case of the above array. If on the next loop iteration we want the third last element, it would be length - 2 - 1.... and we have a pattern here now.... so in other words, length - i - 1 will give us the "ith element from the end" of the array. :-) We need that element to swap it with the corresponding ith element from the start of the array.
@abdulrahmansafwat1555
@abdulrahmansafwat1555 2 жыл бұрын
Thanks for the great explanation Another way to do it: #include int main() { int sizeOfArray,element; printf("enter size of array: "); scanf("%d", &sizeOfArray); printf("enter elements of array: "); int ar1[sizeOfArray]; int ar2[sizeOfArray]; for(int i=0; i
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Yes, that looks good! :-)
@sarker_sudi21
@sarker_sudi21 11 ай бұрын
why is your i looping through the whole array? It only needs to go thought the first half of the array
@abdulrahmansafwat1555
@abdulrahmansafwat1555 11 ай бұрын
@@sarker_sudi21 Looking at it again after 1 year, I would say my code is not completely correct. I copied the array into another array in reverse... I didn't reverse the same array. That's why the loop is iterating through the whole array.
@justcurious1940
@justcurious1940 7 ай бұрын
@@abdulrahmansafwat1555 The code looks correct, But the technique is costly.
@abdulrahmansafwat1555
@abdulrahmansafwat1555 7 ай бұрын
​@@justcurious1940Yeah, I copied the array into another in reverse, instead of reversing the original one. So, I kind of avoided the problem instead of actually solving it. Same way I handle life problems haha
@Codewitheyezyc
@Codewitheyezyc Жыл бұрын
This reserved array is complex
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Hopefully the video helped! :-)
@user-xp6mh5jb4n
@user-xp6mh5jb4n 8 ай бұрын
how about this ...? #include int main() { int sz; scanf("%d", &sz); int a[sz], b[sz]; for (int i = 0, j = sz - 1; i < sz; i++, j--) { scanf("%d", &a[i]); b[j] = a[i]; } for (int i = 0; i < sz; i++) { printf("%d ", b[i]); } return 0; }
Check If A String Is A Palindrome | C Programming Example
10:56
Portfolio Courses
Рет қаралды 36 М.
Reverse an Array ( C++ )
9:52
Heidi Gentry Kolen
Рет қаралды 6 М.
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 10 МЛН
لااا! هذه البرتقالة مزعجة جدًا #قصير
00:15
One More Arabic
Рет қаралды 33 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 34 МЛН
Count the Occurrences of a Value in an Array | C Programming Example
10:26
C Program To Print Elements of Array In Reverse Order
4:56
Technotip
Рет қаралды 80 М.
Stop, Intel’s Already Dead! - AMD Ryzen 9600X & 9700X Review
13:47
Linus Tech Tips
Рет қаралды 1,1 МЛН
Create a copy of an array | C Programming Example
11:33
Portfolio Courses
Рет қаралды 7 М.
Computing the Fibonacci Sequence | C Programming Example
15:48
Portfolio Courses
Рет қаралды 10 М.
Compute the Average of an Array | C Programming Example
8:39
Portfolio Courses
Рет қаралды 13 М.
Remove Duplicate Array Elements | C Programming Example
9:40
Portfolio Courses
Рет қаралды 11 М.
Find the Maximum Number in an Array | C Programming Example
8:18
Portfolio Courses
Рет қаралды 25 М.
Japanese | Can you solve this? | Math Olympiad
9:48
Master T Maths Class
Рет қаралды 251
Света квадробер в парке! Часть 4 #shorts
0:31
Настя AmyMyr
Рет қаралды 1,7 МЛН
Быстрые листья для голубцов
0:36
Мистер Лайфхакер
Рет қаралды 10 МЛН
Хочет выиграть для папы...
0:21
MovieLuvsky
Рет қаралды 1,3 МЛН
Читерская рыбалка!🤯🐟 (@fishinglover790)
0:24
Взрывная История
Рет қаралды 2,9 МЛН
Света квадробер в парке! Часть 4 #shorts
0:31
Настя AmyMyr
Рет қаралды 1,7 МЛН