cs:c_language:array_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/array_1?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1551188115&difftype=sidebyside
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:array_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Array (Example 1) ====== | ||
+ | **Concepts: | ||
+ | Arrays, initialization and utilization | ||
+ | **Text:**\\ | ||
+ | Implement a C program that: | ||
+ | * after the declaration and the initialization of a vector of 10 elements | ||
+ | * finds and prints the first even number and its position in the array | ||
+ | |||
+ | **Solution: | ||
+ | <file C array_1.c> | ||
+ | /* Print the first even number and its position of a 10 elements array/ | ||
+ | |||
+ | #include < | ||
+ | #define SIZE 10 | ||
+ | |||
+ | int main(){ | ||
+ | |||
+ | int vec[SIZE] = {1, 5, 3, 7, 10, 1, 2, 3, 4, 5}; | ||
+ | int i; | ||
+ | int found = 0; | ||
+ | |||
+ | /* First solution */ | ||
+ | for(i=0; i<SIZE && !found; i++){ | ||
+ | if ( vec[i]%2 == 0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* Second solution */ | ||
+ | i = 0; found = 0; | ||
+ | while ( i<SIZE && !found ){ | ||
+ | if ( vec[i]%2==0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | i = i + 1; | ||
+ | } | ||
+ | | ||
+ | if (found){ | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Comments: | ||
+ | The program: | ||
+ | * after the initialization of an array of dimension //SIZE//, directly performed during the declaration: | ||
+ | <code c> | ||
+ | int vec[SIZE] = {1, 5, 3, 7, 10, 1, 2, 3, 4, 5}; | ||
+ | </ | ||
+ | * it scans the array by using a //for// cycle until all the elements of the vector are analyzed (condition // | ||
+ | <code c> | ||
+ | for(i=0; i<SIZE && !found; i++){ | ||
+ | if ( vec[i]%2 == 0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | * the same exercise can be resolved equivalently by using the //while// statement: | ||
+ | <code c> | ||
+ | /* Second solution */ | ||
+ | i = 0; found = 0; | ||
+ | while ( i<SIZE && !found ){ | ||
+ | if ( vec[i]%2==0 ){ | ||
+ | found = 1; | ||
+ | } | ||
+ | i = i + 1; | ||
+ | } | ||
+ | </ | ||
+ | * results are printed at the end of the program. It is important to notice that at the exit of both the //for// and the //while// cycle, the variable //i// is the next position of the first even number. For such reason, to print the first even element of the vector and its position the variables // | ||
+ | <code c> | ||
+ | if (found){ | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/array_1?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1551188115&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/array_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1