Function exercises and knowledge points

catalogue

(1) Knowledge points

(2) Function returns two variables

(3) Print prime numbers within 100-200

(4) Implement a function to print the multiplication formula table, and the number of rows is specified by yourself

(5) The reverse order of string is realized by function. The string operation function in C function library (symmetrical character exchange) cannot be used

(6) Find the sum of each digit of a number

(1) Knowledge points

(1) Return can only return one value, not two. For example: return a,b; This is the wrong way of writing.

(2) The array name is the address

(3) The formal and actual parameters of a function can have the same name

(4) The calling function and the called function may not be in the same folder #include ".h that function name"

(5) Function design should pursue high cohesion and low coupling (function independence is relatively high)

(6) Functions should use as few global variables as possible and should not have too many parameters

(7) Memory space includes stack area (local variables, temporary parameters, etc. are recovered and released when they are stored out of the program, and destroyed when they are not used for a while), heap area (dynamically allocated memory), and static area (static variables, global variables). So the formal parameters of the function are saved in the stack.

(2) Function returns two variables

Code display:

#include <stdio.h>
void test(int arr[])
{
	arr[0] = 10;
	arr[1] = 20;
}
int main()
{
	int arr[2] = { 0 };
	test(arr);
	printf("%d %d", arr[0], arr[1]);
	return 0;
}

(1) The array name is originally the address. (2) the array parameter is the address of the first element to avoid space waste

(3) Print prime numbers within 100-200

Code display:

#include <stdio.h>
int sushu(int n)
{
	int j = 0;
	for (j = 2; j < n; j++)
	{
		if (n % j == 0)
			return 0;
	}
	if (j == n)
		return 1;
}
int main()
{
	int i = 0;
	for (i = 100; i <= 200; i++)
	{
		if (sushu(i) == 1)
			printf("%d", i);
		else
			continue;
	}
	return 0;
}

Optimization code display:

#include <stdio.h>
#include <math.h>
int is_prime(int n)
{
	int j = 0;
	for (j = 2; j <= sqrt(n); j++)
	{
		if (n % j == 0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int i = 0;
	for (i = 100; i < 201; i++)
	{
		if (is_prime(i) == 1)
		{
			printf("%d ", i);
		}
	}
	return 0;
}

Pay attention to comparing these two functions and learn a more concise way to write them

(4) Implement a function to print the multiplication formula table, and the number of rows is specified by yourself

#include <stdio.h>
void chengfa(int n)
{
	int i = 0;
	int j = 0;
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= i; j++)
		{
			printf("%d*%d=%-2d ", i, j, i * j);
		}
		printf("\n");
	}
}
int main()
{
	int line = 0;
	scanf("%d", &line);
	chengfa(line);
	return 0;
}

(5) The reverse order of string is realized by function. The string operation function in C function library (symmetrical character exchange) cannot be used

Code 1 shows: (using array method)

#include <stdio.h>
int my_strlen(char* s)
{
	int count = 0;
	while (*s != '\0')
	{
		count++;
		s++;
	}
	return count;
}
void reverse_str(char arr[])
{
	int left = 0;
	int right = my_strlen(arr) - 1;
	while (left < right)
	{
		char a = arr[left];
		arr[left] = arr[right];
		arr[right] = a;
		left++;
		right--;
	}
}
int main()
{
	char arr[] = "abcdefg";
	reverse_str(arr);
	printf("%s", arr);
	return 0;
}

Code display: (using pointer method)

#include <stdio.h>
int my_strlen(char* s)
{
	int count = 0;
	while (*s != '\0')
	{
		count++;
		s++;
	}
	return count;
}
void reverse_str(char* a)
{
	char* left = a;
	char* right = a + my_strlen(a) - 1;
	while (left < right)
	{
		char b = *left;
		*left = *right;
		*right = b;
		left++;
		right--;
	}
}
int main()
{
	char arr[] = "abcdefg";
	reverse_str(arr);
	printf("%s", arr);
	return 0;
}

Code display: (using recursive method)

#include <stdio.h>
int my_strlen(char* s)
{
	int count = 0;
	while (*s != '\0')
	{
		count++;
		s++;
	}
	return count;
}
void reverse_str(char* a)
{
	int b = my_strlen(a);
	char c = *a;
	*a = *(a + b - 1);
	*(a + b - 1) = '\0';
	if (my_strlen(a + 1) > 1)
		reverse_str(a + 1);
	*(a + b - 1) = c;
	
}
int main()
{
	char arr[] = "abcdefg";
	reverse_str(arr);
	printf("%s", arr);
	return 0;
}

This code is difficult to understand. After the first element and the last element are exchanged, the last element is replaced with '\ 0', and then the last element is replaced with the first element one by one during recursive regression.

(6) Find the sum of each digit of a number

Write a recursive function, enter a non negative integer and return it to the sum of its numbers.

Code display:

#include <stdio.h>
int DigitSum(size_t n)
{
	if (n < 9)
		return n;
	else
		return DigitSum(n / 10) + n % 10;
}
int main()
{
	size_t n = 0;
	scanf("%u", &n);
	int ret = DigitSum(n);
	printf("%d", ret);
	return 0;
}

Nonnegative integer size_t   %u

Keywords: C

Added by c_coder on Fri, 04 Feb 2022 16:28:41 +0200