Thanks, really one of the most perfect explanation I've ever been taught.
@PortfolioCourses2 жыл бұрын
Thank you so much for the positive feedback, I’m really glad to hear you enjoyed it! :-)
@rimaben21202 ай бұрын
I miss C! It was my first programming language in university, it should be learnt before all I think...
@Alex.Shalda Жыл бұрын
you're a great help of passing my cs50 course
@PortfolioCourses Жыл бұрын
I'm glad to hear that Alex, and good luck with the cs50 course! :-)
@justcurious194011 ай бұрын
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(" "); }
@comoyun6 ай бұрын
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.
@justcurious19406 ай бұрын
@@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.
@comoyun6 ай бұрын
@@justcurious1940 I use JS too. I wanted to level up my game :) also good luck.
@bizarrebeats35833 жыл бұрын
I thought C didn't support pass by reference, and that you needed pointers for this kind of function?
@PortfolioCourses3 жыл бұрын
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
@A.Safwat82 жыл бұрын
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
@PortfolioCourses2 жыл бұрын
Yes, that looks good! :-)
@sarker_sudi21 Жыл бұрын
why is your i looping through the whole array? It only needs to go thought the first half of the array
@A.Safwat8 Жыл бұрын
@@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.
@justcurious194011 ай бұрын
@@A.Safwat8 The code looks correct, But the technique is costly.
@A.Safwat811 ай бұрын
@@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
@knightwik2 жыл бұрын
how about the case where the array has an odd integer of length? would you make another case for it?
@PortfolioCourses2 жыл бұрын
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. :-)
@knightwik2 жыл бұрын
@@PortfolioCourses oh wow that works out nicely. Thank you!
@PortfolioCourses2 жыл бұрын
You’re welcome! :-)
@bugrahankaramollaoglu2 жыл бұрын
I couldn't really understand the length / 2 part, could someone briefly explain?
@PortfolioCourses2 жыл бұрын
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! 😀
@vedaajayakumar66174 ай бұрын
Why use void instead of int while declaring function? I'm just learning so pls reply even if this might be dumb question
@PortfolioCourses4 ай бұрын
void is for a function that doesn’t return any value :-)
@sarker_sudi21 Жыл бұрын
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 Жыл бұрын
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. :-)
@Psc_Aspirant124 Жыл бұрын
I couldn't understand (length-i-1)part. Could someone briefly explain?
@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.
@rupeshkumaryadav74433 жыл бұрын
That was awesome..
@PortfolioCourses3 жыл бұрын
Thank you!
@Alex.Shalda Жыл бұрын
Thanks a lot!!!
@PortfolioCourses Жыл бұрын
You're very welcome Alex! :-)
@Codewitheyezyc Жыл бұрын
This reserved array is complex
@PortfolioCourses Жыл бұрын
Hopefully the video helped! :-)
@phanindraslvs7469 Жыл бұрын
Nicee👍
@PortfolioCourses Жыл бұрын
I'm glad you liked it Phanindra! :-)
@mdkanon-x5t Жыл бұрын
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; }