You are on page 1of 4

1)collatz.

c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void collatz_sequence(int n) {
printf("%d ", n);
while (n != 1) {
if (n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
printf("%d ", n);
}
printf("\n");
}

int main() {
int default_starting_number = 35;

pid_t pid = fork();


if (pid < 0) {
printf("Fork failed\n");
return 1;
} else if (pid == 0) { // Child process
collatz_sequence(default_starting_number);
} else {
wait(NULL);
}

return 0;
}

2)collatz.c with salt


#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <math.h>

int x;

void collatz(int n, int salt) {


x = n;
while (n != 1) {
printf("%d ", n);
if (n % 2 == 0) {
n /= 2;
} else {
n = 3* n + 1;
}
}
printf("%d ", n);
//x += salt;
}
int main() {
int n;
int salt;
double val;

pid_t process;
printf("Enter the value of n: ");
scanf("%d %d %lf", &n, &salt, &val);

double res = log2(val + 1.0);


x = n;

for (int i = 0; i < (int)res; i++) {


process = fork();

if (process == 0) {
collatz(x, salt);
printf("\n Child %d (PID: %d)\n", i, getpid());
break; // Add a break to exit the loop in child processes
} else {
wait(NULL);
printf("\n Parent %d (PID: %d)\n", i, getpid());
x += 3; // Increment x for the next iteration
}
}

return 0;
}

3)fibo.cpp
#include <iostream>
#include <thread>
#include <vector>

using namespace std;

// Function to generate Fibonacci sequence up to n numbers


void generateFibonacci(int n, vector<int>& fibSequence) {
fibSequence.resize(n);
fibSequence[0] = 0;
if (n > 1) {
fibSequence[1] = 1;
for (int i = 2; i < n; ++i) {
fibSequence[i] = fibSequence[i - 1] + fibSequence[i - 2];
}
}
}

int main() {
int numFibonacci;
cout << "Enter the number of Fibonacci numbers to generate: ";
cin >> numFibonacci;

vector<int> fibSequence(numFibonacci);

thread fibThread(generateFibonacci, numFibonacci, ref(fibSequence));


fibThread.join();

cout << "Fibonacci sequence:";


for (int num : fibSequence) {
cout << " " << num;
}
cout << endl;

return 0;
}

4)prime.cpp
#include <iostream>
#include <thread>
#include <vector>

bool isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

void printPrimes(int maxNumber) {


std::cout << "Prime numbers up to " << maxNumber << ":\n";
for (int i = 2; i <= maxNumber; i++) {
if (isPrime(i)) std::cout << i << " ";
}
}

int main() {
int maxNumber;
std::cout << "Enter a number: ";
std::cin >> maxNumber;

std::thread primeThread(printPrimes, maxNumber);


primeThread.join();

return 0;
}

5)virtualaddress.cpp
#include <stdio.h>
#include <stdlib.h>

#define PAGE_SIZE 4096 // 4 KB page size

int main() {
unsigned int virtual_address;

printf("Enter the virtual address: ");


scanf("%u", &virtual_address);

// Calculate page number and offset


unsigned int page_number = virtual_address / PAGE_SIZE;
unsigned int offset = virtual_address % PAGE_SIZE;
// Output the result
printf("The address %u contains:\n", virtual_address);
printf("Page number = %u\n", page_number);
printf("Offset = %u\n", offset);

return 0;
}

6)avgmax.cpp
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <algorithm>
#include <limits>

using namespace std;

void calculateAverage(const std::vector<int>& numbers, double& average) {


average = accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
}

void calculateMaximum(const std::vector<int>& numbers, int& maximum) {


maximum = *max_element(numbers.begin(), numbers.end());
}

void calculateMinimum(const std::vector<int>& numbers, int& minimum) {


minimum = *min_element(numbers.begin(), numbers.end());
}

int main() {
std::vector<int> numbers = {90, 81, 78, 95, 79, 72, 85};

double average;
int maximum;
int minimum;

thread avgThread(calculateAverage, cref(numbers), ref(average));


thread maxThread(calculateMaximum, cref(numbers), ref(maximum));
thread minThread(calculateMinimum, cref(numbers), ref(minimum));

avgThread.join();
maxThread.join();
minThread.join();

// Output the calculated statistical values


cout << "The average value is: " << average << endl;
cout << "The maximum value is: " << maximum << endl;
cout << "The minimum value is: " << minimum << endl;

return 0;
}

You might also like