Swap function of elements in array

27,134

Solution 1

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;
   }

Solution 2

I strongly suspect that this,

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

Should be

void
swap(double *array, int a, int b)
   {
   double temp = *array[a]; /* <- it's a double */
   *array[a] = *array[b];
   *array[b] = temp;
   }

And to call it, this

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

should be

swap(array,0,2);

finally, if you prefer, pass in two pointers with the temp variable and call it with swap(array[0], array[2]),

void swap(double *a, double *b) 
{ 
  double temp = *a;
  *a = *b;
  *b = temp;
}

Solution 3

Each time you add a type in front of a variable name you are declaring a new variable of that type and with that name.

So you declare array at the top of main():

double array[3] = {0};
// ... some code in between
swap(double array[0],double array[2]); // here you re-declare it

You don't need the double here because the program already knows array contains doubles. That is why you are getting the redefinition of parameter 'array' error. Remove both double types in the second statement:

double array[3] = {0};
// ... some code in between
swap(array[0], array[2]);

Also, your array contains only one element, namely 0. But you want to swap the first array[0] and the third array[2] element which doesn't exist. Create a bigger array, something like:

double array[3] = {0, 1, 2};
// ... some code in between
swap(array[0], array[2]);

Furthermore, your definition of the swap function is wrong too.

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

You don't need the [position] part as the parameter. This function is generic and shouldn't be allowed to only swap array[0] and array[2]. All you need here is a double * because array[position] is a double and that is what you are passing into the function. You are passing this value by reference (that is why you have the *) so that the swap will actually work outside the function and not just swap locally.

Read more about pointer/references in C

This is what you should have:

void swap(double *number1, double *number2)
{
   int *temp = number1;
   number1 = number2;
   number2 = temp;
}

Solution 4

redefinition of parameter 'array' : 

You are getting this error because of the following function call :

Error 1 :    
    swap(double array[0],double array[2]);

We never pass the data type with the variable while invoking a function, just pass the
parameters.

Correction : 
    swap(array[0],array[2]);     // if you want to pass the values as parameters.
     //or
    swap(array, array); // for passing pointers to the array( pass by reference )      

You can read about pass by value and pass by reference. A point to be noted that in C
the arrays are always passed by reference.

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

This error comes because the prototype of swap function given by you is

    void swap(double *array[0],double *array[2]);     
  1. That's not a standard way of giving function prototypes.

    1. But when you are calling the function you are passing only double values, instead you should be passing the address of the double array as per your prototype definition.

    Correction :

    1. prototype declaration :
      void swap(double *, double *); // pass by reference
      void swap(double, double); // pass by value

2.Mapping correct prototypes with their respective function calls:

If you want to pass the base address of the array as argument :          
prototype : void swap(double *, double *);                   
call      : swap(array, array);             

But here I suggest you need not pass two parameters, if they are same.

If you want to pass a specific value stored in array at some index say 'i':
prototype : void swap(double, double);
call      : swap(array[i], array[j]);

Where 'i' and 'j' are indexes to the array.

Share:
27,134
libbned
Author by

libbned

Updated on July 06, 2022

Comments

  • libbned
    libbned almost 2 years

    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;
           }
    
    • Box Box Box Box
      Box Box Box Box over 8 years
      @self, it smells like homework to me.
  • MD. Khairul Basar
    MD. Khairul Basar over 9 years
    the problem is in passing the values to function and in function calling...when you are calling a function you just need to specify the variable's names you are passing as argument not data type...don't use data types in function calling...
  • nem035
    nem035 over 9 years
    he can directly swap within the array without passing the array in the swap function since you can pass by reference in c so you don't really need the array parameter
  • Keith Nicholas
    Keith Nicholas over 9 years
    errr, your temp variable is an int
  • nem035
    nem035 over 9 years
    @Well yes, your way works as well, just made a comment that I realize now was kind of unnecessary, seemed at first the question poster wanted it that way... sorry for spam :), gave you the deserved +1
  • nem035
    nem035 over 9 years
    don't just give him full code with no explanation. @ElliottFrisch's answer is much better showing him many ways he can do stuff...