Skip to content

Commit

Permalink
Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanYahya101 committed Sep 6, 2024
1 parent 0520598 commit 3e3b3d8
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lab_02/Question_01/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
main: main.o functions.o
gcc main.o functions.o -o main

main.o: main.c
gcc -c main.c

functions.o: functions.c
gcc -c functions.c

clean:
rm *.o main
89 changes: 89 additions & 0 deletions Lab_02/Question_01/functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdbool.h>
#include <stdio.h>
#include "header.h"

void sort(int array[], bool order, int size)
{
int n = size;
if (order == true) // sort in ascending order
{
for (int i = 0; i < n - 1; i++)
{
int min = i;

for (int j = i + 1; j < n; j++)
{
if (array[j] < array[min])
{
min = j;
}
}

int temp = array[min];
array[min] = array[i];
array[i] = temp;
}
printf("Sorted in ascending order\n");
}
else
{
// sort in descending order
for (int i = 0; i < n - 1; i++)
{
int min = i;

for (int j = i + 1; j < n; j++)
{
if (array[j] > array[min])
{
min = j;
}
}

int temp = array[min];
array[min] = array[i];
array[i] = temp;
}
printf("Sorted in decending order\n");
}
}

void findHighest(int array[], int nth, int size)
{
sort(array, false, size); // descending
int highest = array[0];
int count = 1;

for (int i = 1; i < size; i++)
{
if (array[i] != highest)
{
highest = array[i];
count++;
}

if (count == nth)
{
break;
}
}

if (count == nth)
{
printf("%d highest number is: %d\n", nth, highest);
}
else
{
printf("There are less than %d distinct numbers in the array.\n", nth);
}
}

void print(int print[], int size)
{
// print all elements in an array
printf("The array values are as under:\n");
for (int i = 0; i < size; i++)
{
printf("%d\n", print[i]);
}
}
5 changes: 5 additions & 0 deletions Lab_02/Question_01/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdbool.h>

void sort(int array[], bool order, int size);
void findHighest(int array[], int nth, int size);
void print(int print[], int size);
43 changes: 43 additions & 0 deletions Lab_02/Question_01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int main(int argc, char *argv[])
{
if (argc > 1)
{
const int arr_size = argc - 3;
if (arr_size < 1)
{
printf("Error: Please enter a valid array");
exit(1);
}

int arr[arr_size];

for (int i = 0; i < arr_size; i++)
{
arr[i] = atoi(argv[i + 1]);
}

int flag = atoi(argv[argc - 2]);
if (flag != 0 && flag != 1)
{
printf("Enter valid flag\n");
}
int nth = atoi(argv[argc - 1]);
if (nth < 1)
{
printf("error enter valid nth val\n");
exit(0);
}
sort(arr, flag, arr_size);
print(arr, arr_size);
findHighest(arr, nth, arr_size);
}
else
{
printf("No arguments provided.\n");
}
return 0;
}
5 changes: 5 additions & 0 deletions Lab_02/Question_02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
main:
gcc main.c -o main

clean:
rm *.o main
97 changes: 97 additions & 0 deletions Lab_02/Question_02/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Error, Please enter valid arguments");
exit(1);
}

int pid = fork();

if (pid < 0)
{
printf("Error, Fork failed");
exit(1);
}

if (pid == 0)
{
int inputFile = open(argv[1], 0); // read
int outputFile = open(argv[2], 1); // write

if (inputFile < 0 || outputFile < 0)
{
printf("Error: Couldn't oopen file");
exit(1);
}

char char_data[1024];
int bytesRead;
while ((bytesRead = read(inputFile, char_data, 1026)) > 0)
{
for (int i = 0; i < bytesRead; i++)
{
if (isdigit(char_data[i]))
{
if (write(outputFile, &char_data[i], 1) < 0)
{
printf("Error: File not open");
close(inputFile);
close(outputFile);
exit(1);
}
}
}
}

close(inputFile);
close(outputFile);
exit(0);
}
else
{
wait(NULL);

int outputFile = open(argv[2], O_RDONLY);
if (outputFile < 0)
{
printf("Error opening file");
exit(1);
}

int sum = 0, count = 0;
char chars[1024];
int bytesRead;
while ((bytesRead = read(outputFile, chars, 1024)) > 0)
{
for (int i = 0; i < bytesRead; i++)
{
if (isdigit(chars[i]))
{
sum += chars[i] - '0';
count++;
}
}
}

close(outputFile);

if (count > 0)
{
int average = sum / count;
printf("Sum: %d\n", sum);
printf("Average: %d\n", average);
}
else
{
printf("No numbers found in the output file.\n");
}
}

return 0;
}

0 comments on commit 3e3b3d8

Please sign in to comment.