iOS Learning Notes---C language fourth day

Source: Internet
Author: User
Tags array sort

1 //? Generate 2 arrays, each array has 10 elements, the element value range 20-40, the array corresponding elements added, put in another? An array2#import <Foundation/Foundation.h>3 4 intMainintargcConst Char*argv[])5 {6     7     intnum1[Ten]={0},num2[Ten]={0},num3[Ten]={0};8      for(inti =0; i<Ten; i++) {9Num1[i]=arc4random ()% ++ -;TenNum2[i]=arc4random ()% ++ -; Onenum3[i]=num1[i]+Num2[i]; Aprintf"%d +%d =%d\n", Num1[i],num2[i],num3[i]); -          } -printf"\ n"); the     return 0; -}

One-dimensional array, array sort, character array

One or one-D arrays

Arrays: A set of data that consists of members of the same data type

Only data of the same data type can be placed in an array

Type descriptor array name [constant expression] ={value 1, value 2 ...};

int A[3]={5,3,0};//int [3] is a data type, a is a variable name

Access to array elements: array name + subscript.

Array subscript: The ordinal number of an array element in an array. Starting from 0, to the total number of arrays-1;

The subscript can be a constant or a variable.      int a[5]={2,7,4,3,0}; a[0]=2,a[3]=3;

1 //? An array with 20 elements, each of which has a value range of 30-70, and the array elements. 2#import <Foundation/Foundation.h>3 4 intMainintargcConst Char*argv[])5 {6 7     //array of 20 elements8     intnumber[ -]={0},sum =0;9     //random number Arc4random ()%41+30Ten      for(inti =0; i< -; i++) { OneNumber[i]=arc4random ()% A+ -; Aprintf"%d", Number[i]); -sum = sum +Number[i]; -     } theprintf"\n%d", sum); -     return 0; -}

//? Generate 2 arrays, each array has 10 elements, the element value range 20-40, the array corresponding elements added, put in another? An array#import <Foundation/Foundation.h>intMainintargcConst Char*argv[]) {        intnum1[Ten]={0},num2[Ten]={0},num3[Ten]={0};  for(inti =0; i<Ten; i++) {Num1[i]=arc4random ()% ++ -; Num2[i]=arc4random ()% ++ -; Num3[i]=num1[i]+Num2[i]; printf ("%d +%d =%d\n", Num1[i],num2[i],num3[i]); } printf ("\ n"); return 0;}

Note: The system does not detect whether the subscript of an array element is out of bounds, and must ensure that the array subscript cannot be crossed when programming

An array is a whole and cannot be directly involved in an operation, only a single element can be processed, and a loop is used where the array is usually used.

An array cannot participate in an operation, only a single element.

Second, array sorting

Bubble Sort:

1 //randomly outputs 0-30 of the number and sorts from small to large2#import <Foundation/Foundation.h>3 4 intMainintargcConst Char*argv[])5 {6     7     intnum1[Ten]={0};8      for(inti =0; i<Ten; i++) {9Num1[i]=arc4random ()% to;Tenprintf"%d", Num1[i]); One          A}printf ("\ n"); -      for(inti =0; i<9; i++) { -          for(intj =0; j<9-I.; J + +) { the             if(num1[j]>num1[j+1]) { - //num1[j]=num1[j]^num1[j+1]; - //num1[j+1]=num1[j]^num1[j+1]; - //Num1[j]=num1[j+1]^num1[j]; +                 inttemp =Num1[j]; -NUM1[J] = num1[j+1]; +num1[j+1] =temp; A             } at         } -     } -      for(inti =0; i<9; i++) { -printf"%d", Num1[i]); -     } -     return 0; in}

Three, character array

An array for storing character variables is called a character array

Char c[10]={' i ', ' P ', ' h ', ' o ', ' n ', ' e '}; or char c[10]= "iphone";

An array of characters that have a ' \ s ' End identifier is also known as a string. The system provides a number of string processing functions

Strlen () computes string length strcpy () string copy strcat () string concatenation strcmp () string comparison

String length: The valid length of the string, which does not contain '/'

The space occupied by the string is at least 1 larger than the string length, because the string ends with a ' \ s ', and the string handler provided by the system is based on ' s ' to determine the end of the character array.

1#import <Foundation/Foundation.h>2 3 intMainintargcConst Char*argv[])4 {5     6     Charname[ -]="Zhangsan", name1[ -]="Lisi";7 //printf ("%s length is%lu", Name,strlen (name));//string Length8 //strcpy (name1, name);//string Copy9 //strcat (name, name1);//string concatenation output ZhangsanlisiTen //printf ("%s", name); One     intresult =strcmp (name, name1); Aprintf"%d\n", result); -     if(result>0) { -printf"%s is greater than%s", name,name1); the}Else if(result==0) { -printf"%s equals%s", name,name1); -}Else { -printf"%s is less than%s", name,name1); +     } -  +     return 0; A}

Arrays are usually combined with loops to make use of.

array subscript from 0 start, most big to array capacity -1 .

Be sure to pay attention to the subscript cross-border problem.

An array of strings must be left with an element at the time of definition.

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.