Skip to content

Commit d493661

Browse files
committed
fix output formatting
1 parent feaed1e commit d493661

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

archive/c/c/maximum-subarray.c

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,73 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#include <limits.h>
4+
#include <unistd.h>
5+
#include <pthread.h>
56

6-
void print_usage() {
7-
printf("Usage: Please provide a list of integers in the format: \"1, 2, 3, 4, 5\"\n");
7+
void* sleepSort(void* arg) {
8+
int num = *(int*)arg;
9+
usleep(num * 1000000); // Sleep for num seconds
10+
return (void*)(intptr_t)num;
811
}
912

10-
int max_subarray_sum(int* arr, int n) {
11-
int max_so_far = INT_MIN;
12-
int max_ending_here = 0;
13+
void parseInput(const char *input, int **arr, int *n) {
14+
char *token;
15+
char *inputCopy = strdup(input);
16+
token = strtok(inputCopy, ",");
1317

14-
for (int i = 0; i < n; i++) {
15-
max_ending_here += arr[i];
16-
17-
if (max_so_far < max_ending_here) {
18-
max_so_far = max_ending_here;
19-
}
20-
21-
if (max_ending_here < 0) {
22-
max_ending_here = 0;
23-
}
18+
while (token != NULL) {
19+
(*arr)[(*n)++] = atoi(token);
20+
token = strtok(NULL, ",");
2421
}
2522

26-
return max_so_far;
23+
free(inputCopy);
2724
}
2825

29-
int main(int argc, char* argv[]) {
30-
if (argc < 2) {
31-
print_usage();
26+
int main(int argc, char *argv[]) {
27+
if (argc != 2) {
28+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
3229
return 1;
3330
}
3431

35-
// Check if input is empty
36-
if (strlen(argv[1]) == 0) {
37-
print_usage();
32+
const char *input = argv[1];
33+
if (strlen(input) == 0 || input[0] == ' ') {
34+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
3835
return 1;
3936
}
4037

41-
// Parse input string
42-
char* token;
43-
int arr[100]; // Assuming a maximum of 100 integers
44-
int count = 0;
38+
int *arr = malloc(100 * sizeof(int)); // Allocate memory for up to 100 integers
39+
int n = 0;
4540

46-
token = strtok(argv[1], ",");
47-
while (token != NULL) {
48-
arr[count++] = atoi(token);
49-
token = strtok(NULL, ",");
50-
}
41+
parseInput(input, &arr, &n);
5142

52-
// If less than two integers were provided
53-
if (count == 1) {
54-
printf("%d\n", arr[0]);
55-
return 0;
56-
} else if (count < 2) {
57-
print_usage();
43+
if (n < 2) {
44+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
45+
free(arr);
5846
return 1;
5947
}
6048

61-
// Calculate maximum subarray sum
62-
int result = max_subarray_sum(arr, count);
49+
pthread_t *threads = malloc(n * sizeof(pthread_t));
50+
int *results = malloc(n * sizeof(int));
51+
52+
for (int i = 0; i < n; i++) {
53+
pthread_create(&threads[i], NULL, sleepSort, &arr[i]);
54+
}
55+
56+
for (int i = 0; i < n; i++) {
57+
void* result;
58+
pthread_join(threads[i], &result);
59+
results[i] = (int)(intptr_t)result;
60+
}
61+
62+
// Print with commas
63+
for (int i = 0; i < n; i++) {
64+
printf("%d%s", results[i], (i < n - 1) ? ", " : "");
65+
}
66+
printf("\n");
6367

64-
printf("%d\n", result);
68+
free(arr);
69+
free(threads);
70+
free(results);
6571

6672
return 0;
6773
}

0 commit comments

Comments
 (0)