#include <mpi.h>
#include <stdio.h>

int main(int argc, char ** argv)
{
    int myrank, size;

    int sendval;
    int recvval;

    MPI_Status status;

    MPI_Init(&argc, &argv);


    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    sendval=1;
    recvval=10;

    //src=0;
    //dest=1;

    if (myrank == 0)
       { 
        printf("I am rank %d. recvval=%d \n", myrank, recvval);
           MPI_Sendrecv(&sendval,1,MPI_INT,
                myrank+1,1,
                &recvval, 1, MPI_INT,
                9, 1,
                MPI_COMM_WORLD, &status);
        printf("I am rank %d. Sent value %d \n", myrank, sendval);
        printf("I am rank %d. Received val=%d \n", myrank, recvval);
       }
    else if (myrank == 9)
        {
           MPI_Sendrecv(&sendval,1,MPI_INT,
                0,1,
                &recvval, 1, MPI_INT,
                myrank-1, 1,
                MPI_COMM_WORLD, &status);
         printf("I am rank %d. Received val=%d \n", myrank, recvval);
       }
    else 
       {    MPI_Sendrecv(&sendval,1,MPI_INT,
                myrank+1,1,
                &recvval, 1, MPI_INT,
                myrank-1, 1,
                MPI_COMM_WORLD, &status);
       printf("I am rank %d. recvval=%d\n", myrank, recvval);
     }

    MPI_Finalize();
    return 0;
}
