MPO
MPO
MPO
/*
1. Write a simple C program to generate a random number without initializing the seed for
the random number generator {Within a span of "one second", if you keep running the
program multiple times, you should see the same random number output}
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
time_t t;
int random_number;
return 0;
}
/*
2. Write a C program to generate a random number in C with initializing the seed for the
random number generator {With every run you should observe a differnt random number}.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int random_number;
return 0;
}
/*
3. Write a simple C program to generate a random number between 0 and 9.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int random_number;
return 0;
}
//4. Write a simple C program to generate a random number between 1 and 100.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int random_number;
return 0;
}
#include <stdio.h>
#include <time.h>
int main() {
// Declare a variable to hold the current time
time_t current_time;
return 0;
}
//6. Write a C program to find the execution time of outputting Hello world 10 times and
10,000 times respectively.
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double cpu_time_used;
return 0;
}
//7. Write a C program to populate a matrix of size 5*10 by generating random numbers in
the range 1 to 10. Display the matrix to the screen.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 5
#define COLS 10
int main() {
int matrix[ROWS][COLS];
int i, j;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 20
#define COLS 50
int main() {
int matrix[ROWS][COLS];
int i, j;
int smallest, largest, sum;
clock_t start, end;
double total_time;
// Populate the matrix with random numbers in the range 101 to 201
start = clock();
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
matrix[i][j] = (rand() % 101) + 101;
}
}
end = clock();
return 0;
}
LAB-2:
/*1. (i) Write a C program to search for a given element in a matrix. The program should take
a matrix and a target element as input, and it should output whether the element is present in
the matrix or not.
*/
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int target, found;
if (found) {
printf("Element %d is present in the matrix.\n", target);
} else {
printf("Element %d is not present in the matrix.\n", target);
}
return 0;
}
/*(ii) Modify your code so that you convert the code into a function called search(). Signature
of search() can be as below:
int search(int mat[][], int rows, int cols, int key).
Let the search function return 0 on finding the key, and return -1 if the key is not present in
the matrix. Write a suitable main function which invokes search() and observe your results.
*/
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int key, result;
if (result == 0) {
printf("Key %d is present in the matrix.\n", key);
} else {
printf("Key %d is not present in the matrix.\n", key);
}
return 0;
}
/*
2. (i) Write a C program to sort the elements of a matrix. The program should take a matrix
as input and display the sorted matrix.
*/
#include <stdio.h>
#define ROWS 3
#define COLS 3
// Function to sort the elements of a matrix
void sortMatrix(int matrix[ROWS][COLS]) {
int temp;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
for (int k = 0; k < ROWS; k++) {
for (int l = 0; l < COLS; l++) {
if (matrix[i][j] < matrix[k][l]) {
temp = matrix[i][j];
matrix[i][j] = matrix[k][l];
matrix[k][l] = temp;
}
}
}
}
}
}
int main() {
int matrix[ROWS][COLS];
return 0;
}
/*
(ii) Modify your code so that you convert the code into a function called sort(). Signature of
matrix_sort() can be as below:
int sort(int mat[][], int rows, int cols).
Let the return value of sort() be zero always. Write a suitable main() which invokes sort() and
observe your results.
*/
#include <stdio.h>
int main() {
int mat[3][3];
int rows = 3;
int cols = 3;
return 0;
}
/*
3. (i) Write a C program to interchange any two rows of a matrix. The user should input the
matrix, and the row numbers to be interchanged, and the program should display the
modified matrix.
*/
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int rows, cols, row1, row2;
return 0;
}
/*
(ii) Make suitable modifications to your code so that you have function for the same.Write a
suitable main() which invokes your function and observe your results.
*/
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int rows, cols, row1, row2;
return 0;
}
/*
4. Implement a C function to find the transpose of a given matrix. The program should take a
matrix as input, computeits transpose, and display the result.
*/
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
// Computing transpose
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transpose[i][j] = matrix[j][i];
}
}
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int rows, cols;
return 0;
}
/*
5. Implement a C program to multiply two integer matrices. The program should take two
matrices as input from the user and display the result. If the two matrices are not compatible
for multiplication, output an adequate message to the user.
*/
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int result[MAX_ROWS][MAX_COLS];
int main() {
int mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS][MAX_COLS];
int rows1, cols1, rows2, cols2;
return 0;
}
/*
6. Develop a C program to subtract one matrix from another. Make sure they are compatible
by comparing their rows and columns; give an error message otherwise. The program should
populate both the matrices with random numbers between 20 and 50, display the matrices
first and later display the result matrix. How much time does your computation take?
Convert the code to a function and determine how much time the code takes when your
function is invoked by your main.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main() {
int mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
int rows, cols;
printf("\nMatrix 2:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat2[i][j]);
}
printf("\n");
}
printf("\nResult:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
//6_1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main() {
int mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
int rows, cols;
printf("\nMatrix 2:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat2[i][j]);
}
printf("\n");
}
printf("\nResult:\n");
subtractMatrices(mat1, mat2, result, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
LAB_3:
/*1. Hello from each thread */
#include <stdio.h>
#include <omp.h>
int main() {
#include <stdio.h>
#include <omp.h>
int main() {
// start of OpenMP parallel region
#pragma omp parallel
{
printf("My thread id is %d\n", omp_get_thread_num());
}
// OpenMP parallel region ends here
printf("\nI'm the master thread!\n");
return 0;
}
/*
4. Write C code for the following using omp library:
1. Display a welcome message in a parallel bock and observe how many times it gets
displayed.
*/
#include <stdio.h>
#include <omp.h>
int main() {
int count = 0; // Variable to count the number of times the message is displayed
return 0;
}
/*
5. Write C code for the following using omp library:
Modify the "omp4.c file" to print the thread id of the executing thread, with each disply
message.
*/
#include <stdio.h>
#include <omp.h>
int main() {
int count = 0; // Variable to count the number of times the message is displayed
return 0;
}
/*
6. Write C code for the following using omp library:
Further modify the "omp6.c file" to print the total number of executing threads.
*/
#include <stdio.h>
#include <omp.h>
int main() {
int count = 0; // Variable to count the number of times the message is displayed
return 0;
}
/*
7. Create a parallel block. Compute the sum of numbers from zero to thread_id, and output
the sum to the screen. Run your code and observe the result.
*/
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel
{
// Get the thread ID
int tid = omp_get_thread_num();
int sum = 0;
// Compute the sum of numbers from zero to thread_id
for (int i = 0; i <= tid; i++) {
sum += i;
}
return 0;
}
LAB_5:
/*
Illustrate loop parallelism with the for loop by writing a program to calculate the sum of an
array in parallel using OpenMP. (Use a shared variable for the result and private variables
for loop indices).
*/
#include <stdio.h>
#include <omp.h>
#define ARRAY_SIZE 10
int main() {
int array[ARRAY_SIZE];
int sum = 0,i;
return 0;
}
/*
Implement a program to calculate the square of each element in an array in parallel using
OpenMP. Use shared variables for the input array and private variables for the parallel
for loop indices.
*/
#include <stdio.h>
#include <omp.h>
#define ARRAY_SIZE 10
int main() {
int array[ARRAY_SIZE];
int squares[ARRAY_SIZE];
int i;
return 0;
}
/*
Create a parallel program to calculate the factorial of a number using OpenMP. Use a shared
variable for the result, and private variables for loop indices.
*/
#include <stdio.h>
#include <omp.h>
int main() {
int number = 10,i; // Number for which factorial will be calculated
unsigned long long factorial = 1; // Result variable, initialized to 1
return 0;
}
/*
Write a program to find the minimum element in an array in parallel using OpenMP. Use
shared variables for the array and the minimum value, and private variables for loop indices.
*/
#include <stdio.h>
#include <omp.h>
int main() {
int array[ARRAY_SIZE], i;
int min_value = __INT_MAX__; // Initialize min_value to maximum possible value
return 0;
}
/*
Implement a program to generate the Fibonacci series in parallel using OpenMP. Use shared
variables for the result array and private variables for each thread's local variables.
*/
#include <stdio.h>
#include <omp.h>
#define N 10 // Number of Fibonacci numbers to generate
int main() {
int fib[N];
fib[0] = 0;
fib[1] = 1;
return 0;
}
/*
Write a program to transpose a matrix in parallel using OpenMP. Use shared variables for
the input and output matrices and private variables for loop indices.
*/
#include <stdio.h>
#include <omp.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int transposed[COLS][ROWS];
return 0;
}
LAB_6:
//Critical.c
#include <omp.h>
#include <stdio.h>
int main() {
int arr[ARR_LEN];
int sum = 0;
// Initialize array
for (int i = 0; i < ARR_LEN; ++i) {
arr[i] = i + 1;
}
return 0;
}
//Master.c
/*** Example: omp master directive ***/
#include <stdio.h>
#include <omp.h>
int main() {
int num_threads;
int thread_id;
return 0;
}
LAB_6:
/*
Write a multithreaded program to multiply every element in an array by a constant number.
Create “4” threads and print the thread number too with the results.
*/
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 10
#define NUM_THREADS 4
#define CONSTANT 2
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
return 0;
}
/*
Write a multithreaded program to add two arrays.
Create “4” threads and print the thread number with each computation that is done.
*/
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 10
#define NUM_THREADS 4
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
return 0;
}
/*
Create two functions, one to add two arrays and one to multiply two arrays. Write a program
that will call these two functions in two separate threads. Also display their thread ids
*/
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 5
return NULL;
}
int main() {
pthread_t thread_add, thread_multiply;
int thread_id_add = 1;
int thread_id_multiply = 2;
return 0;
}
/*
Write a multithreaded program to multiply every element in the array by a constant. The
even numbered threads should multiply the elements in the even indices of the array and the
odd numbered threads should multiply the elements present in the odd indices of the array.
(Assume 0 to be the starting even index).
*/
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 10
#define CONSTANT 2
#define NUM_THREADS 2
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
return 0;
}
/*
Write a multithreaded program that will take two numbers as input from the user, num1 and
num2. The program should list out all the numbers between 0 and num2 which are divisble by
num1. (parallelise the for loop)
*/
#include <stdio.h>
#include <omp.h>
int main() {
int num1, num2;
printf("\n");
return 0;
}
/*
Write a multithreaded program that will compare two arrays, and print all the common
elements and their count.
*/
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 10
return NULL;
}
int main() {
pthread_t thread;
int thread_id = 1;
if (count == 0) {
printf("No common elements found.\n");
} else {
printf("Common elements and their count:\n");
for (int i = 0; i < count; i++) {
printf("%d ", common_elements[i]);
}
printf("\nCount: %d\n", count);
}
return 0;
}
/*
Write a multithreaded program that will find the repetition of a number within a n*n order
square matrix. (Let the intilization of the matrix be done within the parallel region by just
one thread.Let the number to be searched be input by the user before entering the parallel
region).
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int matrix[MAX_SIZE][MAX_SIZE];
int n;
int target;
int repetition_count = 0;
// Each thread searches its portion of the matrix for repetitions of the target number
for (int i = tid; i < n; i += n) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == target) {
repetition_count++;
}
}
}
return NULL;
}
int main() {
// Input the size of the square matrix and the target number from the user
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
printf("Enter the number to search for: ");
scanf("%d", &target);
// Create threads
pthread_t threads[pthread_num_threads];
int thread_ids[pthread_num_threads];
for (int i = 0; i < pthread_num_threads; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, search_repetition, (void *)&thread_ids[i]);
}
return 0;
}
/*
Write a multithreaded program to compute the sum of all the elements in a matrix. Let each
thread should determine the partial sum of a column. Let the number of threads working
together be equal to the number of columns.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int matrix[MAX_ROWS][MAX_COLS];
int row_count, col_count;
int total_sum = 0;
return NULL;
}
int main() {
// Input the size of the matrix from the user
printf("Enter the number of rows in the matrix: ");
scanf("%d", &row_count);
printf("Enter the number of columns in the matrix: ");
scanf("%d", &col_count);
return 0;
}
/*
Write a multithreaded program to compute the sum of two n*n square order matrices.
Let the intilization of the matrix be done with in the parallel region, just by only one thread
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_SIZE 10
int matrix1[MAX_SIZE][MAX_SIZE];
int matrix2[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];
int n; // Size of the matrices
pthread_exit(NULL);
}
int main() {
printf("Enter the size of the square matrices: ");
scanf("%d", &n);
// Join threads
for (int i = 0; i < n; i++) {
pthread_join(threads[i], NULL);
}
printf("Matrix 2:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
// Displaying the sum matrix
printf("Sum Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
/*
Write a mulithreaded program to multiply every element in a matrix with a constant number.
The numbers in even rows should be multiplied by even numbered threads, and the number
in odd rows should be multiplied by odd numbered threads. Print the thread number after
each multiplication.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int matrix[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];
int n, constant;
return NULL;
}
int main() {
// Input the size of the square matrix and the constant number from the user
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
printf("Enter the constant number to multiply with: ");
scanf("%d", &constant);
return 0;
}