Location>code7788 >text

Computer 2 C in 2 Months - Question (10) Explanation

Popularity:965 ℃/2024-11-08 09:22:45

1. Preamble

In this post we explain2 months to get computer level 2 C language--True Question 10

真题10-程序评分

2. Procedural fill-in-the-blank questions

2.1 Title requirements

真题10-程序填空

2.2 Code provided

#include  <>
#pragma warning (disable:4996)
double  fun(double x[], int n)
{
	int i, k = 0;
	double avg = 0.0, sum = 0.0;
	for (i = 0; i < n; i++)
		avg += x[i];
	/**********************found***********************/
	avg /= ____(1)____;
	for (i = 0; i < n; i++)
		if (x[i] > avg)
		{
			/**********************found***********************/
			____(2)____ += x[i];
			k++;
		}
	/**********************found***********************/
	return  ____(3)____;
}
main()
{
	double score[12] = { 50,60,70,80,90,100,55,65,75,85,95,99 };
	double aa;
	aa = fun(score, 12);
	printf("%f\n", aa);
}

2.3 Problem solving ideas

Fill in the blank at (1):

Before this statement, the program usesforThe loop adds the grades of all students to the variableavgIn this case, what we're trying to accomplish is to find the grade point average, so we need to use theavgIn addition to the number of studentsn, you can get the average grade of all students and assign it to theavg

included among theseavg /= n;equivalenceavg = avg / n;

avg /= n;

Fill in the blank at (2):

go throughif (x[i] > avg)Judgment, coming in after thex[i]They're all above average.avgs, what's being performed here is to set the eligiblex[i]accumulatesum(the program is given), and each time thek++to facilitate subsequent averaging.

sum += x[i];

Fill in the blank at (3):

Here we need to return the average of the grades of the students with higher than average grades, wheresumis the overall performance of students who are above average.kis the number of students with above average scores usingsum / kThe average of the grades of students with higher than average grades can be obtained.

return  (sum / k);

2.4 Code Implementation

Fill in the complete code:

#include  <>
#pragma warning (disable:4996)
double  fun(double x[], int n)
{
	int i, k = 0;
	double avg = 0.0, sum = 0.0;
	for (i = 0; i < n; i++)
		avg += x[i];
	/**********************found***********************/
	avg /= n;
	for (i = 0; i < n; i++)
		if (x[i] > avg)
		{
			/**********************found***********************/
			sum += x[i];
			k++;
		}
	/**********************found***********************/
	return  (sum / k);
}
main()
{
	double score[12] = { 50,60,70,80,90,100,55,65,75,85,95,99 };
	double aa;
	aa = fun(score, 12);
	printf("%f\n", aa);
}

Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.

3. Program modification questions

3.1 Title requirements

真题10-程序修改

3.2 Code provided

#include <>
void  fun(char* s)
{
    int  i, j;
    for (i = 0, j = 0; s[i] != '\0'; i++)
        if (s[i] >= '0' && s[i] <= '9')
            /**********found**********/
            s[j] = s[i];
    /**********found**********/
    s[j] = "\0";
}
main()
{
    char  item[80];
    printf("\nEnter a string  :  "); gets(item);
    printf("\n\nThe  string  is  : \"%s\"\n", item);
    fun(item);
    printf("\n\nThe string of changing is  : \"%s\"\n", item);
    getchar();
}

3.3 Problem solving ideas

Revise (1):

Here you need to form a new string with the removed numeric characters and replace the original string.

using onlys[j]is not possible because in the programjnever changes value and remains at 0, causing the numeric character to be stored each time to thes[0]in the address of the string, and no new string is formed, so we're going to make thej++, thus changing the address of the storage.

s[j++]=s[i];

Amend (2):

""is used to represent strings, whereas here thes[j]can only store a single character, so you need to use the''to bracket it.

Here's tos[j]assign a value to something'\0'is should be for strings that start with'\0'As a coda.

s[j]='\0';

3.4 Code Implementation

Modified code:

#include <>
void  fun(char  *s)
{  int  i,j;
   for(i=0,j=0; s[i]!='\0'; i++)
        if(s[i]>='0' && s[i]<='9')
/**********found**********/
            s[j++]=s[i];
/**********found**********/
        s[j]='\0';
}
main()
{  char  item[80];
   printf("\nEnter a string  :  ");gets(item);
   printf("\n\nThe  string  is  : \"%s\"\n",item);
   fun(item);
   printf("\n\nThe string of changing is  : \"%s\"\n",item );
  getchar();
}

Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.

4. Programming questions

4.1 Title requirements

真题10-程序设计

4.2 Code provided

#include <>
#include <>

void fun(char* s, char t[])
{



}

main()
{
    char s[100], t[100]; void NONO();
    printf("\nPlease enter string S:"); scanf("%s", s);
    fun(s, t); printf("\nTNT", s)
    printf("\nThe result is: %s\n", t);
    NONO();
    getchar();
}

void NONO()
{/* This function is used to open a file, input data, call a function, output data, and close the file. */
    char s[100], t[100];
    FILE* rf, * wf.
    int i;

    rf = fopen("", "r");
    wf = fopen("", "w");
    for (i = 0; i < 10; i++) {
        fscanf(rf, "%s", s);
        fun(s, t).
        fprintf(wf, "%s\n", t);
    }
    fclose(rf);
    fclose(wf);
}

4.3 Problem Solving Ideas

This question is actually quite easy, it's just traversing the array and adding a parity judgment.

Parity judgment has been mentioned before, here is a brief mention: a number divided by 2 to get the remainder equal to 0, is even, equal to 1 is odd.

At the beginning of the function you have to first set thetThe array will be cleared and the pointer will be traversed.sThe ASCII value of the string is an even number, then it is stored in thetin whicht[j++]The role is the same as in the previous question.

The question asks to remove an odd number of characters and then place the remaining characters into thetThe essence is to put an even number of characters into thet, you can operate directly on even numbered characters to reduce the complexity of the program.

4.4 Code Implementation

Fill in the complete code:

#include <>
#include <>

void fun(char* s, char t[])
{
    int i = 0,j = 0;

    for (i = 0; i < (sizeof(t) / sizeof(t[0])); i++) // empty array t to prevent garbage values
    {
        t[i] = 0;
    }

    for (i = 0; i < strlen(s); i++)
    {
        if ((s[i] % 2) == 0) // equal to 0 means even ASCII value.
        {
            t[j++] = s[i]; // save to the array pointed to by t
        }
    }
}

main()
{
    char s[100], t[100]; void NONO();
    printf("\nPlease enter string S:"); scanf("%s", s);
    fun(s, t); printf("\nTNT", s)
    printf("\nThe result is: %s\n", t);
    NONO();
    getchar();
}

void NONO()
{/* This function is used to open a file, input data, call a function, output data, and close the file. */
    char s[100], t[100];
    FILE* rf, * wf.
    int i;

    rf = fopen("", "r");
    wf = fopen("", "w");
    for (i = 0; i < 10; i++) {
        fscanf(rf, "%s", s);
        fun(s, t).
        fprintf(wf, "%s\n", t);
    }
    fclose(rf);
    fclose(wf);
}

Tip: To ensure that the code works correctly, 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.