# Week 1 programming

Posted by GuitarheadCA on Fri, 18 Feb 2022 04:01:12 +0100

1. Program and implement a function getint that inputs an integer within the specified range. Its complete function prototype is:
int getint(int min, int max);， It is responsible for receiving the user's input, verifying, ensuring that the received must be an integer between min and max ([min, max] interval), and finally returning the integer. If the user's input is illegal, you will be prompted to continue until the input is legal. Write a complete program and test the getInt function you wrote. The interval of [min, max] is known to be .
* * * input prompt * * *: "please enter an integer between [% d...% D]: \ n"
* * * input data format * * *: input data in getint() function with "% d"
* * * output data format * * *: "please enter an integer between [% d...% D]: \ n"
"The integer you entered is:% d\n"
The running example of the program is as follows:
Please enter an integer between [3.. 100]: 0
Please enter an integer between [3.. 100]: 102
Please enter an integer between [3.. 100]: 200
Please enter an integer between [3.. 100]: 36
The integer you entered is: 36

```#include<stdio.h>
int getint(int min, int max);
int main()
{
int a = 3, b = 100;
getint(3, 100);
return 0;
}
int getint(int min, int max)
{
int i;
do
{
//Who can see from the title that there should be a \ n???
scanf("%d", &i);
}
while (i < 3 || i > 100);
printf("The integer you entered is:%d", i);
return 0;
}```

2. Hero card. Xiao Ming is infatuated with collecting all kinds of hero cards in simply noodles. For this reason, he once only ate the snack of simply noodles for a month in a row, but some rare hero cards are really too difficult to collect. Later, a shopping mall conducted a hero card exchange activity. As long as you have three consecutive hero cards, you can exchange any number of hero cards. Xiao Ming wants to know how many hero cards he can exchange at most (the new hero card cannot be exchanged again)
Input prompt: "Input n:", "Input card numbers:"
Input data format: '% d'
Output data format: '% d'
Example of program running results:
Input n:6
Input card numbers:3 1 2 4 4 5
Exchanged:1

```#include<stdio.h>
#include<stdlib.h>
int count = {0};
main()
{
int i, j, k, number, n, temp;
number = 0;
printf("Input n:");
scanf("%d", &n);

int *a;
a = (int *)malloc(n * sizeof(int));
printf("Input card numbers:");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
count[a[i]]++;
}
for (i = 0; i < n; i++)
{
temp = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[temp])
temp = j;
}
if (temp != i)
{
k = a[i];
a[i] = a[temp];
a[temp] = k;
}
}

for (i = 0; i < n; i++)
{
while (count[a[i]] > 0 && count[a[i] + 1] > 0 && count[a[i] + 2] > 0)
{
count[a[i]]--;
count[a[i] + 1]--;
count[a[i] + 2]--;
number++;
}
}
printf("Exchanged:%d", number);
}```

3. If the sum of all different factors (including 1) less than m of a positive integer m is exactly equal to m itself, it is called a perfect number. It refers to some special natural numbers. The sum of all its true factors (i.e. divisors other than itself) is exactly equal to itself.
Note: 1 has no true factor, so it is not a complete number. For example, 6 is a perfect number because 6 = 1 + 2 + 3.
Write a function IsPerfect() to judge the perfect number, and then judge whether the integer entered from the keyboard is a perfect number.
Requirements: write the function to judge the complete number according to the following prototype. If the function returns 0, it means it is not a complete number. If it returns 1, it means it is a complete number.
int IsPerfect(int x);
**The required prompt information is: "Input m:\n"
**The required input format is: "% d"
**The output format required is
"%d is a perfect number\n"
"%d is not a perfect number\n"
Note: pointer, structure, common body, file, goto and enumeration types cannot be used for programming.

```#include<stdio.h>
#include<math.h>
int IsPerfect(int x);
main()
{
int a;
printf("Input m:\n");
scanf("%d", &a);
if (IsPerfect(a))
{
printf("%d is a perfect number\n", a);
}
else
{
printf("%d is not a perfect number\n", a);
}
}
int IsPerfect(int x)
{
int i, t, sum = 1;
t = sqrt(x);
if (x <= 1)
return 0;
for (i = 2; i <= t; i++)
{
if (x % i == 0)
sum += (i + x / i);
}
//If the square root of x is an integer, subtract it once
if (x % t == 0)
sum -= t;
if (sum == x)
return 1;
return 0;
}```

4. Input an integer m arbitrarily. If M is not a prime number, output all its factors excluding 1 and itself from small to large; Otherwise, the relevant prompt information of "no factor, prime number" will be output.
Enter the prompt: "Please enter a number:"
Input format: '% d'
Output format:
When there is a factor: "% d\n"
"It is a prime number. No director! \ n"
"It is not a prime number. No director! \ n"

```#include<stdio.h>
int IsPerfect(int x);
int main()
{
int i, a, sum = 0;
printf("Input n:");
scanf("%d", &a);
for (i = 2; i <= a; i++)
{
if (IsPerfect(i))
{
sum += i;
}
}
printf("sum = %d\n", sum);
}

int IsPerfect(int x)
{
int i;
for (i = 2; i < x; i++)
{
if (x % i == 0)
return 0;
}
return 1;
}```

5. Matrix multiplication. Write A C function to realize the product of m-row k-column matrix and k-row n-column matrix. Let A be the matrix of M rows and K columns, and B be the matrix of K rows and N columns, then C=A × The product of B is A matrix of M rows and N columns.
The rule of matrix multiplication is: let A[m,k], B[k,n], then C[m,n]=A[m,k] × B[k,n], where:
C[i,j]=∑kl=1A[i,l]×B[l,j], (i=1,2,...,m   j=1,2,...,n)
**Output format requirements: "\ t%d" "array A=\n" "array B=\n" "array C=\n"
Examples of program operation are as follows:
array A=
1    2    3
4    5    6
array B=
1    1
0    2
2    0
array C=
7    5
16    14

```#include<stdio.h>
main()
{
int a={{1,2,3},{4,5,6}};
int b={{1,1},{0,2},{2,0}};
int c={0};
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}

printf("array A=\n");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}

printf("array B=\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}

printf("array C=\n");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
}```

Topics: C