C Language Blog Job--character array

Source: Internet
Author: User

First, the PTA laboratory work Title 1:7-2 count the number of words in a line of text 1. PTA Submission List

2. Design ideas (flowchart)

3. Code

4. Problems encountered in commissioning process and PTA submission List status note
    • The answer is wrong, judge the word code is wrong, the result output number, did not take into account in a word after the end of the space and comma processing;
    • Debugging found that after a word has been determined to error, the result I in the Loop 3 times after the end;

Topic 2:7-3 Find the longest string 1. PTA Submission List



2. Design Ideas

3. Code

4. Problems encountered in commissioning process and PTA submission List status note
    • Thinking problem: Input string can not be all the output of the line, there is a beginning do not know how to first carry out the logo between each string, and then separate the comparison, need to take time to organize ideas, figuring out how each small step can be achieved, and then DEVC writing;
    • Answer error: Output to the number of characters, output each string out the problem;
    • Ask classmate, can adopt the two-dimensional array of string to realize, solve;
    • Part of the correct, multiple errors, format errors, etc., found to be about the array of cross-border problems, the array storage enlargement, solve;
Topic 3:7-6 Jack Cheng's troubles 21. PTA Submission List

2. Design Ideas

3. Code

4. Problems encountered in commissioning process and PTA submission List status note
    • Start thinking complex error, ask classmates, find simple problem-solving ideas;
    • Programming errors, not run, debugging modify the loop condition, and the previous step read n when the carriage return to be absorbed;

Second, this week's topic set PTA Final Ranking

Third, peer code peer review 1. Mutual evaluation of students name: Xu Hongwei 2. My code, peer-reviewed classmate code My Code:

Xu Hongwei Student Code:

3. Where do I differ from my classmates ' code? What are the advantages? What style of code do you prefer? If the classmate code is wrong, please help to point out where the problem
    • I and classmate of the code in the idea of some differences, the beginning of the string input method is not the same, I directly use get () relative to the code number can be reduced, but in considering the carriage return these will have restrictions, so he will be more rigorous, each have thinking;
    • When looking for the longest string, he used to go into the new array every time he found one, and then output it directly, while mine was using the flag flag = i to save the corresponding number of rows, and finally the string output of the corresponding number of rows was used in the array;
    • The comments in his code are concise but clear, and my notes always comment too much to look bad and affect reading, and this is worth learning;
    • I would like his code relatively, the notes clear, get after reading convenient, the idea is easy to want;
Iv. Study Summary of the Week 1. What did you learn? 1.1 How pointer variables are defined
对指针变量的定义包括三个内容:   (1) 指针类型说明,即定义变量为一个指针变量;   (2) 指针变量名;   (3) 变量值(指针)所指向的变量的数据类型;一般格式:类型说明符  *变量名;                   * 表示这是一个指针变量,变量名即为定义的指针变量名,类型说明符表示本指针变量所指向的变量的数据类型;        例如: int  *p1;                    p1是一个指针变量,其值是某个整型变量的地址,或者说p1指向一个整型变量;至于p1究竟指向哪一个整型变量,应由向p1赋予的地址来决定;        再如:int *p2;   p2是指向整型变量的指针变量           float *p3;    p3是指向浮点变量的指针变量            char *p4;    p4是指向字符变量的指针变量【注意】一个指针变量只能指向同类型的变量,如 P3 只能指向浮点变量,不能时而指向一个浮点变量,时而又指向一个字符变量;
1.2 pointer addition operation in which case, can the 2 pointer variables be added to each other?
(1)对于指向数组的指针变量,可以加上或减去一个整数 n:设 p是指向数组a的指针变量,则 p+n,p-n,p++,++p,p--,--p 运算都是合法的;      指针变量加或减一个整数n的意义是把指针指向的当前位置(指向某数组元素)向前或向后移动n个位置;   【注意】数组指针变量向前或向后移动一个位置和地址 加1 或 减1 在概念上不同的;                  因为数组可以有不同的类型, 各种类型的数组元素所占的字节长度是不同的;                                             如指针变量加1,即向后移动 1 个位置表示指针变量指向下一个数据元素的首地址,而不是在原地址基础上加1;       例如: int a [ 5 ],*p;                      p = a;  p 指向数组a,也是指向a [ 0 ];                p = p + 2;;  p 指向a [ 2 ],即 p 的值为&p[ 2 ];(2)指针变量的加减运算只能对数组指针变量进行, 对指向其它类型变量的指针变量作加减运算是毫无意义的;(3)两个指针变量之间的运算只有指向同一数组的两个指针变量之间才能进行运算, 否则运算毫无意义;   【注意】两个指针变量不能进行加法运算;       【原因】指针变量是种特殊的变量,指针变量的值存放的是所指向变量的地址,两个地址相加并不能保证结果为一个有效的地址值,C 中指针变量相加是非法的;                  
1.3 The pointer does not assign the initial value, direct use, what happens, please verify with DEVC, and show
    • Typically, a pointer without an assignment may point to an arbitrary address, an unpredictable result with a pointer that is not initialized, and if a dereference (or *pointer) operation is performed on the pointer at this point, it may overwrite the in-memory information, which could cause the program that is referencing the memory area to crash and, in severe cases, exit , but does not undermine the program itself;
