Key Elements for Embedded Engineers to Master Programming

Follow+Star Public Account Number, don’t miss out on exciting content

Key Elements for Embedded Engineers to Master Programming

Source | Internet

As an embedded engineer, how can you write efficient and clear C language programs?

  • You need to build the program structure using the C language’s way of thinking.
  • You should have a solid foundation in C language algorithms to implement the program’s logical structure.
  • Flexibly use pointer operations in C language.

Although the above statements may seem abstract and confusing, they essentially describe the process of encountering problems, analyzing them, and solving them using C language.When embedded engineers write C language programs, they need to construct the program structure based on the problems encountered.For example, consider the classic problem of “Monkeys Selecting a King”: a group of monkeys stands in a circle, holding hands. Starting from any monkey, they count from 1. When they reach a predetermined number to be excluded, that monkey exits the circle, and counting continues from the next monkey. This process repeats until only one monkey remains, who becomes the king.

Building Program Structure Using C Language Thinking

The program consists of three main parts:

a. Data acquisition: To run the program, we need to obtain the total number of monkeys, the starting monkey, and the number to be excluded;b. Data computation: We need to exclude the corresponding data from a set of data, ensuring logical correctness;c. Improve the program’s execution speed by using pointers more and loops less.

Logical Implementation Using C Language

1. Data acquisition, using printf and scanf to obtain parameters.

/* Read input conditions */printf("input total num:");scanf("%d", &n);printf("from which num begin:");scanf("%d", &k);if(k&n||k==0){printf("please input the right begin num");return 1; }printf("input the out num:");scanf("%d", &m);if(m&n||m==0){ printf("please input the right del num");return 2; }

Also, pay attention to handling exceptions. For example, the two if statements above check for exceptional cases, with each exception corresponding to a different return value, which facilitates debugging and ensures data validity.

2. Define the linked list node type

typedef struct node{int data;struct node *next;}linklist;

Construct a circular linked list to represent the “monkey circle”.

/* Create a circular linked list, the head node also stores information */head = (linklist*) malloc(sizeof(linklist));p = head;p->data = 1;p->next = p;/* Initialize circular linked list */for (i = 2; i <= n; i++){s = (linklist*) malloc(sizeof(linklist));s->data = i;s->next = p->next;p->next = s;p = p->next;}

After this step, both head and p (present) become a linked list of “monkeys”. During the construction of this linked list, pay attention to a few points: memory allocation should follow the principle of allocating as much as needed.

If too much memory is allocated at once, it can lead to memory leaks, but this small program will not encounter such issues. Additionally, familiarize yourself with the construction method of circular linked lists: the tail of the list points to the head. At this point, one might also think of the case of doubly linked lists.

3. Find the k-th node

p = head;for (i = 1; i <= k; i++){p = p->next;}

Find the starting position for counting. At this point, p points to the starting “monkey”. Since we are using a linked list, this process only requires focusing on the next pointer of p.4. Save the initial parameters of the “monkey” circle

/* Save total number of nodes */total = n;printf("\nthe out num:");q = head;

Why is this necessary? First, to control the number of monkeys, we retain the total count. Secondly, we use q (query) to keep the linked list of monkeys before exclusion and connect it to the linked list after exclusion. This completes the deletion of elements in the circular linked list.5. Counting MonkeysCounting monkeys is the key task of the entire program, which needs to accomplish the following: a. Find the starting “monkey”; b. Delete that “monkey”; c. Connect the head and tail of the deleted circular linked list.

/* Stop the loop when only one node remains */while (total != 1){/* Counting process, p points to the node to be deleted */for (i = 1; i < m; i++){p = p->next;}/* Print the number of the node to be deleted */printf("[%d] ", p->data);/* q points to the predecessor of p node */while (q->next != p){q = q->next;}/* Delete p node */q->next = p->next;/* Save the pointer to the deleted node */s = p;/* p points to the successor of the deleted node */p = p->next;/* Free the deleted node */free(s);/* Decrease the node count by one */total--;}/* Print the last remaining node number */printf("\nthe last num:[%d] \n\n", p->data);free(p);}

Through the above data computation, we can complete the deletion of corresponding elements in the linked list, which perhaps showcases the charm of C language programming.

Using Pointers in C Language

For example, the definitions of the linked list pointers in this program: p, s, q.

linklist *head, *p, *s, *q;

We know that pointer operations can not only reduce the memory required for data operations but also improve the execution speed of the program.The advantages of pointers may not be obvious in this program, but they become significant in cases with large data sets and high-speed operation requirements, such as in the Linux kernel.In summary, for embedded engineers, being able to design good hardware circuits and also write good software programs will make our lives even better.

Disclaimer:This article’s material comes from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact me for removal.

———— END ————

Reply in the background with “Embedded C Language” or “Operating System” to read more related articles.

Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.Welcome to follow my video account:

Key Elements for Embedded Engineers to Master Programming

Click “Read the Original” to see more shares, and feel free to share, bookmark, like, and view.

Leave a Comment