educoder structure and common body

Keywords: Programming

Program fill in blank 1
Task description
This task: the function of the program is to simulate the election and count the votes of candidates. Please fill in the program so that it can run correctly and get the result.

Relevant knowledge
Relevant knowledge

Programming requirements
Please read the code on the right side carefully, combine relevant knowledge, supplement the code in the prompt area on the right side, and complete the small program of writing the program to fill in the blank (1).

Test instructions
The platform tests the code you write:

Test input:
zhang
zhang
sun
li
yang
zhao
zhang
zhang
zhang
zhang

Expected output:
Voting results:
Name votes
zhang 6
sun 1
li 1
yang 1
zhao 1

#include <stdio.h>
#include <string.h>

struct  person
{
	char name[8];
	int count;
};

int main()
{
	int i,j;
	char name[8];
	struct  person leader[5]={"zhang",0,"sun",0,"li",0,"yang",0,"zhao",0 };

	for(i=1;i<=10;i++)
	{
		/***** Fill in the code on the following line*****/
	  //Enter candidate name
			char n[8] ;
			scanf("%s",&n);
		 	for(j=0;j<5;j++)
		/***** Fill in the code on the following line*****/
		{
				if(strcmp(n, leader[j].name )==0 )       //If the j-th candidate's name is the same as the entered name
			  {
			  	leader[j].count++;	
			  
			  	break;
			  }	
		}
		
	}
	
	printf("Voting results:\n");
	printf("full name      Number of votes\n");
	for(j=0;j<5;j++)
		
		/***** Fill in the code on the following line*****/
		 printf("%-10s%d\n", leader[j].name,leader[j].count        );     //Output the names and votes of each candidate
		 

		 
	return 0;
}

Program fill in blank 2
This task: in a given program, the function of fun() is to return the data of the oldest in the structure array referred to by parameter std as the function value and output it in the main function. Please supplement the program completely so that the program can get correct results. Pay attention not to add or delete lines, nor to change the structure of the program!

Relevant knowledge
Relevant knowledge

Programming requirements
Please read the code on the right side carefully, combine relevant knowledge, supplement the code in the prompt area on the right side, and complete the small program of writing the program to fill in the blank (2).

Test instructions
The platform tests the code you write:

Expected output:
Output the name and age of the maximum age.

Let's start your task. I wish you success!

#include <stdio.h> 
#include<string.h>
typedef struct 
{
	char name[10]; 
	int age; 
}STD;

STD fun(STD std[], int n)
{
	
	STD max; int i;
	
	/***** Fill in the code on the following line*****/
	/*max.name ="m";
	max.age = 0 ;
	*/
//	max= {"m",0};
	strcpy(max.name,"myna");
	max.age=0;


	for(i=1; i<n; i++)
		
	/***** Fill in the code on the following line*****/
	if(max.age<std[i].age          )
		max=std[i];
	

	
	return max;
}
int main( )
{
	STD std[5]={"aaa",17,"bbb",16,"ccc",18,"ddd",17,"eee",15 };
	STD max;
	max=fun(std,5);
	printf("The result:\n");
	
	/***** Fill in the code on the following line*****/
	printf("Name : %s, Age : %d\n",     max.name      ,max.age);
	

	
	return 0;
}

Program fill in blank 3
Task: the records of known students are composed of student number and academic performance. The data of N students has been stored in the array named a structure. The function of fun is to find the student record with the lowest score and return it to the main function through parameter (only one minimum score is specified).

Relevant knowledge
Relevant knowledge

Programming requirements
Please read the code on the right side carefully, combine relevant knowledge, supplement the code in the begin end area, and complete the small program of writing the program to fill in the blank (3).

Test instructions
The platform tests the code you write:

Expected output:
Output the student record with the lowest score.

#include <stdio.h>
#include <string.h>
#define N 10
typedef struct ss
{
	char num[10]; 
	int s; 

} STU;
	
void fun( STU a[], STU *s )
{
	/********** Begin **********/
	strcpy(a[0].num,s->num) ;
	s->s=a[0].s;
	
	for ( int i=1; i< N; i++ )
	{
		if((s->s)>a[i].s)
		{
			strcpy(s->num,a[i].num);
			s->s=a[i].s;
		}
		
	}
	

	/********** End **********/
}


int main ( )
{
	STU a[N]={ {"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77},
				{"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71} }, m ;
	int i;
	
	printf("***** The original data *****\n");
	for ( i=0; i< N; i++ )
		printf("No = %s Mark = %d\n", a[i].num,a[i].s);
	
	fun ( a, &m );
	
	printf ("***** THE  RESULT *****\n");
	printf ("The lowest : %s , %d\n",m.num, m.s);
	
	return 0;
}

Posted by Talon on Wed, 17 Jun 2020 19:17:17 -0700