SlideShare a Scribd company logo
1 of 2
Download to read offline
Write an MPI program to accomplish the following for a number of processes:
Each processes generate a list of ten random integers (including the process with rand zero) in the
range 0 to 100, sorts them and sends its sorted 10 numbers to the process with rank zero. The
process with rank zero merges all the lists into one list and display them.
The serial program for this question is:
//a.c
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
const int n=10;
int *a, *b, *c;
void Merge(int a[], int b[], int c[]);
void Sort(int* x);
int main()
{
int i;
srand(time(0));
a =(int *)malloc(n*sizeof(int));
b =(int *)malloc(n*sizeof(int));
c =(int *)malloc(2*n*sizeof(int));
for(i = 0; i < n; i++)
{
a[i] = rand()%100;
b[i] = rand()%100;
}
Sort(a);
Sort(b);
Merge(a, b, c);
return 0;
}
int findIndexOfSmallest(int a[], int n, int from){
int indexOfSmallest = from;
int i;
for(i = from; i < n; i++)
if(a[i] < a[indexOfSmallest])
indexOfSmallest = i;
return indexOfSmallest;
}
void Sort(int* x){
// int* x = (int *)a;
int i, indexOfSmallest, t;
for(i = 0; i < n ; i++){
indexOfSmallest = findIndexOfSmallest(x, n, i);
t = x[i];
x[i] = x[indexOfSmallest];
x[indexOfSmallest] = t;
}
for(i = 0; i < n; i++)
printf("%d, ", x[i]);
printf("n------------------------------n");
}
void Merge(int a[], int b[], int c[]){
int aIndex = 0, bIndex = 0, cIndex = 0, i;
while(aIndex < n && bIndex < n)
if(a[aIndex] < b[bIndex])
c[cIndex++] = a[aIndex++];
else
c[cIndex++] = b[bIndex++];
while(aIndex < n)
c[cIndex++] = a[aIndex++];
while(bIndex < n)
c[cIndex++] = b[bIndex++];
for(i = 0; i < 2 * n; i++)
printf("%d, ", c[i]);
printf("n^^^^^^^^^^^^^^^^^^^^^^^^^^n");
}

More Related Content

Similar to Write an MPI program to accomplish the following for a numbe.pdf

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
Vishal Singh
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
gkgaur1987
 

Similar to Write an MPI program to accomplish the following for a numbe.pdf (20)

54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
C++17 not your father’s c++
C++17  not your father’s c++C++17  not your father’s c++
C++17 not your father’s c++
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
convert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdfconvert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdf
 
C#.net
C#.netC#.net
C#.net
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
week-10x
week-10xweek-10x
week-10x
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
C lab
C labC lab
C lab
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 

More from aaryanentp

ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdfZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
aaryanentp
 
You have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdfYou have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdf
aaryanentp
 
write the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdfwrite the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdf
aaryanentp
 

More from aaryanentp (20)

ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdfZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
 
Zahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdfZahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdf
 
Zachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdfZachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdf
 
Youve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdfYouve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdf
 
Your selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdfYour selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdf
 
Your company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdfYour company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdf
 
You will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdfYou will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdf
 
You want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdfYou want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdf
 
You have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdfYou have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdf
 
You have three alternatives to invest A1 Stocks A2 Bond.pdf
You have three alternatives to invest A1 Stocks A2  Bond.pdfYou have three alternatives to invest A1 Stocks A2  Bond.pdf
You have three alternatives to invest A1 Stocks A2 Bond.pdf
 
You have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdfYou have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdf
 
You know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdfYou know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdf
 
You have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdfYou have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdf
 
you have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdfyou have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdf
 
You are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdfYou are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdf
 
You are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdfYou are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdf
 
You are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdfYou are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdf
 
You are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdfYou are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdf
 
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdfyi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
 
write the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdfwrite the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

Write an MPI program to accomplish the following for a numbe.pdf

  • 1. Write an MPI program to accomplish the following for a number of processes: Each processes generate a list of ten random integers (including the process with rand zero) in the range 0 to 100, sorts them and sends its sorted 10 numbers to the process with rank zero. The process with rank zero merges all the lists into one list and display them. The serial program for this question is: //a.c #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include<time.h> const int n=10; int *a, *b, *c; void Merge(int a[], int b[], int c[]); void Sort(int* x); int main() { int i; srand(time(0)); a =(int *)malloc(n*sizeof(int)); b =(int *)malloc(n*sizeof(int)); c =(int *)malloc(2*n*sizeof(int)); for(i = 0; i < n; i++) { a[i] = rand()%100; b[i] = rand()%100; } Sort(a); Sort(b); Merge(a, b, c); return 0; } int findIndexOfSmallest(int a[], int n, int from){ int indexOfSmallest = from; int i; for(i = from; i < n; i++) if(a[i] < a[indexOfSmallest]) indexOfSmallest = i; return indexOfSmallest; } void Sort(int* x){ // int* x = (int *)a; int i, indexOfSmallest, t;
  • 2. for(i = 0; i < n ; i++){ indexOfSmallest = findIndexOfSmallest(x, n, i); t = x[i]; x[i] = x[indexOfSmallest]; x[indexOfSmallest] = t; } for(i = 0; i < n; i++) printf("%d, ", x[i]); printf("n------------------------------n"); } void Merge(int a[], int b[], int c[]){ int aIndex = 0, bIndex = 0, cIndex = 0, i; while(aIndex < n && bIndex < n) if(a[aIndex] < b[bIndex]) c[cIndex++] = a[aIndex++]; else c[cIndex++] = b[bIndex++]; while(aIndex < n) c[cIndex++] = a[aIndex++]; while(bIndex < n) c[cIndex++] = b[bIndex++]; for(i = 0; i < 2 * n; i++) printf("%d, ", c[i]); printf("n^^^^^^^^^^^^^^^^^^^^^^^^^^n"); }