-
-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into baklava-opa
- Loading branch information
Showing
3 changed files
with
108 additions
and
8 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
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,49 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
bool linear_search(int* arr, int size, int target) { | ||
for (int i = 0; i < size; i++) { | ||
if (arr[i] == target) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int* parse_array(char* input, int* size) { | ||
int* arr = NULL; | ||
*size = 0; | ||
char* token = strtok(input, ", "); | ||
while (token != NULL) { | ||
arr = realloc(arr, (*size + 1) * sizeof(int)); | ||
arr[*size] = atoi(token); | ||
(*size)++; | ||
token = strtok(NULL, ", "); | ||
} | ||
return arr; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc != 3) { | ||
printf("Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")\n"); | ||
return 1; | ||
} | ||
|
||
int size; | ||
int* arr = parse_array(argv[1], &size); | ||
int target = atoi(argv[2]); | ||
|
||
if (size == 0) { | ||
printf("Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")\n"); | ||
free(arr); | ||
return 1; | ||
} | ||
|
||
bool result = linear_search(arr, size, target); | ||
printf("%s\n", result ? "true" : "false"); | ||
|
||
free(arr); | ||
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,56 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define MAX_MATRIX_SIZE 100 | ||
|
||
void transpose_matrix(int *matrix, int rows, int cols, int *transposed) { | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
transposed[j * rows + i] = matrix[i * cols + j]; | ||
} | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc != 4) { | ||
printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); | ||
return 1; | ||
} | ||
|
||
int cols = atoi(argv[1]); | ||
int rows = atoi(argv[2]); | ||
char *input = argv[3]; | ||
|
||
if (cols <= 0 || rows <= 0 || strlen(input) == 0) { | ||
printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); | ||
return 1; | ||
} | ||
|
||
int matrix[MAX_MATRIX_SIZE]; | ||
int transposed[MAX_MATRIX_SIZE]; | ||
int count = 0; | ||
char *token = strtok(input, ", "); | ||
|
||
while (token != NULL && count < rows * cols) { | ||
matrix[count++] = atoi(token); | ||
token = strtok(NULL, ", "); | ||
} | ||
|
||
if (count != rows * cols) { | ||
printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); | ||
return 1; | ||
} | ||
|
||
transpose_matrix(matrix, rows, cols, transposed); | ||
|
||
for (int i = 0; i < cols * rows; i++) { | ||
printf("%d", transposed[i]); | ||
if (i < cols * rows - 1) { | ||
printf(", "); | ||
} | ||
} | ||
printf("\n"); | ||
|
||
return 0; | ||
} |