Reverse An Array | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I miss C! It was my first programming language in university, it should be learnt before all I think...

rimaben
Автор

Thanks, really one of the most perfect explanation I've ever been taught.

xxyounn
Автор

you're a great help of passing my cs50 course

AlexSchneiderDev
Автор

"Advanced" way is without third variable:
Arithmetic operations: a = a + b; b = a - b; a = a - b;
XOR operations: a = a ^ b; b = a ^ b; a = a ^ b;

But somehow in my laptop looping through cycles for:
Arithmetic : 7.7 sec
XORing : 7.7 sec
Temp variable : 2.7 sec

Who could a thought?

I-PixALbI-I
Автор

I thought C didn't support pass by reference, and that you needed pointers for this kind of function?

bizarrebeats
Автор

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("\n");
}

justcurious
Автор

I couldn't really understand the length / 2 part, could someone briefly explain?

bugrahankaramollaoglu
Автор

how about the case where the array has an odd integer of length? would you make another case for it?

knightwik
Автор

Thanks for the great explanation
Another way to do it:
#include <stdio.h>

int main()
{
int sizeOfArray, element;

printf("enter size of array: ");
scanf("%d", &sizeOfArray);
printf("enter elements of array: \n");
int ar1[sizeOfArray];
int ar2[sizeOfArray];
for(int i=0; i<sizeOfArray; i++){
scanf("%d", &element);
ar1[i]= element;
}
for(int i=0, counter=1; i<sizeOfArray; i++, counter++){
ar2[i]= ar1[sizeOfArray-counter];
printf("%d, ", ar2[i]);
}
return 0 ;
}

A.Safwat
Автор

How do you know that without watching a video called "Reverse An Array | C Programming Example
" ???? Please I wanna be good at programming without looking at the internet.

simplex
Автор

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

sarker_sudi
Автор

Why use void instead of int while declaring function? I'm just learning so pls reply even if this might be dumb question

vedaajayakumar
Автор

I couldn't understand (length-i-1)part. Could someone briefly explain?

Psc_Aspirant
Автор

how about this ...?


#include <stdio.h>
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;
}

mdkanon-xt
welcome to shbcf.ru