Skip to content

Instantly share code, notes, and snippets.

@McSinyx
Last active April 26, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McSinyx/8353702462e849288bf4abb256ea4d36 to your computer and use it in GitHub Desktop.
Save McSinyx/8353702462e849288bf4abb256ea4d36 to your computer and use it in GitHub Desktop.
ICT1.2: Labwork 6

After studying about pointers, labworks start to get interesting. So I decided to upload my solutions to compare with others. Hope some may find my implementations either helpful or amusing.

For exercise 4, please head to this commit. I felt that it was too long to be put together with 5-minute attempts.

Don't mind the file name tho, I put it that way so that the file will get alphabetically sorted before the labworks.

Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

#include <stdio.h>
int main()
{
int n, tmp, min, max;
/* No I don't like static arrays. */
scanf("%d", &n);
for (scanf("%d", &tmp), min = max = tmp; --n; scanf("%d", &tmp)) {
if (tmp < min)
min = tmp;
if (tmp > max)
max = tmp;
}
printf("%d %d\n", min, max);
return 0;
}
#include <stdio.h>
int main()
{
unsigned n;
scanf("%u", &n);
unsigned d = n / 2;
unsigned even = d * (d + 1);
printf("%u %u\n", even, n * (n + 1) / 2 - even);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void strswap(char *this, char *that, size_t n)
{
while (n--) {
this[n] ^= that[n];
that[n] ^= this[n];
this[n] ^= that[n];
}
}
void bsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
char unsorted = 0;
size_t length = (nmemb - 1) * size;
do {
unsorted = 0;
for (size_t i = 0; i < length; i += size)
if (compar(base + i, base + i + size) > 0) {
strswap(base + i, base + i + size, size);
unsorted = 1;
}
} while (unsorted);
}
void isort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t length = nmemb * size;
for (size_t i = size; i < length; i += size)
for (size_t j = i; j && compar(base + j, base + j - size) < 0;
j -= size)
strswap(base + j, base + j - size, size);
}
void ssort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t length = nmemb * size;
for (size_t i = 0; i < length - size; i += size)
for (size_t j = i; j < length; j += size)
if (compar(base + i, base + j) > 0)
strswap(base + i, base + j, size);
}
int cmp(const void *x, const void *y)
{
return *(int *) x - *(int *) y;
}
int main()
{
size_t n;
scanf("%zu", &n);
int a[n];
for (int i = 0; i < n; i++)
scanf("%d", a + i);
//bsort(a, n, sizeof(int), cmp);
//isort(a, n, sizeof(int), cmp);
//ssort(a, n, sizeof(int), cmp);
qsort(a, n, sizeof(int), cmp);
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
putchar(10);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
size_t n;
scanf("%zu ", &n);
char *s = malloc(n + 1);
fgets(s, n + 1, stdin);
while (n--) putchar(s[n]);
putchar(10);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
size_t n;
scanf("%zu ", &n);
char *s = malloc(n + 1);
fgets(s, n + 1, stdin);
size_t i, count = 0;
for (i = 0; i < n; i++)
if (s[i] == 97)
count++;
printf("%zu\n", count);
for (i = 0; i < n; i++)
if (s[i] == 97)
printf("%zu ", i);
putchar(10);
return 0;
}
#include <stdio.h>
int main()
{
size_t n;
char x, c;
scanf("%zu %c", &n, &x);
while (n--)
if ((c = getchar()) != x)
putchar(c);
putchar(10);
return 0;
}
Display the source blob
Display the rendered blob
Raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment