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

int main(int argc, char *argv[]) {
		int size, rank, note = 0, tag = 0, dest;
		MPI_Status status;
		srand(getpid());
		MPI_Init(&argc, &argv);
		MPI_Comm_rank(MPI_COMM_WORLD, &rank);
		MPI_Comm_size(MPI_COMM_WORLD, &size);
		dest = rank + 1;
		if (rank == 0) {
				MPI_Send(&note, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
				printf("I passed a note to 1\n");
				MPI_Recv(&note, 1, MPI_INT, size-1, tag, MPI_COMM_WORLD,
								&status);
				printf("I got the note from %d\n", size-1);
				note += rand()%1000+1;
				note /= size;
				printf("It seems that the average salary here is %d\n", note);
		} else {
				MPI_Recv(&note, 1, MPI_INT, rank-1, tag, MPI_COMM_WORLD,
								&status);
				note += rand()%1000+1;
				if (dest == size) dest = 0;
				MPI_Send(&note, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
		}
		MPI_Finalize();
		return 0;
}

