-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0520598
commit 3e3b3d8
Showing
6 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
main: | ||
gcc main.c -o main | ||
|
||
clean: | ||
rm *.o main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |