0% found this document useful (0 votes)
8 views10 pages

Note 0

This document contains code snippets related to various C programming concepts like input/output, arrays, sorting, and more. Some key snippets include: - Code to take integer, float, double, and character input from the user. - Functions to find the minimum and maximum value from a user-entered array until receiving 0. - Code implementing insertion sort on an array. - A function to check if an array is symmetric by comparing the first and last elements. - Functions for ascending and descending sort of arrays.

Uploaded by

Hoàng Lâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Note 0

This document contains code snippets related to various C programming concepts like input/output, arrays, sorting, and more. Some key snippets include: - Code to take integer, float, double, and character input from the user. - Functions to find the minimum and maximum value from a user-entered array until receiving 0. - Code implementing insertion sort on an array. - A function to check if an array is symmetric by comparing the first and last elements. - Functions for ascending and descending sort of arrays.

Uploaded by

Hoàng Lâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Integer: scan(“%d”, &_); { scanf("%d", &distance); return 0;

Float: scan(“%f”, &_); char *tmp_p; distance *= 1000; }


Double: scan(“%lf”, &_); char tmp_str[N]; int fee = 0; Insertion sort
Char: scan(“%c”, &_); fgets(tmp_str, N, stdin); if (distance > 0) { int main()
*num = strtol(tmp_str, fee += 15000; {
TAKE USER INPUT &tmp_p, 10); distance -= 2000; int arr[MAX_SIZE] = {10,
#include <stdio.h> } } -1, 0, 4, 2, 100, 15, 20, 24, -5};
#include <stdlib.h> int counter = 0; int len = 10;
#include <string.h> int numLen(int num) while (distance > 0 &&
{ counter <= 112) {
#define MAX_SIZE 100 int div = 1; fee += 2000; for (int i = 0; i < len; i++) {
int counter = 0; distance -= 250; printf("%d ", arr[i]);
void fgeti(int *num, int N); while (num / div != 0) counter++; }
{ counter++; div *= 10; } }
int main() return counter; while (distance > 0) { sort(arr, len,
{ } fee += 5000; descending);
char name[50]; Find Min, Max from user until distance -= 1000;
int id, score; receiving 0 } printf("\n\nAfter: \n");
printf("Name: "); int main() { printf("Fee: %d vnd", for (int i = 0; i < len; i++) {
fgets(name, MAX_SIZE, printf("Enter Num until 0: fee); printf("%d ", arr[i]);
stdin); "); } }
strtok(name, "\n"); int num = 1; Find all prime components
printf("ID: "); fgeti(&num, MAX_SIZE); int is_divisible_by(int n, int return 0;
fgeti(&id, MAX_SIZE); int min = num; num) { }
printf("Score: "); int max = num; // If n is divisible by num,
fgeti(&score, MAX_SIZE); while(num != 0) { divide n by num. int ascending(int num1, int
printf("\n"); if (num > max) { max if (n % num == 0) { num2) {
= num; } n /= num; return num1 - num2;
printf("%s\n", name); else if (num < min) printf("%d ", num); }
printf("%d\n", id); { min = num; } }
printf("%d\n", score); fgeti(&num, } int descending(int num1, int
MAX_SIZE); num2) {
return 0; } bool is_prime(int num) { return num2 - num1;
} printf("Min: %d\n", min); // Check whether num is }
printf("Max: %d\n", a prime or not.
void fgeti(int *num, int N) max); for(int i = 2; i <= num / 2; void sort(int *arr, int len, int
{ return 0; ++i) (* compare)(int , int )) {
char *tmp_p; } { for (int i = 0; i < len - 1;
char tmp_str[N]; Find Prime with upper bound if(num % i == 0) i++) {
fgets(tmp_str, N, stdin); int main() { { int j = i;
*num = strtol(tmp_str, int upper = 20; return false; while (j >= 0 &&
&tmp_p, 10); printf("Enter upper } compare(arr[j], arr[j+1]) > 0) {
} bound: "); } arr[j] ^= arr[j +
REVERSE INT fgeti(&upper, return true; 1];
123 => 3 2 1 MAX_SIZE); } arr[j + 1] ^=
int main() if (upper < 2) { arr[j];
{ printf("Invalid"); int main() { arr[j] ^= arr[j +
int number = 29294; } //Prompt user 1];
printf("Enter number: "); for (int i = 2; i <= upper; int input; j--;
fgeti(&number, i++) { printf("Enter a positive }
MAX_SIZE); if (i == 2 || i == 3 || number:\n"); }
int len = i == 5 || i == 7) { printf("%d ", scanf("%d",&input); }
numLen(number); i); } Check for symmetry
int div = 10; if (i % 2 == 0 || i % 3 // Calculate and display int main() {
for (int i = 0; i < len; i++) == 0 || i % 5 == 0 || i % 7 == 0) result int arr[MAX_SIZE] =
{ { continue; } printf("Prime {1,2,3,3,2,1};
printf("%d ", else { printf("%d ", components: "); int arrLen = 6;
number % div); i); } for (int i=2; i<=input; i++) int *pstart = arr;
number /= 10; } { int *pend = arr + arrLen -
} } if (is_prime(i)) { 1;
printf("\n"); Taxi fee: first 2Km, next 250m, while(pstart < pend) {
return 0; over 30km. is_divisible_by(input, i); if (*pstart != *pend)
} int main() { } {printf("Asymmetric!\n");
int distance = 30; } return 1;}
void fgeti(int *num, int N) printf("Enter Km: "); pstart++;
pend--; count_decr++; }
} input_array[even_list[i]] = j++; }
printf("Symmetric!\n"); even_array[i]; } return 1;
return 0; } if (max_decr < }
} for (int i = 0; i < odd_size; count_decr) { else {
Sort in array i++){ max_decr = for (int i = 0; i <
253486792 count_decr; input_size-1; i++) {
-> 8 3 5 6 4 2 7 9 2 input_array[odd_list[i]] = index_decr = i; if (input_array[i] <
int input_array[SIZE]; odd_array[i]; } input_array[i+1]){
int input_size; } } return 0;
printf("Enter array size: } }
"); printf("result = "); }
scanf("%d", &input_size); for (int i = 0; i < if (max_incr >= max_decr) { return -1;
for (int i = 0; i < input_size; i++) { printf("Max increasing }
input_size; i++) { printf("%d ", array:\n"); Separate pos ans neg
printf("Enter input_array[i]); for (int i = 0; i < max_incr; 2 -3 4 6 -7 9 8 -2
number: "); } i++) { -> 2 4 6 9 8 -3 -7 -2
int num; printf("%d ", int input_array[SIZE];
scanf("%d", &num); printf(""); input_array[index_incr + i]); int input_size;
input_array[i] = Largest sub-array } printf("Enter array size: ");
num; array = 2 5 3 4 8 9 7 6 10 } scanf("%d", &input_size);
} result = 3 4 8 9 (increasing) for (int i = 0; i < input_size; i++)
int input_array[SIZE]; if (max_decr >= max_incr) { {
int even_array[SIZE]; int input_size; printf("\n"); printf("Enter number: ");
int even_size = 0; printf("Enter array size: "); printf("Max decreasing int num;
int even_list[SIZE]; scanf("%d", &input_size); array:\n"); scanf("%d", &num);
int odd_array[SIZE]; for (int i = 0; i < input_size; i++) for (int i = 0; i < max_decr; input_array[i] = num;
int odd_size = 0; { i++) { }
int odd_list[SIZE]; printf("Enter number: "); printf("%d ",
int num; input_array[index_decr + i]); int pos_idx = 0;
int even_count = 0; scanf("%d", &num); } for (int i = 0; i < input_size; i++)
int odd_count = 0; input_array[i] = num; } {
for (int i = 0; i < } Sorted? if (input_array[i] > 0) {
input_size; i++) { int input_array[SIZE] = int save =
if (input_array[i] % 2 int max_incr = 0, index_incr = {8,8,7,6,5,4,2,2,2,1,1,0}; input_array[i];
== 0) { 0, max_decr= 0, index_decr = int input_size = 12; input_array[i] =
0; printf("Enter array size: "); input_array[pos_idx];
even_array[even_size] = for (int i = 0; i < input_size-1; scanf("%d", &input_size); input_array[pos_idx]
input_array[i]; i++) { for (int i = 0; i < input_size; i++) = save;
even_size++; // mode = 0: increasing; { pos_idx++;
mode = 1: decreasing printf("Enter number: "); }
even_list[even_count] = i; int mode = 0; int num; }
even_count++; if (input_array[i] > scanf("%d", &num);
} input_array[i + 1]) { input_array[i] = num; for (int i = 0; i < input_size; i++)
else { mode = 1; } {
} printf("%d ",
odd_array[odd_size] = int j = i; // mode = 0: increasing; input_array[i]);
input_array[i]; if (mode == 0) { mode = 1: decreasing }
odd_size++; int count_incr = 1; int idx = 0; Positive upfront
while (input_array[j] int mode = 0; int input_array[SIZE];
odd_list[odd_count] = i; < input_array[j + 1]) { while (input_array[idx] == int input_size;
odd_count++; count_incr++; input_array[idx+1]) { printf("Enter array size: ");
} j++; idx++; scanf("%d", &input_size);
} } if (input_array[idx] > for (int i = 0; i < input_size; i++)
if (max_incr < input_array[idx+1]) { {
count_incr) { mode = 1; printf("Enter number: ");
insertion_sort(even_array, max_incr = } int num;
even_size, true); count_incr; } scanf("%d", &num);
insertion_sort(odd_array, index_incr = i; input_array[i] = num;
odd_size, false); } if (mode == 0) { }
} else { for (int i = 0; i <
for (int i = 0; i < even_size; int count_decr = 1; input_size-1; i++) { int pos_idx = 0;
i++){ while if (input_array[i] > for (int i = 0; i < input_size; i++)
(input_array[j] > input_array[j input_array[i+1]){ {
+ 1]) { return 0; if (input_array[i] > 0) {
int save = input_arr[i] = n; } printf("VALID");
input_array[i]; } i--; }
input_array[i] = int target = 22; } else {
input_array[pos_idx]; printf("Enter target: "); i++; printf("INVALID");
input_array[pos_idx] scanf("%i", &target); } }
= save; for (int i = 0; i < len; i++) { Fibbonacci and Factorial
pos_idx++; for (int j = i + 1; j < len; if (str[0] == ' ') { recursion
} j++){ int j = 0; #include <stdio.h>
} if (input_arr[i] + do {
Longest substring input_arr[j] == target) { char tmp = str[j]; int factorial(int n) {
- "abcabcbb", the answer printf("[%i, %i]", str[j] = str[j + 1];
//base case
is "abc", length = 3 i, j); str[j + 1] = tmp;
if(n == 0) {
- "bbbbb", the answer is "b", } j++;
return 1;
length = 1 } } while (str[j - 1] != '\0');
} else {
char input[SIZE] = "helloab"; } }
return n *
printf("Enter string: "); Sort String with cpp
i -= 2; factorial(n-1);
scanf("%s", input); # include <stdio.h>
#include <iostream> if (str[i] == ' ') { }
char substring[SIZE]; #include <string> char tmp = str[i]; }
char max_substring[SIZE]; #include <algorithm> str[i] = str[i + 1];
int length = 0; using namespace std; str[i + 1] = tmp; int fibbonacci(int n) {
int max_length = 0; } if(n == 0){
int idx = 0; string name[S] = {"one", "two", return 0;
while (input[idx] != '\0') { "three"}; printf("%s", str); } else if(n == 1) {
for (int j = 0; j < length; int n; Validity of brackets return 1;
j++) { printf("Enter number of char str[L]; } else {
if (input[idx] == string: "); printf("Enter string: "); return
substring[j]) { std::cin >> n; fgets(str, L, stdin); (fibbonacci(n-1) +
length = 0; for (int i = 0; i < n; i++) { char stack[S] = {}; fibbonacci(n-2));
break; printf("Enter char open[3] = {'(', '[', '{'}; }
} string %i: ", i); char close[3] = {')', ']', '}'}; }
} std::cin >> name[i]; int idx = 0;
length++; } int main() {
substring[length - 1] = int run = 1; int n = 5;
input[idx]; int N = int valid = 1;
int i;
if (length > max_length) { sizeof(name)/sizeof(name[0]); int i = 0;
max_length = //Get the array size while (str[i] != '\0' && run ==
printf("Factorial
length; 1) {
for (int n = 0; n < sort(name,name+N); int add = 1;
of %d: %d\n" , n ,
length; n++) { //Use the start and end like for (int j = 0; j < 3 && factorial(n));
this idx > 0; j++) { printf("Fibbonacci of %d:
max_substring[n] = if (str[i] == close[j]) { " , n);
substring[n]; for(int i = 0; i < N; i++){ if (stack[idx - 1]
} cout << name[i] << == open[j]) { for(i = 0;i<n;i++) {
} endl; stack[idx - printf("%d
idx++; } 1] = 0; ",fibbonacci(i));
} Remove blank spaces idx--; }
for (int i = 0; i < max_length; char str[] = " hello add = 0; }
i++) { everyone! "; }
printf("%c", else {
max_substring[i]); printf("Enter String: "); valid = 0;
} cin >> str; run = 0;
printf("\n%i\n", max_length); int i = 0; }
Add up to target number while (str[i] != '\0') { break;
[2, 7, 11, 15], target = 9 if (str[i - 1] == ' ' && str[i] }
return [0,1] == ' ') { }
int len = 4; int j = i; if (add == 1) {
int input_arr[SIZE] = while (str[j - 1] != stack[idx] = str[i];
{2,7,11,15}; '\0') { idx++;
printf("Enter array size: "); char tmp = }
scanf("%i", &len); str[j]; i++;
for (int i = 0; i < len ; i++) { str[j] = str[j + }
printf("Index %i: ", i); 1];
int n; str[j + 1] = tmp; if (valid == 1 && stack[0] == 0)
scanf("%i", &n); j++; {
Lab7 max_len = len2; void ex03() {
#include <stdio.h> } char str1[MAX_SIZE] = "12+58i";
#include <string.h> char str2[MAX_SIZE] = "24+0i";
#include <stdlib.h> int this_remainder = 0;
int next_remainder = 0; printf("Enter first complex number:
#define MAX_SIZE 100 for (int i = 0; i < max_len; i++) { ");
// Init a,b fgets(str1, MAX_SIZE, stdin);
Find the longest common prefix char a, b; strtok(str1, "\n");
abc, abd, abf -> result is: ab if (len1-1-i < 0) {
int ex01(); a = '0'; printf("Enter second complex
} number: ");
int strSame(char * str1, char * str2); else { fgets(str2, MAX_SIZE, stdin);
a = bin1[len1-1-i]; strtok(str2, "\n");
void del_obj(char arr[][MAX_SIZE], int }
pos, int * size); long a, b, c, d;
if (len2-1-i < 0) { parse_str(str1, &a, &b);
Remove duplicate elements from an b = '0'; parse_str(str2, &c, &d);
array and return the new length. Do }
not create any tmp arr. else {
void ex02(); b = bin2[len2-1-i]; char hi[MAX_SIZE];
} sprintf(hi, "%ld+%ldi", (a*c - b*d),
void parse_str(char str[], long *a, long (a*d + b*c));
*b); // Calculate
if (a == '0' && b == '0') { printf("Result: %s\n", hi);
Product of 2 complex numbers result[res_idx] = '0'; }
void ex03(); }
else if (a == '1' && b == '1') { void ex02() {
void reverse_str(char num[], int len); result[res_idx] = '0'; char arr_str[MAX_SIZE][MAX_SIZE]
next_remainder = 1; = {"hi", "hey", "hi", "hey", "bye"};
int count_str(char *str); } int len = 5;
else {
Binary Summation result[res_idx] = '1'; printf("Enter number of strings: ");
void ex04(); } scanf("%d", &len);

int main() { // Calculate remainder fgets(arr_str[0], MAX_SIZE, stdin);


if (this_remainder) { for (int i = 0; i < len; i++) {
return 0; if (result[res_idx] == '0') { printf("%d. ", i + 1);
} result[res_idx] = '1'; fgets(arr_str[i], MAX_SIZE,
} stdin);
void ex04() { else { strtok(arr_str[i], "\n");
char bin1[MAX_SIZE] = "111"; result[res_idx] = '0'; }
int len1 = 3; next_remainder = 1;
char bin2[MAX_SIZE] = "1"; } int n = len - 1;
int len2 = 1; } for (int i = 0; i < n; i++) {
for (int j = i + 1; j < len; j++) {
printf("Enter first binary number: // Set remainder state if (strSame(arr_str[i],
"); this_remainder = arr_str[j])) {
fgets(bin1, MAX_SIZE, stdin); next_remainder; del_obj(arr_str, j,
strtok(bin1, "\n"); next_remainder = 0; &len);
len1 = count_str(bin1); res_idx++; j--;
} }
printf("Enter second binary }
number: "); if (this_remainder) { }
fgets(bin2, MAX_SIZE, stdin); result[res_idx] = '1';
strtok(bin2, "\n"); res_idx++; printf("Result:\n");
len2 = count_str(bin2); } for (int i = 0; i < len; i++) {
puts(arr_str[i]);
char result[MAX_SIZE] = {'\0'}; reverse_str(result, res_idx); }
int res_idx = 0; printf("Result: "); }
puts(result);
int max_len = len1; } int ex01() {
if (len2 > max_len) {
char arr_str[MAX_SIZE][MAX_SIZE] void del_obj(char arr[][MAX_SIZE], int }
= {"andy", "andre", "anchor"}; pos, int * size) {
int arr_size = 3; for(int i = pos; i < *size; i++) { Draw Digits
int n = 0;
printf("Enter number of strings: "); while (arr[i + 1][n] != '\0') { // 3.1
scanf("%d", &arr_size); arr[i][n] = arr[i + 1][n]; print_a_digit(0);
n++; // 3.2
fgets(arr_str[0], MAX_SIZE, stdin); } print_4_digits(2020);
for (int i = 0; i < arr_size; i++) { arr[i][n] = '\0'; }
printf("%d. ", i + 1); }
fgets(arr_str[i], MAX_SIZE, *size -= 1; void print_a_digit(int n) {
stdin); } for (int i = 0; i < 7; i++) {
} digit_number(n,i);
void parse_str(char str[], long *a, long printf("\n");
if (arr_size == 0) { *b) { }
return 0; char * ptr1; printf("\n");
} char * ptr2; }
int j = 0, plus = 0, i = 0, before_i = 0;
int max_count = 100; while (str[j] != '\0') { void print_4_digits(int num) {
for (int i = 0, n = arr_size - 1; i < n; if (str[j] == '+') { int first, second, third, forth;
i++) { plus = 1; forth = num / 1 % 10;
int counter = 0; } third = num / 10 % 10;
char * s1 = arr_str[i]; if (str[j]== 'i') { second = num / 100 % 10;
char * s2 = arr_str[i + 1]; i = 1; first = num / 1000 % 10;
while((*s1 && *s2) && *s1 == if (str[j - 1] == '0') {
*s2) { before_i = 1; for (int i = 0; i < 7; i++) {
s1++; } digit_number(first,i);
s2++; } printf(" ");
counter++; j++; digit_number(second,i);
} } printf(" ");
*a = strtol(str, &ptr1, 10); digit_number(third,i);
if (counter < max_count) { *b = strtol(ptr1, &ptr2, 10); printf(" ");
max_count = counter; if (*b == 0 && i == 1) { digit_number(forth,i);
} if (before_i == 1) { printf("\n");
*b = 0; }
} } printf("\n");
else { }
printf("Result is: "); *b = 1;
if (max_count == 0) { } void digit_number(int n, int row) {
printf("No common prefix\n"); } if (n == 9) {
} } char digit_9[7][10] = {
"9 9 9 9 9",
for (int i = 0; i < max_count; i++) { void reverse_str(char num[], int len) { "9 9",
printf("%c", arr_str[0][i]); char *left = num; "9 9",
} char *right = &num[len - 1]; "9 9 9 9 9",
printf("\n"); " 9",
return 0; while (left < right) { " 9",
} *left ^= *right; " 9"
*right ^= *left; };
int strSame(char * str1, char * str2) { *left ^= *right; printf("%s", digit_9[row]);
while (*str1 == *str2) {
if (*str1 == '\0' && *str2 == left++; } else if (n == 8) {
'\0') { right--; char digit_8[7][10] = {
return 1; } "8 8 8 8 8",
} } "8 8",
"8 8",
str1++; int count_str(char *str) { "8 8 8 8 8",
str2++; int counter = 0; "8 8",
} while (*(str++) != '\0') { "8 8",
return 0; counter++; "8 8 8 8 8"
} } };
return counter; printf("%s", digit_8[row]);
Recursive numpad printf(" "); }
#include <cstdio> }
#define MAX_SIZE 10 else { void printBitmap() {
list(str, arr, times, idx); for (int i = 0; i < BITMAP_SIZE; i++) {
char map[8][4] = { } printf("%i", bitmap[i]);
{'a', 'b', 'c'}, // 2 } if (i % 2 == 1) { putchar(' '); }
{'d', 'e', 'f'}, if (times == 0) {printf("/ ");} }
{'g', 'h', 'i'}, } printf("\n");
{'j', 'k', 'l'}, }
{'m', 'n', 'o'}, int get_str_and_len(char str[MAX_SIZE])
{'p', 'q', 'r', 's'}, // 7 { void insertBitmap(const int arr[], int len)
{'t', 'u', 'v'}, printf("Enter number's {
{'w', 'x', 'y', 'z'} // 9 combination: "); for (int i = 0; i < BITMAP_SIZE; i++) {
}; fgets(str, MAX_SIZE, stdin); for (int j = 0; j < len; j++){
int len = 0; if (arr[j] == i && bitmap[i]
int get_str_and_len(char char *p_str = str; == 0) {
str[MAX_SIZE]); while (*p_str++ != '\n') {len++;} bitmap[i] = 1;
return len; }
int get_num(char character); } }
}
char map_get(int num, int idx); int get_num(char character) { }
return (int)character - 48;
void list(char str[], char arr[], int times, } void printChange(const int arr[], int len)
int idx); {
char map_get(int num, int idx) { printf("[ ");
int main() { return map[num - 2][idx]; for (int i = 0; i < len; i++) {
char combination[MAX_SIZE] = } printf("%d ", arr[i]);
"214"; Bitmap Sorting }
int len = 3; #include <stdio.h> printf("] -> ");
#define MAX_SIZE 20 printBitmap();
len = #define BITMAP_SIZE 11 }
get_str_and_len(combination);
char order[MAX_SIZE]; void printBitmap(); void scanBitmap() {
list(combination, order, len, -1); printf("Ascending order: ");
return 0; void insertBitmap(const int arr[], int for (int i = 0; i < BITMAP_SIZE; i++)
} len); {
if (bitmap[i] == 1) { printf("%d
void list(char str[], char arr[], int times, void printChange(const int arr[], int len); ", i); }
int idx) { }
times--; void scanBitmap(); printf("\n");
idx++; }
int num = get_num(str[idx]); int bitmap[BITMAP_SIZE] = {};
int loop_times;
if (num < 2) { int main() {
printf("Failed: Number out of int set[MAX_SIZE] = {9, 3, 4};
range\n"); int set_len = 3;
return;
} printf("Enter set's size: ");
else if (num == 7 || num == 9) { scanf("%d", &set_len);
loop_times = 4; printf("Enter set:\n");
} for (int i = 0; i < set_len; i++) {
else { printf("%d) ", i + 1);
loop_times = 3; scanf("%d", &set[i]);
} }
for (int i = 0; i < loop_times; i++) {
arr[idx] = map_get(num, i); insertBitmap(set, set_len);

if (times == 0) { printChange(set, set_len);


for (int j = 0; j <= idx; j++)
{ scanBitmap();
printf("%c", arr[j]);
} return 0;
Struct Students printf("Score: %d\n",
Min Max #include <stdio.h> arr[i].score);
#include <stdio.h> #include <string.h>
#define MAX_SIZE 100 #include <stdlib.h> }
#define MAX_SIZE 100 }
typedef struct MinMax {
int min; void sort(Student arr[MAX_SIZE], int N)
int max; typedef struct Student { {
}MinMax; char name[MAX_SIZE]; for (int i = 0; i < N - 1; i++) {
int id; int j = i;
MinMax getMinMax(int *arr, int len); int score; while (j >= 0 && arr[j].score >
}Student; arr[j + 1].score) {
int main() Student tmp = arr[j];
{ void fgeti (int *num, int N); arr[j] = arr[j + 1];
int arr[MAX_SIZE] = {10, -1, 0, 4, 2, arr[j + 1] = tmp;
100, 15, -20, 24, -5}; void printStudents(Student j--;
int len = 10; arr[MAX_SIZE], int N); }
}
MinMax arrayMinMax; void sort(Student arr[MAX_SIZE], int N); }
arrayMinMax = getMinMax(arr, Function overload
len); int main() {
const int size = 3; int plusFunc(int x, int y) {
Student student_arr[size]; return x + y;
for (int i = 0; i < len; i++) { for (int i = 0; i < size; i++) { }
printf("%d ", arr[i]); printf("Student N0.%d:\n", i +
} 1); double plusFunc(double x, double y) {
printf("Name: "); return x + y;
printf("\nMin: %d\n", fgets(student_arr[i].name, }
arrayMinMax.min); MAX_SIZE, stdin);
printf("Max: %d\n", strtok(student_arr[i].name, int main() {
arrayMinMax.max); "\n"); int myNum1 = plusFunc(8, 5);
printf("ID: "); double myNum2 = plusFunc(4.3, 6.26);
return 0; fgeti(&student_arr[i].id, cout << "Int: " << myNum1 << "\n";
} MAX_SIZE); cout << "Double: " << myNum2;
printf("Score: "); return 0;
MinMax getMinMax(int *arr, int len) { fgeti(&student_arr[i].score, }
int min = *arr; MAX_SIZE);
int max = *arr; printf("\n");
}
for (int i = 0; i < len; i++) {
if (arr[i] > max) { sort(student_arr, size);
max = arr[i];
} printStudents(student_arr, size);
else if (arr[i] < min) {
min = arr[i]; return 0;
} }
}
void fgeti (int *num, int N) {
MinMax arrayMinMax; char *tmp_p;
arrayMinMax.min = min; char tmp_str[N];
arrayMinMax.max = max; fgets(tmp_str, N, stdin);
*num = strtol(tmp_str, &tmp_p,
return arrayMinMax; 10);
} }

void printStudents(Student
arr[MAX_SIZE], int N) {
for (int i = 0; i < N; i++) {
printf("Student NO.%d:\n", i);
printf("Name: ");
puts(arr[i].name);
printf("ID: %d\n", arr[i].id);
Maze_Solver // A wall ahead
#include <stdio.h> turn_left(&dir, &right);
#include <time.h> }
#include <stdlib.h> }
#define BOUND 12 else {
// No wall on the right
int trans_dir (char dir); turn_right(&dir, &right);
step_forward(map,dir_map, dir, &x, &y);
int check_right (char map[BOUND][BOUND], int dir_map[4][2], }
char right, int x, int y);
if (steps++ == 2 * (BOUND - 2) * (BOUND - 2)) {
int check_forward (char map[BOUND][BOUND], int printf("UNSOLVABLE!!! (Steps taken: %i; Map
dir_map[4][2], char dir, int x, int y); area: %i)\n", steps, (BOUND - 2) * (BOUND - 2));
return 1;
void turn_right (char *p_dir, char *p_right); }
}
void turn_left (char *p_dir, char *p_right);
printf("Route taken:\n");
void step_forward (char map[BOUND][BOUND], int for (int row = 0; row < BOUND; row++) {
dir_map[4][2], char dir, int *x, int *y); for (int col = 0; col < BOUND; col++) {
printf("%c ", map[row][col]);
int main() { }
char map[BOUND][BOUND] = {...} printf("\n");
... path| # wall }
// Randomly generate a maze. printf("=================================\n");
srand(time(0)); printf("SOLVED!!!\n");
for (int row = 1; row < BOUND - 1; row++) {
for (int col = 1; col < BOUND - 1; col++) { return 0;
int r = rand() % 4; }
if (r) {
map[row][col] = '.'; int trans_dir (char dir) {
} if (dir == 'E') {
else { return 0;
map[row][col] = '#'; }
} else if (dir == 'W') {
} return 1;
} }
else if (dir == 'N') {
for (int row = 0; row < BOUND; row++) { return 2;
for (int col = 0; col < BOUND; col++) { }
printf("%c ", map[row][col]); else if (dir == 'S') {
} return 3;
printf("\n"); }
} return -1;
printf("=================================\n"); }

int dir_map[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; int check_right (char map[BOUND][BOUND], int dir_map[4][2],
char right, int x, int y) {
char dir = 'E'; int n = trans_dir(right);
char right = 'S'; int i = dir_map[n][0];
int x = 0; int j = dir_map[n][1];
int y = 2;
int steps = 0; // If there is a wall return 1
if (map[y + j][x + i] == '#') {
while (x != (BOUND - 1)) { return 1;
if (check_right(map, dir_map, right, x, y)) { }
// A wall on the right return 0;
if (check_forward(map, dir_map, dir, x, y)) { }
// No wall ahead
step_forward(map, dir_map, dir, &x, &y); int check_forward (char map[BOUND][BOUND], int
} dir_map[4][2], char dir, int x, int y) {
else { int n = trans_dir(dir);
int i = dir_map[n][0]; *p_right = 'W';
int j = dir_map[n][1]; }
else if (*p_right == 'S') {
if (map[y + j][x + i] == '.') { *p_right = 'E';
return 1; }
} }
return 0;
} void step_forward (char map[BOUND][BOUND], int
dir_map[4][2], char dir, int *x, int *y) {
void turn_right (char *p_dir, char *p_right) { int n = trans_dir(dir);
// Turn direction; int i = dir_map[n][0];
if (*p_dir == 'E') { int j = dir_map[n][1];
*p_dir = 'S';
} map[*y][*x] = 'x';
else if (*p_dir == 'W') { *x += i;
*p_dir = 'N'; *y += j;
} map[*y][*x] = 'O';
else if (*p_dir == 'N') { }
*p_dir = 'E';
}
else if (*p_dir == 'S') { String Manipulation
*p_dir = 'W'; const char claim[] = "I am Nguyen Anh Khoa, ITITIU19141\n.
} This is my midterm exercises for C/C++\n."
"This is all my own work. I did
// Change its right appropriately not copy the code from any other source\n";
if (*p_right == 'E') { // 2.1 count the number of characters
*p_right = 'S'; int i = 0;
} while (claim[i] != '\0') {
else if (*p_right == 'W') { i++;
*p_right = 'N'; }
} printf("Number of characters: %i\n", i);
else if (*p_right == 'N') {
*p_right = 'E'; // 2.2 count number of appearances of characters (a-z or
} A-Z) and digits 0-9
else if (*p_right == 'S') { int array[36] = {0};
*p_right = 'W'; i = 0;
} while (claim[i] != '\0') {
} if (claim[i] >= 65 && claim[i] <=90) {
array[(int)claim[i] - 65]++;
void turn_left (char *p_dir, char *p_right) { } else if (claim[i] >= 97 && claim[i] <= 122) {
// Turn direction; array[(int)claim[i] - 97]++;
if (*p_dir == 'E') { } else if (claim[i] >= 48 && claim[i] <= 57) {
*p_dir = 'N'; array[(int)claim[i] - 21]++;
} }
else if (*p_dir == 'W') { i++;
*p_dir = 'S'; }
}
else if (*p_dir == 'N') { // 2.3 draw a histogram
*p_dir = 'W'; printf("\nHISTOGRAM\n");
} for (int j = 0; j < 5; j++) {
else if (*p_dir == 'S') { printf("%c%c %02i ", j+97,j+65, array[j]);
*p_dir = 'E'; for (int k = 0; k < (int)array[j]; k++) {
} printf("*");
}
// Change its right appropriately printf("\n");
if (*p_right == 'E') { }
*p_right = 'N';
}
else if (*p_right == 'W') {
*p_right = 'S';
}
else if (*p_right == 'N') {

You might also like