Sunteți pe pagina 1din 3

Lesson 14: Arrays - C Tutorial In C language arrays are very popular.

They can be found in almost any program code and are pretty helpful and easy to understand. To understand them, just visualize a sequence of numbers and try assigning them to only one declaration. It can be done if the declaration is properly stated as an array including index brackets (example int MySequence[20]).

Arrays Array is data structure used to share multiple data elements under a single name declaration. Its important that every single element of data, we wish to assign to array, belongs to the same data type. Arrays elements are easily accessed we use index; a number that must be nonnegative integer (constant, variable, integer expression). Elements index is a number between 0 and the number of elements minus one, including. In short: Index ( [0, NrOfElements 1].

Declaration of an array: data_type array[index];

Arrays definition in C: int x[20] array consisted of 20 integer numbers char symbols[2] array consisted of 2 characters float sequence[MAX] MAX is constant

Assigning elements values definition: int array[ array[0] = array[1] = array[2] = ] = {1, 2, 3}; 1 2 3

int array [4] = {1, 2}; array[0] = 1 array[1] = 2 array[2] = 0 array[3] = 0

Accessing arrays elements: x[0] first element of an array sequence[i] i. element of an array, 0 <= i <= NrOfElements 1 sequence[MAX 1] last element of an array

Common Wrong access to arrays elements: int array[10] = {0}; int x = sequence[10]; float int x int a int x a = = = = 1.; array[a]; 1, b = 0, c = 1; array[(a && b) - c];

Example: Write your own program that asks user to input sequence of numbers, afterwards it calculates arithmetic middle of the given sequence. Program also prints numbers smaller than arithmetic middle, and afterwards prints numbers bigger than arithmetic middle.

#include <stdio.h>#define DIMENSION 10int main(void) {int i; float sum = 0., ar it_midd = 0., sequence[DIMENSION]={0}; for (i = 0; i < DIMENSION; i++) { p rintf("Input number: "); scanf("%f",&sequence[i]); sum += sequence[i]; } arit_midd = sum / DIMENSION; printf("Arithmetic middle of the sequence is %6. 2f.\n", arit_midd); for (i = 0; i < DIMENSION; i++) { if (sequence[i] < arit _midd) { printf("%6.2f is smaller than arithmetic middle.\n", sequence[i]); } } for (i = 0; i < DIMENSION; i++) { if (sequence[i] > arit_midd) { printf("%6.2f is bigger than arithmetic middle.\n", sequence[i]); } } // What happens if the number is equal to arithmetic middle?}

Example: Write your own program that asks for input of sequence of numbers. After the program reads given numbers, it divides every number with the biggest sequence element and shows them in a way relative to the biggest element. #include <stdio.h>#define DIMENSION 10int main(void) { int i; float max, array [DIMENSION]; for (i = 0; i < DIMENSION; i++) { printf("array[%d] = ", i); scanf("%f",& array[i]); if (i == 0) { max = array[i]; } if ( max < array[i]) { max = array[i]; } } printf("Biggest element in arra y is %f.\n\n", max); for (i = 0; i < DIMENSION; i++) { array[i] /= max; p rintf("array[%d] = %f\n", i, array[i]); }}

Example: Compose your own program that reads given natural numbers that belong in [10, 99] interval and counts how many times each number showed up. Program stops reading numbers when element that doesnt belong to interval is given. Afterwards, program prints each number from the interval that has showed at least once, and number of times it has really been given.

#include <stdio.h>#define LL 10 /* lower limit of the interval */#defin e UL 99 /* upper limit of the interval */int main(void) { int number, i; int counter[UL LL + 1] = { 0 }; do { printf("\nInput number from in terval [%d, %d]: ", LL, UL); scanf("%d", &number); if (number >= LL && num ber <= UL) { counter[number - LL]++; } } while (number >= LL && number <= UL); for (i = DG; i <= UL; i++) { if (counter[i - LL] > 0) { printf ("\nNumber %d showed up %d times", i, counter [i - LL]); } }} Technorati Tags: Array, Sequence, Declaration, Void, Brackets, Input, Output

1 Responses to Lesson 14: Arrays zafar on 3:52 AM The first example could have been very simple. int main(void) { int i; float Ary[10]; for (i = 0; i < 10; i++) { Ary[i]=i; } for (i = 0; i < 10; i++) { printf("array[%d] = ", i)}; } }Posting your comment... Leave a Reply Name (required) Website Convert to boldConvert to italicConvert to link Remember me

S-ar putea să vă placă și