1.4 Lessons about separating the integer and fractional parts of a floating-point number, use DEVC to verify the implementation, and show it in this map, and also indicate which is the pointer variable to do the function parameter, and how the function argument should be represented. What is the use of pointer variables for function parameters?

    • If the address of a variable is used as the argument of a function, the corresponding parameter is the pointer;
    • 13 line is the pointer variable to do the formal parameter, the argument means: void splitfloat (float num, int z, float x);
    • A pointer variable makes a function parameter that allows the function to return multiple values;
1.5 Please change the bubble sort function of the textbook into a pointer variable to do the formal parameter format, and write the code at the bottom, pay attention to the markdown syntax rendering
#include<stdio.h>void bubble(int *p,int n);int main(){    int i,n,t,k,count=0,flag = 0;    int s[5]={1,6,8,5,3};    int *p = s;     for(i=0;i<5;i++)    printf("%d ",*(p+i));    bubble(p,5);    printf("\n");    for(i=0;i<5;i++)    printf("%d ",*(p+i));    return 0; } void bubble(int *p,int n){    int t;    int i,j;    for(i=0;i<n;i++){        for(j=0;j<n-1;j++){            if(*(p+j)>*(p+j+1)){                t=*(p+j);                *(p+j) = *(p+j+1);                *(p+j+1) = t;            }        }    }}
1.6 How to define a pointer variable to an array, and how to use a pointer variable to represent an array element?
数组指针用于指向一个数组;如:int (*p)[ 10 ];用指针变量表示数组元素:   (1)p++; *p;   (2)*p++;等价于*(p++);因为++和*的优先级一样,故结合方向是从右向左;   (3)*(p++);和*(++p);二者是有区别的,前者是先取*p的值,然后p加1;后者是先p加1,再取p的值;即如果p指向的是数组a元素a[0],则前者得到a[0]的值,后者得到a[1]的值;   (4)++(*p);将p指向的元素的值加1;   (5)如果p指向元素a [ i ]:             *(p--); 先得到p指向的地址的元素a [ i ],然后 p 减1;             *(++p);执行结果得到a [ i+1 ],p加1;            *(–p);执行结果得到a [ i-1 ],p减1;
1.7 How do I define a character pointer to a string? Where is the initial position after the pointer points to the string?

Define a pointer
Char *p;
Char str[10] = "AFJKFDHSJK";
p = str;
Pointer p points to the first address of the string str

1.8 Use a character pointer to manipulate strings, such as design functions, to implement string joins, and show the code in this map. What are the advantages of the method that indicates the pointer represents a character?
    • The method of the pointer represents the advantages of the character: the use of flexible, in the actual data transmitted is too large, the use of pointers can be directly transmitted data address, both to improve transmission speed and save memory;
#include <stdio.h>char *strmcat( char *str1, char *str2 );//使用const来约束strSrc,提高程序的健壮性。如果函数体内的语句试图改动str2的内容,编译器将指出错误。int main(){    char s[80],t[80];    int m;    char *p=s,*q=t;    gets(s);    gets(t);    printf("%s",strmcat(p,q));    return 0;}char *strmcat( char *str1, char *str2 ){    int i=0,j=0;    while(*(str1+i)!=‘\0‘) i++;//找到字符串的末尾    while(*(str2+j)!=‘\0‘){//进行拼接        *(str1+i)=*(str2+j);        i++;        j++;    }    *(str1+i)!=‘\0‘; //结束    return str1;}
2. What is your content this week? 2.1 Classroom allocation and how to revise the wrong questions

Questions and corrections in class assignment:
7, single-choice: among the following assignments or initializations, __ is wrong.
A, Char str[10]; Str= "string";
B, char str[]= "string";
C, char p= "string";
D, char
p; P= "string";
My answer: D
Reference Answer: A
Parsing: Character array, character pointer processing string;
The character array occupies contiguous cells in memory, determines the address, each array element places a character of the string, and the string is stored in the array;
Instead of placing a string in a character pointer variable, the character pointer occupies only one memory unit that can hold the address, storing the address of the first character of the string;

2.2 Others not? How are we going to solve
    • Other will not be in the class to check the information oneself first try to solve the problem, really can not solve, it is necessary to find will write the big boys to consult, understand, understanding, and then try to write again or find similar training examples;
3. The test summary of the array on the machine
    • The problem of the array on the computer exam is more difficult than before, not on the paper on the idea of a simple way to start writing procedures, it will lead to the program is too messy, or simply does not work;
    • The content of the teacher is not fully understood at ordinary times, or some problems in itself did not understand but not in time to find teachers or classmates to understand, the results of the test in the machine and immediately will not be;
    • After class did not go to a certain programming exercise, resulting in programming ability has been unable to improve or even poor;
    • The ability to check the wrong is not enough, always find out or check is very slow;
    • Summary: Textbook examples to understand after trying to program, the classroom can not only stay in the understanding stage, the wrong problem also must learn to take the right ideas to absorb, and their own programming, many times to change the wrong, and constantly try to understand, but also to read more excellent code, really understand the code ideas, oneself also try to learn to use, first to be able It is relatively quick to find fault;
3.1 Which question is wrong, please list
    • On the machine test written but not the wrong question list:



3.2 Wrong question How to revise, why wrong?
    • Has not started to revise, first wrote the PTA above the similar question, looked at the teacher said the question, then to change the mistake will be better;
    • The cause of the mistake: see above summary;

C Language Blog Job--character array

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.