Second experiment Report of C language

Source: Internet
Author: User

A Experimental topics, design ideas, implementation methods

1. Experimental topics

11-4 judging the upper triangular matrix (15 points)

The upper triangular matrix refers to a matrix of 0 elements below the main diagonal, which is the line from the upper-left corner of the matrix to the lower-right corner.

The subject asks to write a program to determine whether a given phalanx is on a triangular matrix.

12-3 Reverse string (15 points)

Enter a string, reverse the string, and output the string after the reverse order.

13-6 Array Loop Right shift (20 points)

The subject requires the realization of a simple function that loops the right of an array.A is stored inN>) integer to move each integer loop to the rightM≥) a position that is about toThe data in a is determined by (A?0??A?1???A?N?1?? ) Transformation to (A?N?M???A?N?1??A?0??A?1???a? n? M? 1?? ) (Last m number of loops moved to the front < span class= "Strut" > M position).

14-5 specifying the position output string (20 points)

The subject requires implementing a function that prints out all the characters in a given string from the position that matches the first character to the position that matches the second character, given a string and two characters.

15-8 Decimal Conversion binary (15 points)

The subject requires implementing a function that converts a positive integer n to a binary post-output.

2. Design ideas:

11-4: Judging the upper triangular matrix:

In two cycles, the outer loop is used to determine the number of rows, and the inner loop is used to see if the indicated element is 0. Starting from line No. 0, the first element of each row must be 0, and when it is not 0 o'clock, it jumps out of the loop. And judge that this is not an upper triangular matrix.

12-3: Reverse string:
Iterate forward from the end of the string, outputting characters.

13-6: Array loop right Shift:

The direct way to move right is too cumbersome, so choose to create a new array, the data position after the displacement is m%n, according to the characteristics of the loop, the data into a new array.

14-5: output string for specific location:

Sets a pointer at the beginning of S, whenever s! When =ch1, the pointer points to the next element. If s==ch1, the pointer is counted once each time it is moved to null or CH2. At the end of the program, the pointer subtracts the number of counts.

15.8: Decimal Turn binary:
Through the non-stop/2, and then reverse the principle of output, the use of recursive method to complete the program.

Two SOURCE program

11-4: Judging the upper triangular matrix:

#include <stdio.h>#include<string.h>intMain () {intT,n,count=0, c=0; inti,j,l; inta[ -][ -]; Chars[Ten][ the]; scanf ("%d",&t);  for(i=0; i<t;i++) {Count=0; scanf ("%d",&N);  for(j=0; j<n;j++)        {             for(l=0; l<n;l++) scanf ("%d",&A[j][l]); }         for(j=1; j<n;j++)        {             for(l=0; l<j;l++)            {            if(a[j][l]!=0) Count=1; }        }        if(count==0) strcpy (S[c],"YES"); Elsestrcpy (S[c],"NO"); C++; }     for(i=0; i<t;i++) printf ("%s\n", S[i]); return 0;}

12-3: Reverse string:

#include <stdio.h>#include<string.h>intMain () {intI=0, count=0; Chara[Bayi],ch;        Gets (a);  for(I=strlen (a)-1; i>=0; i--) {printf ("%c", A[i]); }        return 0;}
13-6 Array Loop Right Shift:
intArrayshift (intA[],intNintm) {    intb[n],d,i,j=0; D=m%N;  for(i=n-d;i<n;i++) {B[j]=A[i]; J++; }     for(i=0; i<n-d+1; i++) {B[j]=A[i]; J++; }     for(i=0; i<n;i++) {A[i]=B[i]; }    }
14-5 Specify the location output string:
Char*match (Char*s,CharCH1,CharCH2) {    intm=0;  while(*s!=ch1&&*s!=0) s++;  while(*s!=ch2&&*s!=0) {printf ("%c",*s); S++; M++; }    if(*s==CH2) printf ("%c\n",*s); Elseprintf ("\ n"); S-=m; returns;}

15.8: Decimal Turn binary:

voidDectobin (intN) {    intI=N; if(n<2) printf ("%d", N); Else    {        if(n>=2) {dectobin (n/2); printf ("%d", n%2); }    }}

Three Problems encountered and solutions, experience


The first contact to the pointer and recursion when some understand the principle of the, read the book is smattering. Only in the process of writing a lot of programs, I gradually understand the principle. Programming is a subject that needs to be actually manipulated, practice makes perfect.

Second experiment Report of C language

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.