C language-first time job

Source: Internet
Author: User

Topic 6-1 calculates the and the difference of two numbers

1. Design Ideas
(1) Main description problem algorithm
The first step: see the main function know the program input floating-point variable A, B, through the function to calculate the output and the difference.
The second step: the function section assigns a value op1,b assignment op2,&sum to the pointer variable Psum,&diff assignment to the pointer variable Pdiff.
Step three: Write the calculation process within the function.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

void sum_diff( float op1, float op2, float *psum, float *pdiff ){  *psum=op1+op2;  *pdiff=op1-op2;}

3. Problems encountered during commissioning and solutions
No

Topic 6-2 splitting the integers and fractional parts of a real number

1. Design Ideas
(1) Main description problem algorithm
The first step: The main function defines the floating-point variable x, Fracpart; reshape variable Intpart. Enter x and calculate by calling the function splitfloat, and then output the result.
The second step: X assignment to X,&intpart assignment to Intpart, &fracpart assignment to Fracpart.
The third step is to convert the X into an integral type, and subtract the integer part from the real part to get the fractional part.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

void splitfloat( float x, int *intpart, float *fracpart ){  *intpart=(int)x;  *fracpart=x-(int)x;}

3. Problems encountered during commissioning and solutions
No

Topic 6-1 Finding the specified element in an array

1. Design Ideas
(1) Main description problem algorithm
The first step: The main function defines the integer variable i, index, N, x,a[10], input n,for Loop input array element, input unknown origin element x, through function search to get the index value, if index is not 1, then output index
value, otherwise the output is not found;
The second step: Define pointer variable p, integer variable i;p point to List[i];
The third step is to see if there is an X value in the array through the For loop and if judgment statement, or return 1 if it is obtained.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

int search( int list[], int n, int x ){  int *p,i;  p=&list[i];  for(i=0;i<n;i++)  {    if(*p++==x)      return i;  }  return -1;}

3. Problems encountered during commissioning and solutions
When a pointer variable is used, the *p++ is applied within the loop to represent the array element.

Topic 6-2 Finding the maximum value and its subscript

1. Design Ideas
(1) Main description problem algorithm
The first step: Define N as 10. The main function defines an integer variable a[n],i,max,p=0;for the loop input array element, which is called by the function Fun (a,&p,n) to get the maximum value of Max and subscript p; output.
Step two: Variables in fun (A,&P,N) are assigned to variables in int fun (int a,int b,int N);
The third step is to assign the first element to max as the maximum value, and then make the largest one by a for-loop comparison, and assign the subscript to B.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

int fun(int *a,int *b,int n){  int i;  int  max=*a;  for(i=1;i<N;i++)  {    if(*(a+i)>*a)    {      *b=i;      max=*(a+i);    }  }  return max;}

3. Problems encountered during commissioning and solutions
No

6-1 minimum number before the maximum number of places

1. Design Ideas
(1) Main description problem algorithm
The first step: The main point is to call three functions after the resulting and output. The first and third functions use a for loop input and output.
Step Two: The second function is the most important. In two parts, the maximum and minimum values of all elements are identified with a for loop, then the maximum value is exchanged with the first number, and the minimum value is exchanged with the last number. 、
(2) Flowchart
Main function:

Call Function:
①input (int *arr,int N)

②max_min (int *arr,int N)

③output (int *arr,int N)

2. Experiment Code

void input(int *arr,int n){  int i;  for(i=0;i<n;i++)  {    scanf("%d",arr+i);  }}void max_min(int *arr,int n){  int i,max,min,t,p;  max=min=*arr;  for(i=0;i<n;i++)  {    if(max<*(arr+i))    {      max=*(arr+i);      t=i;    }    if(min>*(arr+i))    {      min=*(arr+i);      p=i;    }  }  int j;  j=*(arr+t);  *(arr+t)=*(arr+n-1);  *(arr+n-1)=j;  int k;  k=*(arr+p);  *(arr+p)=*arr;  *arr=k;}void output(int *arr,int n){  int i;  for(i=0;i<n;i++)  {    printf("%3d",*(arr+i));  }}

3. Problems encountered during commissioning and solutions
No

Topic 6-2 Ordering of the pointer selection method

1. Design Ideas
(1) Main description problem algorithm
The first step: to invoke a function to select the sorting method, you need to use a double loop structure.
The second step: Call the function of the first layer loop as the number of times, the second layer is the comparison size, a comparison to find the maximum value put to the front.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

void sort(int *x,int n){  int i,j,t;  for(i=1;i<=n-1;i++)  {    for(j=0;j<=n-i-1;j++)    {      if(x[j]<x[j+1])      {        t=x[j];        x[j]=x[j+1];        x[j+1]=t;      }    }  }}

3. Problems encountered during commissioning and solutions
More curly braces cause a later missing one to end, and a compilation error.
After careful examination, the error is found and corrected.

6-1 judging a palindrome string

1. Design Ideas
(1) Main description problem algorithm
The first step: Observe the main function, enter the array elements after input by calling Palindrome to determine whether the string is a palindrome string. Final output.
Step Two: Read the length of the array through the Strlen function.
The third step is to compare the characters in the symmetric position in the string for equality, if there is an inequality, the return value is false, and if no false is returned, the Palindrome function returns True.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

bool palindrome( char *s ){    int i;    int n = strlen(s);    for(i=0;i<=n/2;i++)    {        if(*(s+i)!=*(s+n-i-1))        return false;    }    return true;}

3. Problems encountered during commissioning and solutions
No

6-2 using a function to implement a partial copy of a string

1. Design Ideas
(1) Main description problem algorithm
First step: main function definition string T[MAXN], S[MAXN] (maxn=20), variable M.
Step Two: Reference the function strmcpy (char t, int m, char s) to implement a partial copy of the string. Output.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

void strmcpy( char *t, int m, char *s ){    int i;    for(i=m;*(t+i-1)!=‘\0‘;i++)    {        *(s+i-m)=*(t+i-1);    }    *(s+i-m)=‘\0‘;}

3. Problems encountered during commissioning and solutions
The part of the function does not know how to achieve, through the students to help complete the explanation.

Additional Questions

Experiment Code

#include<stdio.h>int main(){    char c;    while((c=getchar())!=‘\n‘)    {        if((c>=‘a‘&&c<=‘z‘)||(c>=‘A‘&&c<=‘Z‘))        {            c=c+2;            if(c>‘Z‘&&c<=‘Z‘+2||c>‘z‘)            c=c-26;        }        printf("%c",c);    }    printf("\n");    return 0;}

Learning Summary and Progress

1. What have you learned from the knowledge points learned in the two weeks? which has not yet been learned?
Learned the use of pointers in programs. Learn about the use of pointer variables in relation to some of the previously learned variables.
We also learned how to count string lengths (using the Strlen function).
What you have not learned or want to learn is to use the pointer variables more skillfully, at present is very unfamiliar, should be more practice.
2. Git address: https://git.coding.net/ZJY15/ZJY15.git
Upload:

3. Reviews:
Wang Juewen http://www.cnblogs.com/phsudie/p/8590614.html
Fung Dawei http://www.cnblogs.com/DavidPark/p/8551402.html
Dong Yajie http://www.cnblogs.com/exo123/p/8575595.html
4. Please use the table and line chart to present the number of lines and time of your week (3/12 8:00~3/26 8:00), blog count and Time spent

C language-first time job

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.