In this post, all the solutions of C are given with the difficulty level medium.
For easy-level solutions, click here.
All these solutions are successfully compiled and passed all the tests.
Printing pattern using loops
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int len = n*2 - 1;
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
int min = i < j ? i : j;
min = min < len-i ? min : len-i-1;
min = min < len-j-1 ? min : len-j-1;
printf("%d ", n-min);
}
printf("n");
}
return 0;
}
1D Array
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, sum=0;
scanf("%d",&n);
int *a = malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("%i",sum);
free(a);
return 0;
}
Array Reversal
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", (arr + i));
}
/* Write the logic to reverse the array. */
int temp;
for (int i; i<num/2; i++) {
temp = *(arr+num-i-1);
*(arr+num-i-1) = *(arr+i);
*(arr+i) = temp;
}
for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}
Digit frequency
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int* nums = (int*) malloc(10 * sizeof(int));
char c;
for(int i = 0; i < 10; i++)
*(nums+i) = 0;
while(scanf("%c", &c) == 1)
if(c >= '0' && c <= '9')
(*(nums+(c-'0')))++;
for(int i = 0; i < 10; i++)
printf("%d ", *(nums+i));
return EXIT_SUCCESS;
}
Printing Tokens
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^n]", s);
s = realloc(s, strlen(s) + 1);
//Write your logic to print the tokens of the sentence here.
for (char *c=s; *c != NULL; c++) {
if (*c ==' ') {
*c='n';
}
}
printf("%s",s);
return 0;
}
Small Triangles, Large Triangles
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
double area(triangle t) {
double p = (t.a + t.b + t.c) / 2.0;
return (p * (p - t.a) * (p - t.b) * (p - t.c));
// You can also write
// return sqrt(p * (p - t.a) * (p - t.b) * (p - t.c));
// There will be no issues at all, cause even without sqrt function you'll get different values
}
void swap(triangle *t1, triangle *t2) {
triangle temp = *t1;
*t1 = *t2;
*t2 = temp;
}
void sort_by_area(triangle* tr, int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (area(tr[j]) > area(tr[j + 1])) {
swap(&tr[j], &tr[j + 1]);
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %dn", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}
The rest of the codes will also be uploaded soon.