C hapter 4 array

Posted by hkay1 on Tue, 21 Jan 2020 15:48:15 +0100

Practical question 1

[problem description]

Program, realize the following functions:

(1) Define two one-dimensional arrays x, y, no more than 50 elements.

(2) Enter k integers from the keyboard into array x.

(3) Calculate the average value ave of data in x and the number of elements greater than the average value n and output.

(4) Copy the data in array x to array y, store the repeated data only once, and output the data in Y finally.
    
[input form]

Input two lines, the first line is the number of data input, the second line is several integers, each integer is followed by a space for separation;

[output form]

There are several integers in the first line, each integer is followed by a space, and the last space is followed by a new line; there are several integers in the second line, each integer is followed by a space, and the last space does not need to be followed by a new line;
[sample input]

6
6 3 4 3 2 9

[sample output]

4.5 2
6 3 4 2 9

[Abstract]

#include<iostream>
using namespace std;
int main()
{
	int x[50],y[50];
	int i,j,sum=0,n=0,m=0;
	float avg=0;
	int k;
	cin>>k;
	for(i=0;i<k;i++)
	{
		cin>>x[i];
		sum+=x[i];
	}
	avg=(float)sum/k;
	for(j=0;j<k;j++)
	{
		if(x[j]>avg)
		n++;
	}
	for(i=1,y[0]=x[0],m=1;i!=k;i++)
	{
	  	for(j=0;j<i;j++)
		{ 
			if(x[i]==x[j])
	  		break;
   		}		
		if(j==i)
		{
	    	y[m]=x[i];
			m++;
		}	
	}
	cout<<avg<<" "<<n<<endl;
    for(i=0;i<m;i++)
    cout<<y[i]<<" ";	
}

Practical question 2

[problem description]

There are 12 people sitting in a circle to play the counting game. The number of people who report to k will be eliminated from the game starting from the number 1 person. Then the number of people who report to k will be eliminated from the next person who report to k from the number 1 person who still reports to k from the next person who is eliminated from the game in a clockwise direction. This continues until only one person is left. Please write a program to output the number of the last person left.

Be careful:
(1) Suppose that the number of the participants is 1 to 12 in a clockwise direction, and the array can be used to store the data;

(2) K > 1, specified by the user through cin input.

[input form]

Enter an integer to represent the reported value;

[output form]

Output an integer, that is, the number of the last remaining person;

[sample input]

3

[sample output]

10

[Abstract]

#include <iostream>
using namespace std;
int main()
{
	int k;
	int i=0,n=12,t=0;
	cin>>k;
	int a[13];
	for(i=0;i<13;i++)
	{
		a[i]=i;
	}
	  for(i=0,t=1;i<=k;t++)
    {
	    if(t>=13)
	    t=1;
		if(a[t]==0)
	    continue;
		i++;
		if(i==k)
	    {
		a[t]=0;
	    n--;
	    i=0;
	    }
		if(n==1)
	     break;
	 }
	for(i=1;i<13;i++)
	if(a[i])
	cout<<a[i]<<endl;
}

Practical question 3

[problem description]

Xiao Zong wants to know the number of days between two dates. He wants to have a date calculator, which can automatically calculate the number of days between two dates after inputting them.
Requirements: design the corresponding function to calculate the completion days, and verify the correctness in the main function.

[input form]

Enter two dates in the order of MM DD YY, separated by a space;

[output form]

Output the number of days between two dates, i.e. an integer, after which no line wrapping is required;

[sample input]

2016 3 6
2017 1 1

[sample output]

301

[Abstract]

#include<iostream>
#include<cmath>
using namespace std;
bool fun(int year)
{
	if((year%4==0)&&(year%100!=0))
	return true;
	else
	return false;
}
long int past(int year)
{
	int p=0,q=0,i;
	for(i=0;i<year;i++)
	if(fun(i))
	p++;
	else
	q++; 
	return (366*p+365*q); 
}

int now(int year,int month,int day)
{
	int ans=0;
	if(fun(year))
	{
		switch(month)
		{
			case 1:ans=day;break;
			case 2:ans=31+day;break;
			case 3:ans=31+29+day;break;
			case 4:ans=31+29+31+day;break;
			case 5:ans=31+29+31+30+day;break;
			case 6:ans=31+29+31+30+31+day;break;
			case 7:ans=31+29+31+30+31+30+day;break;
			case 8:ans=31+29+31+30+31+30+31+day;break;
			case 9:ans=31+29+31+30+31+30+31+31+day;break;
			case 10:ans=31+29+31+30+31+30+31+31+30+day;break;
			case 11:ans=31+29+31+30+31+30+31+31+30+31+day;break;
			case 12:ans=31+29+31+30+31+30+31+31+30+31+30+day;break;
		}
	}
	else
	{
		switch(month)
		{
			case 1:ans=day;break;
			case 2:ans=31+day;break;
			case 3:ans=31+28+day;break;
			case 4:ans=31+28+31+day;break;
			case 5:ans=31+28+31+30+day;break;
			case 6:ans=31+28+31+30+31+day;break;
			case 7:ans=31+28+31+30+31+30+day;break;
			case 8:ans=31+28+31+30+31+30+31+day;break;
			case 9:ans=31+28+31+30+31+30+31+31+day;break;
			case 10:ans=31+28+31+30+31+30+31+31+30+day;break;
			case 11:ans=31+28+31+30+31+30+31+31+30+31+day;break;
			case 12:ans=31+28+31+30+31+30+31+31+30+31+30+day;break;
		}
	}
	return ans;
}
int main()
{
	int a,b,c,p,q,r;
	cin>>a>>b>>c>>p>>q>>r;
	cout<<abs(past(a)+now(a,b,c)-past(p)-now(p,q,r));
}

