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

#define ndata 1000 

int main(int argc, char* argv[]){

    int ierr, me, nprocs, i=0, left, right, count=1;
    MPI_Status status;
    
    float a;
    float b;
    float somma;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);

    	
    a = me;
    b = -1;
    somma = a;
        
    right = (me+1)%nprocs;
    left = ((me-1)+nprocs)%nprocs;
 
    while(count++<nprocs){
      MPI_Sendrecv(&a, 1, MPI_REAL, left, 0, &b, 1, MPI_REAL, right, 0, MPI_COMM_WORLD, &status);
      a = b;
      somma += a;
    }
    printf("\n\tI am proc %d and somma(0) = %1.2f \n", me, somma);
  
    MPI_Finalize();

}

	
