[introduction to C language] Note 8

Keywords: C Back-end

Exercise 7-8

The square matrix moves to the right. Read in two positive integers m and n (1 ≤ n ≤ 6), then read in the n-order square matrix a, and move each element in the square matrix to the right by m positions, that is, columns 0, 1,..., n-1 are transposed into columns N-M, n-m+1,..., n-1, 0, 1,..., n-m-1. The moved square matrix can be stored in another two-dimensional array.

#include <stdio.h>
int main(){
int i,j,k,m,n,temp;
printf("Enter m,n:");
scanf("%d %d",&m,&n);
int a[n][n];
int b[n][n]; //Another two-dimensional array
for(i=0;i<=n-1;i++){ //Input array
for(j=0;j<=n-1;j++){
scanf("%d",&a[i][j]);
}
}
if(m>n){
m=m%n;
}
printf("Result:\n");
for(i=0;i<=n-1;i++){
for(j=n-m;j<=n-1;j++){ //Do it first and then part
b[i][j-(n-m)]=a[i][j];
}
for(j=0;j<n-m;j++){ //Do the later part
b[i][j+m]=a[i][j];
}
}
for(i=0;i<=n-1;i++){
for(j=0;j<=n-1;j++){
printf("%3d",b[i][j]);
}
printf("\n");
}
return 0;
}

Exercise 7-9

Calculate the number of days. Enter the date (year, month, day), and the output is the day of the year. It is required to call the function day defined in example 7-10_ of_ year(year,month,day).

#include <stdio.h>
int day_of_year(int year,int month,int day);
int main(void){
int year,month,day;
printf("Enter y m d:");
scanf("%d %d %d",&year,&month,&day);
printf("%d",day_of_year(year,month,day));
return 0;
}
int day_of_year(int year,int month,int day){
int k, leap;
int tab[2][13]={
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
leap=(year%4==0 && year&100!=0 || year%400==0);
for(k=1;k<month;k++){
day=day+tab[leap][k];
}
return day;
}

7.3

7.3.2 one dimensional character array

One dimensional character array is used to store character data. Its definition, initialization and reference are similar to other types.

For example:

char t[5]={'H','a','p','p','y'};

The integer 0 represents' \ 0 ', that is, the character with ASCII code of 0. For example:

char t[6]={'H','a','p','p','y','\0'};
/* or */
char t[6]={'H','a','p','p','y',0};

7.3.3 string

A string constant is a character sequence enclosed in a pair of double quotation marks, that is, a string of characters with an end flag '\ 0'. For example, the string "Happy" consists of six characters, namely 'H', 'a', 'p', 'p', 'y' and '\ 0', of which the first five are valid characters of the string and '\ 0' is the string terminator.

The effective length of a string is the number of effective characters. For example, the effective length of "Happy" is 5.

Character array initialization can also use string constants, such as:

char t[6]="Happy";
/* or */
char t[6]={"Happy"};

When a string is stored in a character array, because it has a terminator '\ 0', the length of the array is at least + 1. For example, the string "Happy" has a valid length of 5 and the length of the array in which it is stored is at least 6

If the length of the array is greater than the effective length of the string + 1, there are other contents in the array in addition to the stored string, that is, the characters only occupy part of the array.

For example:

char t[80]="Happy";

Only initial values are assigned to t[0]~t[5], and other elements are uncertain. The string ends when it meets' \ 0 ', so all strings in front of' \ 0 'and' \ 0 'constitute the string "Happy".

Note: the string consists of valid characters and the string terminator '\ 0'.

Since the number of array elements in an ordinary array is determined, the cycle is generally controlled by subscripts, and the string does not explicitly give the number of valid characters, it is generally determined whether to end the cycle by comparing whether the value of the array element is equal to '\ 0', that is, the end character '\ 0' controls the cycle.

Distinguish between 'a' and 'a':

• The former is a string constant, including two characters' a 'and' \ 0 ', which is stored in a one-dimensional character array; The latter is a character constant with only one character and can be assigned to a character variable.

There are some special input conditions. Because the string terminator '\ 0' represents null operation, it cannot be input. Therefore, when inputting a string, it is necessary to set an input terminator in advance to put it after a string of characters.

Exercise 7-10

Finds the specified character. Enter a character, and then enter a string ending with carriage return (less than 80 characters) to find the character in the string. If found, the maximum subscript corresponding to the character in the string will be output, starting from 0; Otherwise, output "Not Found".

#include <stdio.h>
int main(void){
int i=0,j,n,result=0;
char put;
char a[80];
put=getchar(); //Enter the character to find
getchar();
while((a[i]=getchar())!='\n'){ //Input string
i++;
}
a[i]='\0';
for(j=0;j<=i-1;j++){
if(a[j]==put){
result=1;
n=j;
}
}
if(result==1){
printf("Found char: %d",n);
}else{
}
return 0;
}

Exercise 7-11

Reverse sorting of strings. Enter a string ending with carriage return (less than 80 characters), store the string in reverse order, and output the string in reverse order.

#include <stdio.h>
int main(void){
int i=0,j,k;
char a[80],temp;
while((a[i]=getchar())!='\n'){ //Input string
i++;
}
a[i]='\0';
k=i;
for(j=0;j<=i/2-1;j++){ //The number of cycles is determined by the 0~(i/2-1) bit, rather than the number of cycles
temp=a[j];
a[j]=a[k-1];
a[k-1]=temp;
k--;
}
for(j=0;j<=i-1;j++){
printf("%c",a[j]);
}
return 0;
}

Exercise 7

Programming question 1

Select the sort method. Enter a positive integer n (1 < n ≤ 10), then enter n integers, sort them from large to small, and then output.

#include <stdio.h>
int main(void){
int a[10];
int i,j,n,index,temp;
printf("Enter n:");
scanf("%d",&n);
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++){
index=i;
for(j=i+1;j<=n-1;j++){
if(a[index]<a[j]){
index=j;
}
}
temp=a[i];
a[i]=a[index];
a[index]=temp;
}
for(i=0;i<=n-1;i++){
printf("%d ",a[i]);
}
return 0;
}

