libbned libbned - 1 month ago 8
C Question

Swap function of elements in array

the final task for me to perform is to swap the value of the first element of my array (array[0] with the last element of my array (array[2]); however, whenever i compile i receive these three errors and cannot seem to fix them:

:4 redefinition of parameter 'array'

and

:32 incompatible type for argument 1 of 'swap'
:32 incompatible type for argument 2 of 'swap'

here is my code:

#include <stdio.h>

void
swap(double *array[0],double *array[2])
{
int temp = *array[0];
*array[0] = *array[2];
*array[2] = temp;
}
int
main(int argc, char **argv)
{

double array[3] = {0};
double realNumber;
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);
printf("enter the first real number:\n");
scanf("%lf",&realNumber);
array[0] = realNumber;
printf("enter the second real number:\n");
scanf("%lf",&realNumber);
array[1] = realNumber;
printf("enter the third real number:\n");
scanf("%lf",&realNumber);
array[2] = realNumber;
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);

swap(double array[0],double array[2]);

printf("after swapping...\n");
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);
return 0;
}

Answer

use this...updated code...

#include <stdio.h>

void swap(double *a,double *b)
   {
   double temp = *a;
   *a = *b;
   *b = temp;
   }    
int main(int argc, char **argv)
   { 

   double array[3] = {0};
   double realNumber;
   printf("array[0] is %lf\n",array[0]);
   printf("array[1] is %lf\n",array[1]);
   printf("array[2] is %lf\n",array[2]);
   printf("enter the first real number:\n");
   scanf("%lf",&realNumber);
   array[0] = realNumber;
   printf("enter the second real number:\n");
   scanf("%lf",&realNumber);
   array[1] = realNumber;
   printf("enter the third real number:\n");
   scanf("%lf",&realNumber);
   array[2] = realNumber;
   printf("array[0] is %lf\n",array[0]);
   printf("array[1] is %lf\n",array[1]);
   printf("array[2] is %lf\n",array[2]);

   swap(array,array+2);

   printf("after swapping...\n");
   printf("array[0] is %lf\n",array[0]);
   printf("array[1] is %lf\n",array[1]);
   printf("array[2] is %lf\n",array[2]);
   return 0;
   }
Comments