Practical question 4

[problem description]

For integer arrays a[10] and b[10], the following tasks are accomplished by programming:

(1) Input values for two arrays from the keyboard by the user;

(2) Find the maximum and minimum values of two arrays;

(3) The integers in arrays a and b are sorted from small to large and from large to small respectively;

(4) Make two ordered arrays a and b into an ordered array c[20] with a length of 20, so that the order of array c is from small to large

[input form]

Input two lines of integers, 10 for each line. The first line is the array in array a, and the second line is the value in array b;

[output form]

Output five lines, the first line has two integers, which are the maximum and minimum values of array a, separated by a space; the second line has two integers, which are the maximum and minimum values of array b, separated by a space; the third line outputs the values of array a in the order of small to large, with a space after each number, and the last one There are also spaces after the numbers; the fourth line outputs the values in array b in the order of large to small, with a space after each number and a space after the last number; the fifth line outputs the values in the combined array c in the order of small to large, with a space after each number and a space after the last number.

[sample input]

2 5 9 1 3 4 0 6 7 8
10 5 25 9 6 3 7 1 2 13

[sample output]

9 0
25 1
0 1 2 3 4 5 6 7 8 9
25 13 10 9 7 6 5 3 2 1
0 1 1 2 2 3 3 4 5 5 6 6 7 7 8 9 9 10 13 25

[Abstract]

#include<iostream>
using namespace std;
int comparemax(int x[])
{
	int max=0,i;
	for(i=0;i<10;i++)
	{
		if(max<x[i])
		max=x[i];
	}
	return max; 

}
int comparemin(int x[])
{
	int min,i;
	for(i=0,min=x[0];i<10;i++)
	{
		if(min>x[i])
		min=x[i];
	}
	return min; 
}
void up(int a[],int n)
{
	
	int i,j,t=0;
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-1-i;j++)
		{
			if(a[j]>a[j+1])
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
			
		}
	}
}
void down(int a[])
{
	int i,j,t=0;
	for(i=0;i<10-1;i++)
	{
		for(j=0;j<10-1-i;j++)
		{
			if(a[j]<a[j+1])
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
			
		}
	}
}
int main()
{
	int a[10],b[10],i,c[20];
	for(i=0;i<10;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<10;i++)
	{
		cin>>b[i];
	}
	up(a,10);
	down(b);
	cout<<comparemax(a)<<" ";
	cout<<comparemin(a)<<endl;
	cout<<comparemax(b)<<" ";
	cout<<comparemin(b)<<endl;
	for(i=0;i<10;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
	for(i=0;i<10;i++)
	cout<<b[i]<<" ";
	cout<<endl;
	for(i=0;i<10;i++)
	c[i]=a[i];
	for(i=10;i<20;i++)
	c[i]=b[i-10];
	up(c,20);
	for(i=0;i<20;i++)
	{
		cout<<c[i]<<" ";
	}
}

Practical question 5

[problem description]

Use the function of cin.getline() to input an English sentence from the keyboard, in which each word is separated by a space, and finally ends with '.'. Count the number of words in the sentence, and output each word in turn. The output number is followed by a new line, and each word is followed by a new line. Note: in this platform, the cin.getline() function is used as follows:

char s[50];

cin.getline(s,50); / / store up to 49 characters

[input form]

Enter an English sentence, in which each word is separated by a space, and the last word is followed by an English "." as the end;

[output form]

Output each word followed by a new line, and the last line outputs the number of words.

[sample input]

I like juice.

[sample output]

I
like
juice
3

(please input and output in strict accordance with the specified format.)

[Abstract]

#include<iostream>
using namespace std;
int main()
{
   int i,num=0;
   char s[50];
   cin.getline(s,50);
   for(i=0;;i++)
   {
   if(s[i]!=32&&s[i]!=46)
   cout<<s[i];
   else
   {
   	num++;
   	cout<<endl;
	}
    
	if(s[i]=='.')
	break;
	}
	cout<<num;
}
Published 6 original articles, praised 0 and visited 12
Private letter follow

Topics: calculator Programming