Why doesn't the renverser
function (reverse) do the job and why does the program exit right after intitialising the table?
#include <stdio.h>
#include <stdlib.h>
void renverser(int t[])
{ int j,i,n;
for(i=0,j=n-1;i<j;i++,j--)
{
int a=t[i];
t[i]=t[j];
t[i]=a;
}
for (i=0;i<n;i++)
{
printf("%d",t[i]);
}
}
int main()
{ int i,n;
float t[100];
printf("donner le taille du tableau a renverser \n :");
scanf("%d",&n);
for (i=0;i<n;i++)
{ printf("donner l'element %d: \n",i);
scanf("%d",&t[i]);
}
renverser(t);
return 0;
}
The function does not make a sense because the size of the passed array is unknown.
Moreover the function has undefined behavior because there is used uninitialized variable n
.
And the type of the function parameter is int *
while the passed argument expression has the type float *
.
It seems you mean the following array declaration
int t[100];
And the function should do only one thing: reverse an array.
The result array you should output in main.
The function can be declared and defined the following way
void renverser( int t[], size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
int tmp = t[i];
t[i] = t[n - i - 1];
t[n - i - 1] = tmp;
}
}
and the function can be called like
renverser( t, n );