CHAPTER VIII EXPERIMENTAL REPORT

Keywords: PHP Programming C

C Language Programming

 

Experiments: Functions and Macro Definitions

Name: Xu Xinqi. Place of the experiment: Classroom 514, First Teaching Building. Time of the experiment: 12 June

I. The Purpose and Requirement of the Experiment

1. The Basis of Pointer and Pointer Operation

2. Data Exchange

3. String inversion and string connection

4. Parity Arrangement of Array Elements

 

II. EXPERIMENTAL CONTENTS

8.3.1 Exercise:

(1) Define an integer pointer variable p to point to an integer variable a, define a floating point pointer q to point to a floating point variable b, and define another integer variable c and assign an initial value 3.
(2) Using pointer variables, call the scanf function to enter the values of a and b, respectively.
(3) Indirectly accessing and outputting the values of a and B through the pointer
(4) The value of P and Q and the address of a and B are output in hexadecimal mode.
(5) Point p to c and indirectly access the value of c through P and output it.
(6) Output the value of p and the address of c, and compare with the results above.

 

 

 

Operation results:

 

Experimental code:

#include <stdio.h>
int main ()
{
    int *p,a,c=3;
    float *q,b;
    p=&a;
    q=&b;
    printf("Please Input the Value of a,b:");
    scanf("%d%f",p,q);  /* Use pointers p and q to enter the values of a and B */ 
    printf("Result:\n");
    printf("     %d,%lf\n",a,b);    
    printf("     %d,%lf\n",*p,*q);    /*  Indirectly output the values of a and B through pointers p and q  */
    printf("The Address of a,b: %p,%p\n",&a,&b);
    printf("The Address of a,b:%p,%p\n",p,q);/* Output the values of p and q and compare them with the previous output */
    p=&c;
    printf("c=%d\n",*p);
    printf("The address of c:%x, %x\n",p,&c);
    return 0;
}

 

 

 

Procedure analysis: the problem is relatively simple, but there is a mistake: scanf("%d%f",p,q); because P and Q are addresses themselves, there is no need for &, but I started to learn to add inertia, it turned into a drag, runtime chaos;

 

 

 

8.3.2 Exercise:

(1) Define two functions, void swap(int a,int b) and void Swap 2 (int a, int b), to exchange the values of a and B.
(2) Two integer variables A and B are input from the main function.
(3) Call the above two exchange functions from the main function and print out the results of a and B after exchange.

 

 

Operation results:

Experimental code:

#include <stdio.h>
//Function declaration void swap1(int x,int y); void swap2(int *x,int *y); main() { int a,b; printf("Please input a=: "); scanf("%d",&a); printf("\n b=: "); scanf("%d",&b); swap1(a,b); printf("\n After call swap1: a=%d b=%d\n",a,b); swap2(&a,&b); printf("\n After call swap2:a=%d b=%d\n",a,b); return 0; } void swap1(int x,int y) { int temp; temp=x; x=y; y=temp; } void swap2(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; }

 

 

 

Procedure analysis: The teacher talked about this topic in class. Because I discussed it with my classmates in class, I wrote this program smoothly.

 

 

8.3.3 Exercise:

(1) Define two character pointers and input two strings through get () function
(2) Define a function char reverse(charstr) by pointer
(3) Define a function char link(charstr1,char*str2) to connect two strings by pointer movement
(4) Call the above functions from the main function, input the string and print the output results.

 

 

Operation results:

Experimental code:

# include <stdio.h>
# include <conio.h>
char *reverse(char *str);
char *link(char *str1,char *str2);
int main()
{
    char str[30],str1[30],*str2;
    printf("Input reverse character string:");
    gets(str);
    str2=reverse(str);
    printf("\n Output reverse character string:");
    puts(str2);
    printf("\n Input str1: ");
    gets(str);
    printf("\n Input str2: " );
    gets(str1);
    str2=link(str,str1);
    puts(str2);
    return 0;    
}

char *reverse(char *str)
{
    char *p,*q,temp;
    p=str,q=str;
    while(*p!='\0')
       {
        p++;
       }
    p--;
    while(q<p)
    {
        temp=*q;
        *q=*p;
        *p=temp;
        q++;
        p--;
    }
    return str;
}

char *link(char *str1,char *str2)
{
    char *p=str1,*q=str2;
    while(*p!='\0')
    {
           q=str1;
           p=str2;
         p++;
         q--;        
    }
    putchar('\0');
    return str1;
}

 

 

 

Procedural analysis:

This program I card for a long time: make the end character empty; I got the answer through Baidu and tutorials; first, add a header file in front: # include < conio. h >; putup ('\ 0'). But there was a nauseating mistake: I added a "while" after it; "I checked for a long time, by eliminating the other functions first; checked the wrong functions, checked them out;

 

 

8.3.4 Exercise:

(1) Define an integer one-dimensional array with elements of any input array containing odd and even numbers.
(2) Define a function to arrange the odd number of array elements on the left and even number on the right.
(3) No new arrays are allowed in the function defined by the appeal.
(4) Call the above functions from the main function and print out the results.

 

 

Operation results:

Experimental code:

# include <stdio.h>
#define N 10
void arrsort(int a[],int n);
int main()
{
    int a[N],i;
    printf("input:");
    for(i=0;i<N;i++)
    scanf("%d",&a[i]);
    arrsort(a,N);
    printf("output:");
    for(i=0;i<N;i++)
    printf("%d ",a[i]);
}
void arrsort(int a[],int n)
{
    int *p,*q,temp;
    p=a;
    q=a+n-1;
    while(p<q)
    {
        while(*p%2==1)
        p++;
        while(*q%2==0)
        q--;
        if(p>q)
        break;
        temp=*p;
        *p=*q;
        *q=temp;
        p++;
        q--;   
    }
}

 

 

 

 

Procedural analysis:

Writing this program, there is no problem, but it has not been able to run; later found that: N==10, to enter 10 numbers is useful.

 

 

 

III. EXPERIMENTAL SUMMARY

Harvest:

1. Learn the pointer, through the experiment to have a further understanding of the pointer, pointer is an address, in the output does not need to use the address operator &;

2. A function can be expressed in several ways, such as emptying the end character: adding a header file conio, putchar (' 0') in the main function, or NULL;

3. In learning, we should deepen our understanding and impression of the topic with the students'seminars in class, and communicate more with the students and teachers in class.

4. When you are checking for errors, the understanding of the code will also be helpful. If you have doubts, verify first, and think about why.

 

Insufficient:

1. In the experiment, we do not have enough care and often make some habitual mistakes: adding addressing operators after scanf when eg is output, adding ";" when a sentence is written;

2. There are few exchanges with classmates in peacetime, so we don't often think about many ways to achieve an effect.

3. There is too little training after class. If you write several questions before the experiment, you will have immediate effect and complete the experiment faster.

4. When checking the program, I did not look at the problem with a heart of correcting errors, but felt that my code must be correct, but the fact is a pile of errors.

Posted by jwhite68 on Thu, 13 Jun 2019 11:06:24 -0700