Add everything, Yes.

This commit is contained in:
juan 2021-12-10 23:31:54 +08:00
parent 4473d19940
commit 4df6ada921
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n;
scanf("%d", &n);
int *arr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n; i++) {
arr[i] = arr[i] * -10;
}
for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
printf("%d\n", arr[n - 1]);
free(arr);
return 0;
}

View file

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int row, col;
scanf("%d%d", &row, &col);
// Initialize the 2d array
int **arr = malloc(sizeof(int *) * row);
for (int i = 0; i < row; i++) {
arr[i] = malloc(sizeof(int) * col);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
scanf("%d", &arr[i][j]);
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col - 1; j++) {
printf("%d ", arr[i][j] * -10);
}
printf("%d\n", arr[i][col - 1] * -10);
}
// Free the 2d array
for (int i = 0; i < row; i++) {
free(arr[i]);
}
free(arr);
return 0;
}

View file

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100001
void sort(char **arr, int n);
void swap(char **arr1, char **arr2);
int main(void) {
int n;
scanf("%d", &n);
getchar();
// 2d array
char **str = malloc(sizeof(char *) * n);
// Buffer array
char *buffer = malloc(sizeof(char) * MAX);
for (int i = 0; i < n; i++) {
fgets(buffer, MAX, stdin);
str[i] = malloc(strlen(buffer) + 1);
strcpy(str[i], buffer);
}
sort(str, n);
for (int i = 0; i < n; i++) {
printf("%s", str[i]);
}
// Free up memory
free(buffer);
for (int i = 0; i < n; i++) {
free(str[i]);
}
free(str);
return 0;
}
void sort(char **arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (strcmp(arr[j], arr[j + 1]) > 0) {
// Why I can't use swap() here?
char *tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}

View file

@ -0,0 +1 @@
2-3-ArrayEX.c