c++ fourth check-in (array from entry to debut)


foreword

For example: With the continuous development of artificial intelligence, the technology of machine learning is becoming more and more important. Many people have started to learn machine learning. This article introduces the basic content of machine learning.

Tip: The following is the text of this article, and the following cases are for reference

1. Knowledge points of arrays

  1. Array declaration: type array name [size]
    _______________________ length size-1 : [0,1…size-1]

    #For example: int a [3] ;
    __________ char b [5] ;

  2. Array initialization__can
    indicate size, or not.
    In C++, arrays out of bounds will not report errors, please check carefully

#For example: int a[5] = {1,3,5,7,9};
#Or: int b[ ] = {2,4,6,8,10};

  1. The array subscript variable ( a [n] )
    subscript variable can be assigned separately ,
    can be directly calculated , and
    can be input by cin>>, and output by cout<< .
#include<iostream>
using namespace std;
main()
{
    
    
	int a[5],i,sum;
	double avg;
	for(i=0;i<=5;i++){
    
    
		cout<<"a["<<i<<"]=";
		cin>>a[i];
	}
	cout<<endl;
	
	for(i=0;i<=5;i++){
    
    
		cout<<"a["<<i<<"]=";
		cout<<a[i]<<"\t"; 
	} 
	
	for(i=0;i<=5;i++){
    
    
		sum+=a[i]; 
	} 
	cout<<endl<<"sum="<<sum<<endl;
	
	avg=sum/5;
	cout<<"avg="<<avg<<endl;
 } 

Note: ' ' single quotes and " " double quotes have different purposes, " " double quotes represent characters, ' ' single quotes will only output garbled characters!
insert image description here

2. Array sorting and searching

1. Traversal method to find the maximum value

code show as below:

#include<iostream>
using namespace std;
main(){
    
    
int a[8]={
    
    16,47,29,65,83,56};
int max=0,i;
for(i=0;i<8;i++){
    
    
	if(a[max]<a[i]) 
	  max=i;
}	
cout<<"max="<<a[max];
}

2. Dichotomy to find elements

The code is as follows (example):

#include<iostream>//有问题的代码
using namespace std;
main()
{
    
    //程序功能 :排序并查找元素 
  int a[5],low,high,mid;
  int i,cir,count;
  	
  for(i=0;i<5;i++){
    
    
	cout<<"a["<<i+1<<"]=";
	cin>>a[i];//输入 
  }	
  
  while(count>=10)
  {
    
    
  	for(i=1;i<5;i++)
	  {
    
    
  		if(a[i]<a[i-1])
		  {
    
    
  			cout<<a[i]<<"<"<<a[i-1]<<endl;
  			cir=a[i+1];
			a[i+1]=a[i];
  			a[i]=cir;
  			cout<<endl<<"i="<<i<<endl;
  			break;
		  } 
		  count+=1; 
	  }
	  cout<<endl<<"i="<<i<<endl;
  }
  cout<<"a[0]="<<a[0]<<endl;
  cout<<"a[1]="<<a[1]<<endl;
  cout<<"a[2]="<<a[2]<<endl;
  cout<<"a[3]="<<a[3]<<endl;
  cout<<"a[4]="<<a[4]<<endl;
}

After continuous hard work and debugging, we wrote the correct code!

#include<iostream>
#define size 5
using namespace std;
main()
{
    
    
 int a[5];
	
 //功能1:数组数据的输入和输出 
  for(int i=0;i<5;i++)
   {
    
    
	cout<<"a["<<i<<"]:\n";
    cin>>a[i];
   }
  cout<<"a:";
  for(int i=0;i<5;i++)
	  cout<<i<<":\t"<<a[i]<<endl ;
	    
	//功能2:排序(从小到大)
	int t;//t为中间变量 
  for(int i=0;i<4;i++)
   {
    
    
  	for(int j=i+1;j<5;j++)
  	 {
    
    
  	  if(a[j]<a[i])
		{
    
    
		 cout<<"a[j]="<<a[j]<<"\t";
		 cout<<"a[i]="<<a[i]<<endl;
		 //记住:是将后面的直赋值给前面,a=2 
		 t=a[j];
		 a[j]=a[i];
		 a[i]=t;
		 	
		 cout<<"change a[j]="<<a[j]<<"\t";
		 cout<<"change a[i]="<<a[i]<<endl;
			}	
	  }
    }	 
   for(int i=0;i<5;i++)
	cout<<i<<":\t"<<a[i]<<endl ;
	
 //排序完毕,开始二分查找
 int low,high,mid;
 int value,find;
 low=0,high=size;
 mid=(high+low)/2;
 
 cout<<"value=";
 cin>>value;
 
 while(low<=high&&find==0)
  {
    
    
   	mid=(high+low)/2;
   	
   	cout<<endl<<"high=\t"<<high<<endl;
   	cout<<"low=\t"<<low<<endl;
   	cout<<"mid=\t"<<mid<<endl;
   	cout<<"a[mid]\t"<<a[mid]<<endl;
   	cout<<"value=\t"<<value<<endl;
   	cout<<"find=\t"<<find<<endl;
   	if(a[mid]==value)
   	find=1;
   	
   	else if(a[mid]>value)
   	 {
    
    
   	  high=mid-1;
	 }
	 
	else if(a[mid]<value)
	 {
    
    
	 	low=mid+1;
	  } 
  } 
   
  if(find==1)
    cout<<"找到了!目标值在"<<mid<<"位上\n";
  else
    cout<<"找不到QAQ"<<"\n";  
	  
}

Summarize

Here is a summary of the article:
This time we will get started with arrays, because binary search is an algorithm we encountered. So I hope everyone can rely on their own strength to write it again after class (because binary search is really difficult, QAQ)
The content of the next check-in: pointer variables. Stay tuned!

Guess you like

Origin blog.csdn.net/m0_53590279/article/details/115464210