[C Language] Use arrays, and C Language Use Arrays

Source: Internet
Author: User

[C Language] Use arrays, and C Language Use Arrays

-----------------------------------------------------------------------------

// Main. c example // Created by weichen on 15/1/9. // Copyright (c) 2015 weichen. All rights reserved.
# Include <stdio. h> int main (){
// Calculate the input average int x = 0; // The input number double num = 0; // The sum (this is defined as double, because floating point numbers may appear in the calculation result) int count = 0; // number of double per; // average (the result also defines double) printf ("Enter some numbers:"); scanf ("% d ", & x); // execute cumulatively when the value is greater than 0. Enter a value smaller than or equal to 0 and press enter to terminate the loop. Then, execute the following code while (x> 0) {num + = x; count ++; scanf ("% d", & x) ;}if (count> 0) {per = num/count; printf ("% f \ n", per);} return 0 ;}

 

// Main. c example // Created by weichen on 15/1/14. // Copyright (c) 2015 weichen. All rights reserved.
# Include <stdio. h> int main () {// enter a number, and output the average value and the number greater than the average value. // key point: you need to store the input numbers int x = 0; int n = 0; double total = 0; double per = 0; int number [100]; // 1. define the array printf ("Enter some numbers:"); scanf ("% d", & x); while (x! = 0) {number [n] = x; // 2. array Element assignment total + = x; n ++;} if (n> 0) {per = total/n; printf ("average: % f \ n ", per); printf ("the number greater than the average is:"); for (int I = 0; I <= n; I ++) {if (number [n]> per) // 3. use the array element {printf ("% d \ t", number [I]); // 4. traversing the array }}return 0 ;}

Note: After compiling in Xcode, enter the number and press Enter. line number [n] = x returns the Thread1: EXC_BAD_ACCESS (code = 2, address = 0x7fff5fc00000) error. Does anyone know the cause?

 

// Main. c example // Created by weichen on 15/1/19 // Copyright (c) 2015 weichen. All rights reserved.
# Include <stdio. h> int main (){
// Enter a number in the range of 0-9 to calculate the number of times each number is entered
Const int number = 9; // 1. array size int x; int count [number]; // 2. defines the array int I; for (I = 0; I <= number; I ++) {count [I] = 0; // 3. initialize the array} printf ("Enter:"); scanf ("% d", & x); while (x! =-1) {if (x> = 0 & x <= 9) {count [x] ++; // 4. array involved in calculation} scanf ("% d", & x) ;}for (I = 0; I <= number; I ++) {printf ("% d: % d \ n ", I, count [I]);} return 0 ;}

 

// Main. c one-dimensional array // Created by weichen on 15/1/28. // Copyright (c) weichen. All rights reserved.
# Include <stdio. h> int main () {/* 1. array integration initialization int a [] = {1, 2, 6, 7, 9}; for (int I = 0; I <5; I ++) {printf ("% d \ t", a [I]);} */
/* 1. declare an array with a length of 12. The first element is assigned a value of 1, and the rest is 0 int a [12] = {1}; for (int I = 0; I <12; I ++) {printf ("% d \ t", a [I]);} * // * 2. do not use loops. initialize the array int a [12] = {0 };*/
/* 2. positioning int a [12] = {[1] = 2, 3, [4] = 9} during Integration initialization }; if [x] is used to locate the unlocated data when initializing the data, the value of other positions following the previous one can be supplemented with 0 or the array size is not given, make compiler operations suitable for initializing sparse arrays {for (int I = 0; I <12; I ++) {printf ("% d \ t", a [I]); // 0 2 3 0 9 0 ....}
}*/
/* 3. array assignment */int a [] = {1, 3}; int length = sizeof (a)/sizeof (a [0]); int B [length]; // 1. the array variable itself cannot be assigned a value, for example, int B [] = a; error // 2. to give the value of an array to another array, you must use traversal, as shown below: for (int I = 0; I <length; I ++) {B [I] = a [I];}
{For (int I = 0; I <length; I ++) {printf ("% d \ t", a [I]); // 0 2 3 0 9 0 ....}}
// Function of traversing the array: // 3. assign values, initialize values, and determine whether the values exist. Count // 4. After the loop is left, I cannot access the array element return 0 as a subscript ;}

 

/*** Thinking question: Find whether a value exists in the array * @ weiChen */# include <stdio. h> int main () {int a [] = {1, 3, 4, 8, 0, 34}; int key; int length = sizeof () /sizeof (a [0]); printf ("enter a number:"); scanf ("% d", & key); int location = search (key,, length); if (location! =-1) {printf ("key: % d exist in array. \ N ", key);} the key: % d does not exist in else {printf (" array. \ N ", key);} return 0;}/*** function: locate the location of the key in the array * @ param key location * @ param a array * @ param length array length * @ return returns the location of the array, otherwise, the system returns-1 ** int search (int key, int a []; int length); */int search (int key, int a [], int length) {for (int I = 0; I <length; I ++) {if (a [I] = key) {return I; break ;}} return-1;
}

 

// Main. c multi-dimensional array // Created by weichen on 15/1/30. // Copyright (c) weichen. All rights reserved.
# Include <stdio. h> int main (int argc, const char * argv []) {// two-dimensional array, three-row five-column matrix int a [3] [5]; for (int I = 0; I <3; I ++) {for (int j = 0; j <3; j ++) {a [I] [j] = I * j ;}} // initialize the two-dimensional array int B [] [5] = {1, 2, 3, 4, 5}, {3, 4, 5, 6, 8 },}; // 1. the number of columns is required, and the number of rows can be measured by the compiler. // 2. one {} for each row, separated by commas // 3. the last comma can exist. There is an old tradition // 4. if omitted, it indicates zero completion // 5. you can also use the location (C99 ONLY) // int c [] [3] ={{ 0}, {1}, {2}; // three rows and three columns, auto-completion for each column // printf ("% d", c [1] [2]); int d [] [3] = {1, 2, 4, 5, 6, 7, 8, 9, 3,}; // initialized to 3 columns, automatically divided into three rows: int I, j, k = 2; for (I = 0; I <3; I ++) {printf ("% d", d [k] [I]); // cyclically Output d [2] [I]} return 0 ;}

 

Link: http://www.cnblogs.com/farwish/p/4212336.html

@ Blackeye poet <www. chenwei. ws>

Related Article

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.