C + + pointer exercise

Source: Internet
Author: User

1#include <iostream>2 3 using namespacestd;4 5 //to change the value of a variable in the keynote function through a function call:6 //(1) in the keynote function, the address of the variable or the pointer to the variable as an argument7 //(2) in the modulated function, accept the address of the variable with the pointer type parameter8 //(3) in the modulated function, change the value of the parameter pointed to the variable9 voidSWAP2 (int* x,int*y) {Ten     intT; Onet = *x; A*x = *y; -*y =T; - } the  - //find the maximum number in an array - //first declare a pointer variable and let him point to the address of the first element of the array - //use a For loop to compare the number pointed to by the pointer to each number in the array + //let the pointer point to the maximum number of comparisons after each comparison - //Finally, the pointer to the largest tree in the array is returned . + int* SWAP1 (intAintLen) { A     int* t =A; at      for(inti =1; i < Len;i + +){ -         if(*t<* (A +i)) { -t = a +i; -         } -     } -     returnT; in } -  to //Use the addition operation of the pointer to add all the elements in an array and return the result + //declares a pointer variable and points it to the first element address of array a - //Use the pointer to iterate through all the elements in the array, add to the variable sum, and return the value of the last int type the intSWAP3 (intBintLen) { *     int* p =A; $     intsum =0;Panax Notoginseng      for(inti =0;i<5; i++){ -         //The pointer 's addition operation, the pointer variable is 1 each time, the pointer moves backwards the number of bytes that the pointer type occupies the         //that is, when the pointer variable is 1 each time, the pointer points to the next element of the array +Sum + = *p + +; A     } the     returnsum; + } -  $ //to sort the elements of an array using the Bubbling method $ voidSWAP4 (int* Array,intLen) { -     inttemp =0; -      for(inti =1; i < Len;i + +){ the          for(intj =0; J<len-i;j + +){ -             if(array[j]>array[j+1]){Wuyitemp =Array[j]; theARRAY[J] = array[j+1]; -array[j+1] =temp; Wu             } -         } About     } $ } -  - voidMain () { -     //computes the sum of an array A     intA[] = {1,2,3,4,5}; +     intAsize =sizeof(a)/sizeof(int); the     intsum =swap3 (a,asize); -cout <<"the sum of all elements of array A is:"<< sum <<Endl; $  the     //calculates the maximum number in an array the     intB[] = {2,5, the, -,4,7}; the     intBsize =sizeof(b)/sizeof(int); the     int* t =Swap1 (b,bsize); -cout <<"The maximum number of all elements of array A is:"<< *t <<Endl; in  the     //Exchange values for two variables the     intx =1, y =2; About     int*pa = &x, *PB = &y; thecout <<"x="<<x<<"; y="<<y<<Endl; the swap2 (PA,PB); thecout <<"After calling Swap2:"<<"\ n"<<"x="<<x<<"; y="<<y<<Endl; +  -     //Array Sorting the     intarray[5];Bayicout <<"Please enter a 5 integer"<<Endl; the      for(inti =0;i<5; i++){ theCIN >>Array[i]; -     } -cout <<"before sorting:"; the      for(inti =0; I <5; i + +){ thecout << Array[i] <<"\ t" ; the     } theSWAP4 (Array,5); -cout <<"\ n after sorting:"; the      for(inti =0; I <5; i + +){ thecout << Array[i] <<"\ t" ; the     }94}

C + + pointer exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.