Skip to content

Commit

Permalink
Merge branch 'main' into baklava-opa
Browse files Browse the repository at this point in the history
  • Loading branch information
rzuckerm authored Jan 22, 2025
2 parents 5eb143b + e1c0d3b commit 737258d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
11 changes: 3 additions & 8 deletions archive/c/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Welcome to Sample Programs in C! To find documentation related to the C code in this repo, look [here.](https://sampleprograms.io/languages/c)

## Sample Programs List - 35/37 :smile:
## Sample Programs List - 37/37 :partying_face:

In this section, we feature a list of completed and missing programs in C. See above for the current amount of completed programs in C. If you see a program that is missing and would like to add it, please submit an issue, so we can assign it to you.

Expand All @@ -28,6 +28,7 @@ Below, you'll find a list of completed code snippets in C. Code snippets precede
- :warning: [Insertion Sort in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+insertion+sort+c) [[Requirements](https://sampleprograms.io/projects/insertion-sort)]
- :warning: [Job Sequencing in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+job+sequencing+c) [[Requirements](https://sampleprograms.io/projects/job-sequencing)]
- :warning: [Josephus Problem in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+josephus+problem+c) [[Requirements](https://sampleprograms.io/projects/josephus-problem)]
- :warning: [Linear Search in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+linear+search+c) [[Requirements](https://sampleprograms.io/projects/linear-search)]
- :warning: [Longest Common Subsequence in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+longest+common+subsequence+c) [[Requirements](https://sampleprograms.io/projects/longest-common-subsequence)]
- :warning: [Longest Palindromic Substring in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+longest+palindromic+substring+c) [[Requirements](https://sampleprograms.io/projects/longest-palindromic-substring)]
- :warning: [Longest Word in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+longest+word+c) [[Requirements](https://sampleprograms.io/projects/longest-word)]
Expand All @@ -45,13 +46,7 @@ Below, you'll find a list of completed code snippets in C. Code snippets precede
- :warning: [Rot13 in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+rot13+c) [[Requirements](https://sampleprograms.io/projects/rot13)]
- :warning: [Selection Sort in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+selection+sort+c) [[Requirements](https://sampleprograms.io/projects/selection-sort)]
- :warning: [Sleep Sort in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sleep+sort+c) [[Requirements](https://sampleprograms.io/projects/sleep-sort)]

### Missing Programs

The following list contains all of the approved programs that are not currently implemented in C. Click on the name of the project to easily open an issue in GitHub. Alternatively, click requirements to check out the description of the project.

- :x: [Linear Search](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,linear+search&template=code-snippet-request.md&title=Add+Linear+Search+in+C) [[Requirements](https://sampleprograms.io/projects/linear-search)]
- :x: [Transpose Matrix](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,transpose+matrix&template=code-snippet-request.md&title=Add+Transpose+Matrix+in+C) [[Requirements](https://sampleprograms.io/projects/transpose-matrix)]
- :warning: [Transpose Matrix in C](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+transpose+matrix+c) [[Requirements](https://sampleprograms.io/projects/transpose-matrix)]

## Testing

Expand Down
49 changes: 49 additions & 0 deletions archive/c/c/linear-search.c
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;
}
56 changes: 56 additions & 0 deletions archive/c/c/transpose-matrix.c
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;
}

0 comments on commit 737258d

Please sign in to comment.