Programming question 2

Find the number that appears most in a batch of integers. Enter a positive integer n (1 < n ≤ 1000), then enter n integers, analyze each number of each integer, and find the number with the most occurrences. For example, enter three integers 1234, 2345 and 3456, of which 3 and 4 appear the most times, both three times.

#include <stdio.h>
#include <math.h>
int main(void){
int i,j,n,m,num,num2,temp,big_times,index=0;
int a[1000]; //Max. 1000 integers
int b[10]={0,0,0,0,0,0,0,0,0,0}; //Count the number of each number and initialize it to 0
scanf("%d",&n);
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}

for(i=0;i<=n-1;i++){
m=1;
num=a[i];
/*Number of digits of Statistics*/
while(num>=10){
num/=10;
m++;
}
/*Extract each digit and add one to the number*/
temp=a[i]; //First give the value of a[i] to temp
for(j=m;j>0;j--){
num2=temp/pow(10,j-1); //Extract the highest digit first each time
b[num2]++; //Add one to the number
temp-=num2*pow(10,j-1); //Remove the highest bit
}
}

for(i=0;i<=9;i++){ //Find the most digits
for(j=i+1;j<=9;j++){
if(b[index]<b[j]){
index=j;
}
}
big_times=b[index];
}

printf("number ");
for(i=0;i<=9;i++){ //Find the number with the most times
if(b[i]==big_times){
printf("%d ",i);
}
}
printf("Maximum number of occurrences, total %d second",big_times);

return 0;
}

Programming question 3

Judge the triangular matrix. Input a positive integer n (1 ≤ n ≤ 6) and the elements in the n-order square matrix A. if a is an upper triangular matrix, output "YES"; otherwise, output "NO". The upper triangular matrix refers to the matrix whose elements below the main diagonal are 0, and the main diagonal is the connecting line from the upper left corner to the lower right corner of the matrix.

#include <stdio.h>
int main(void){
int i,j,n,result=1;
int a[6][6];
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

for(i=1;i<n;i++){ //From below the diagonal, i.e. from the second line
for(j=0;j<=i-1;j++){
if(a[i][j]!=0){
result=0;
}
}
}

if(result==0){
printf("NO");
}else{
printf("YES");
}

return 0;
}

Programming question 4

Find the sum of the elements of each row of the matrix. Input two positive integers m and n (1 ≤ m ≤ 6, 1 ≤ n ≤ 6), then input the elements in matrix a (M rows and N columns), calculate the sum of each row of elements respectively, and output.

#include <stdio.h>
int main(void){
int a[6][6];
int i,j,m,n,sum;
printf("m=");
scanf("%d",&m);
printf("n=");
scanf("%d",&n);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

for(i=0;i<m;i++){
sum=0;
for(j=0;j<n;j++){
sum+=a[i][j];
}
printf("The first %d The sum of the elements of the row is:%d\n",i+1,sum);
}

return 0;
}

-Chapter 7 temporary end of array-

(because there are still a few questions I haven't done in time, I'll make them up next time) (* ゜゜゜ *)

(. _. )

Compiled from the book C language programming

If there is any error, please correct it!

Posted by parkej60 on Sun, 31 Oct 2021 06:49:25 -0700