1. Preface
In this post we explain2 months to get computer level 2 C language--True Question 12
2. Procedural fill-in-the-blank questions
2.1 Title requirements
2.2 Code provided
#include <>
#define N 3
int fun(int (*a)[N])
{
int i, j, m1, m2, row, colum;
m1 = m2 = 0;
for (i = 0; i < N; i++)
{
j = N - i - 1; m1 += a[i][i]; m2 += a[i][j];
}
if (m1 != m2) return 0;
for (i = 0; i < N; i++) {
/**********found**********/
row = colum = __1__;
for (j = 0; j < N; j++)
{
row += a[i][j]; colum += a[j][i];
}
/**********found**********/
if ((row != colum) __2__(row != m1)) return 0;
}
/**********found**********/
return __3__;
}
main()
{
int x[N][N], i, j;
printf("Enter number for array:\n");
for (i = 0; i < N; i++)
for (j = 0; j < N; j++) scanf("%d", &x[i][j]);
printf("Array:\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++) printf("%3d", x[i][j]);
printf("\n");
}
if (fun(x)) printf("The Array is a magic square.\n");
else printf("The Array isn't a magic square.\n");
getchar();
}
2.3 Problem solving ideas
Fill in the blank at (1):
At the bottom of this statement, anfor
The loop accumulates the rows and columns of the array into therow
cap (a poem)colum
in the file, so you need to initialize both of them to 0 here.
and abovem1 = m2 = 0;
is the same kind of effect, and in practice it can be initialized directly at the time of variable definition, for example:int m1 = 0,m2 = 0;
。
row = colum = 0;
Fill in the blank at (2):
if
In the statement is to return 0, then you need to judge and check whether the sum of the current row is equal to the sum of the current column, or whether the sum of the current row is equal to the sum of the diagonal (phantom square requirements), as long as one of these two is not equal to the other, then you can say that the array is not a phantom square.
where the conditions for the judgment have already been given, and we only need to fill in the middle of the||
, which means that if one of the conditions is met, it will execute thereturn 0;
。
if ((row != colum) || (row != m1)) return 0;
Fill in the blank at (3):
After the previous series of judgments, none of them were executed toreturn 0;
, then it states that the array is a phantom cube, returning1
That's enough.
return 1;
2.4 Code Implementation
Fill in the complete code:
#include <>
#define N 3
int fun(int (*a)[N])
{
int i, j, m1, m2, row, colum;
m1 = m2 = 0;
for (i = 0; i < N; i++)
{
j = N - i - 1; m1 += a[i][i]; m2 += a[i][j];
}
if (m1 != m2) return 0;
for (i = 0; i < N; i++) {
/**********found**********/
row = colum = 0;
for (j = 0; j < N; j++)
{
row += a[i][j]; colum += a[j][i];
}
/**********found**********/
if ((row != colum) || (row != m1)) return 0;
}
/**********found**********/
return 1;
}
main()
{
int x[N][N], i, j;
printf("Enter number for array:\n");
for (i = 0; i < N; i++)
for (j = 0; j < N; j++) scanf("%d", &x[i][j]);
printf("Array:\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++) printf("%3d", x[i][j]);
printf("\n");
}
if (fun(x)) printf("The Array is a magic square.\n");
else printf("The Array isn't a magic square.\n");
getchar();
}
Tip: To ensure that the code works properly, please test and run it in the corresponding topic of the question bank programming environment.
3. Program modification questions
3.1 Title requirements
3.2 Code provided
#include <>
#include <>
#define N 80
int fun(char* s, char* t)
{
int n;
char* p, * r;
n = 0;
while (*s)
{
p = s;
/*********found**********/
r = p;
while (*r)
if (*r == *p) { r++; p++; }
else break;
/*********found**********/
if (*r = 0)
n++;
s++;
}
return n;
}
main()
{
char a[N], b[N]; int m;
printf("\nPlease enter string a : "); gets(a);
printf("\nPlease enter substring b : "); gets(b);
m = fun(a, b);
printf("\nThe result is : m = %d\n", m);
getchar();
}
3.3 Problem solving ideas
Revise (1):
It was executed first.p = s;
And again, it's executed.r = p;
at this timer
be tantamount top
be tantamount tos
The statement below will determine*r
cap (a poem)*p
whether they are equal or not. In the program it does not use the formal parametert
, which means that the program runs and is alwayss
Compare yourself.
Here you need to set thet
address assigned to ther
, in order to achieve the results required by the topic.
r = t;
Amend (2):
There's not much to say here, there's an equal sign missing, just add it.
if (*r == 0)
3.4 Code Implementation
Modified code:
#include <>
#include <>
#define N 80
int fun(char* s, char* t)
{
int n;
char* p, * r;
n = 0;
while (*s)
{
p = s;
/*********found**********/
r = t;
while (*r)
if (*r == *p) { r++; p++; }
else break;
/*********found**********/
if (*r == 0)
n++;
s++;
}
return n;
}
main()
{
char a[N], b[N]; int m;
printf("\nPlease enter string a : "); gets(a);
printf("\nPlease enter substring b : "); gets(b);
m = fun(a, b);
printf("\nThe result is : m = %d\n", m);
getchar();
}
Tip: To ensure that the code works properly, please test and run it in the corresponding topic of the question bank programming environment.
4. Programming questions
4.1 Title requirements
4.2 Code provided
#include <>
#pragma warning (disable:4996)
#define N 5
struct mpow
{
double a;
int t;
};
double fun(struct mpow* x, int n)
{
}
void main()
{
void NONO();
struct mpow x[N] = { 12,0,9,2,23,1,7,2 };
double sum;
sum = fun(x, 4);
printf("sum=%lf\n", sum);
NONO();
}
void NONO()
{/* Please open the file within this function,Input test data,invocations fun function (math.),output data,Close file。 */
FILE* in, * out;
struct mpow x[N];
int i, j;
double sum;
in = fopen("", "r");
out = fopen("", "w");
for (i = 0; i < 10; i++) {
for (j = 0; j < 4; j++) fscanf(in, "%lf,%d,", &x[j].a, &x[j].t);
sum = fun(x, 4);
fprintf(out, "sum=%lf\n", sum);
}
fclose(in);
fclose(out);
}
4.3 Problem Solving Ideas
In math, a power is used to represent the result of an operation in which a number is multiplied by itself a number of times. A power consists of two parts: the base and the exponent.
This everyone in the math have learned, here will not repeat, the following mainly see how to use the code to achieve the function.
We can use two nested loops to find the result of the calculation in the array of structures in turn, with the outer loop beingn
A power, and the inner loop takes the bottom of the powera
travel by oneselft
times, after calculating the sum of a power accumulates tosum
in the program, so that at the end of the traversal you getx
in the array referred ton
The sum of the powers is enough to return it.
4.4 Code Implementation
Fill in the complete code:
#include <>
#pragma warning (disable:4996)
#define N 5
struct mpow
{
double a;
int t;
};
double fun(struct mpow* x, int n)
{
double sum = 0.0, number = 1.0;
int i = 0, j = 0;
for (i = 0; i < n; i++)
{
number = 1.0;
for (j = 0; j < x[i].t; j++)
{
number *= x[i].a;
}
sum += number;
}
return sum;
}
void main()
{
void NONO();
struct mpow x[N] = { 12,0,9,2,23,1,7,2 };
double sum;
sum = fun(x, 4);
printf("sum=%lf\n", sum);
NONO();
}
void NONO()
{/* Please open the file within this function,Input test data,invocations fun function (math.),output data,Close file。 */
FILE* in, * out;
struct mpow x[N];
int i, j;
double sum;
in = fopen("", "r");
out = fopen("", "w");
for (i = 0; i < 10; i++) {
for (j = 0; j < 4; j++) fscanf(in, "%lf,%d,", &x[j].a, &x[j].t);
sum = fun(x, 4);
fprintf(out, "sum=%lf\n", sum);
}
fclose(in);
fclose(out);
}
Tip: To ensure that the code works properly, please test and run it in the corresponding topic of the question bank programming environment.
5. Postscript
This is the end of this blog, if you have questions or suggestions you are welcome to leave them in the comments section.