Cainiao cultivation C language small design-score statistics

Source: Internet
Author: User

It has been a year since I decided to learn embedded by myself. I have reviewed a series of courses on linux system management, ARM programming, system porting, and driver, however, I still feel that the ability to write code is far from enough, and it is difficult to think about it. So I began to cultivate C language again, in order to gain a deep understanding of C language and write high-quality C code. I believe that many of my colleagues will certainly be the same as me. Although I understand the basic syntax of C language, I can write single-chip microcomputer and ARM programs or solve some simple questions, but I will only stay at the stage of understanding, little is known about the essence and focus of C language. I decided to write some small designs in C language to change this status. I will update my learning track one after another. I hope you can learn it together. These designs will be applied to all aspects of the C language from a simple perspective.

This small design focuses on understanding the usage of arrays and pointers:

Pointer array;

Pointer subscript reference;

Dynamic Memory Allocation and release.

Arrays and pointers are important concepts in C language, and the relationship between arrays and pointers is easy to confuse. In addition, the usage of string arrays is different from that of ordinary numeric arrays, and their relationship with pointers is also special.

1.1 Design Questions

This course is designed for the use of exercise arrays and pointers.

Assume that the original information of the average score of each subject in a given class is as follows:

Mathematics: 75

Physical: 80

Foreign language: 83

Politics: 85

Sports: 86

Number of students: 30

Calculate the total average score of the entire semester, the lowest score of the subject, and the score of the subject. The output result is as follows:

The average scores of all subjects in the class are as follows:

Mathematics: 75

Physical: 80

Foreign language: 83

Politics: 85

Sports: 86

The statistical result is as follows:

Number of students: 30

Average score: 81

Score of the lowest score: 75

Number of subjects: Mathematics

1.2 design requirements

The requirements for storing raw data are as follows:

(1) Use integer array a to store scores of mathematics, physics, foreign language, politics, sports, people, average score, and lowest score.

(2) Use the string array name to store mathematics, physics, foreign languages, politics, sports, and scores.

(3) Use the string array MeanLow to store the number of students, average score, average score, objective score, and average score.

There are many ways to implement this design function. Here we provide three different levels of difficulty requirements, in order to deepen the understanding of the array and pointer knowledge.

1. Implement with Array

Use arrays and pointer arrays.

2. Implement with allocated memory

You must store the contents of array a in the allocated memory and use pointers to access and compute the memory content.

3. store all the array content in the memory

Allocate memory for characters and store the content of the character array in the allocated memory.

2. Design Code

# Include <stdio. h> # define sub_num 5 void init_name (char * name []); void init_mean_low (char * mean_low []); void input_messge (char * name [], char * mean_low [], int * A); void analysis_message (int * a, char * name []); void print_message (char * name [], char * mean_low [], int * A); void recycle_memory (char * name [], char * mean_low [], int * A); int main (INT argc, char * argv []) {int * A; char * name [sub_num + 1]; char * mean_low [3]; /* array a stores mathematics, physics, foreign languages, politics, sports, people, average score, and scores */A = (int *) malloc (8 * sizeof (INT); memset (A, 0, 8 * sizeof (INT); init_name (name); init_mean_low (mean_low); input_messge (name, mean_low, a); analysis_message (A, name); print_message (name, mean_low, a); recycle_memory (name, mean_low, a); getch (); Return 0 ;} void init_name (char * name []) {name [0] = "Mathematics"; name [1] = "physics"; name [2] = "Foreign Language "; name [3] = "Politics"; name [4] = "Sports"; name [5] = "subject";} void init_mean_low (char * mean_low []) {mean_low [0] = "Student count"; mean_low [1] = "average score"; mean_low [2] = "average score "; mean_low [3] = "subjects with low scores";} void input_messge (char * name [], char * mean_low [], int * A) {int I; printf ("Enter your score for each subject: \ n"); for (I = 0; I <sub_num; I ++) {printf ("% s :", name [I]); scanf ("% d", A + I); While (A [I]> 100) | (A [I] <0 )) {printf ("incorrect input. Please enter \ n % s:", name [I]); scanf ("% d", A + I );}} printf ("Enter the number of students in your class:"); scanf ("% d", A + sub_num);} void analysis_message (int * a, char * name []) {int aver, I, sum = 0, Sm = 200, j = 0; for (I = 0; I <sub_num; I ++) {sum + = A [I]; if (a [I] <sm) {Sm = A [I]; j = I ;}} A [sub_num + 1] = sum/sub_num; A [sub_num + 2] = Sm; name [sub_num] = Name [J];} void print_message (char * name [], char * mean_low [], int * A) {int I; printf ("analysis result: \ n"); for (I = 0; I <sub_num; I ++) {printf ("% s: % d \ n", name [I], a [I]);} printf ("Students: % d \ n ", A [sub_num]); printf ("average score: % d \ n", a [sub_num + 1]); printf ("minimum score: % d \ n ", A [sub_num + 2]); printf ("minimum subject: % s \ n", name [sub_num]);} void recycle_memory (char * name [], char * mean_low [], int * A) {int I; free (a); A = NULL; for (I = 0; I <sub_num + 1; I ++) {free (name [I]); name [I] = NULL;} for (I = 0; I <4; I ++) {free (mean_low [I]); mean_low [I] = NULL ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.