0% found this document useful (0 votes)
3 views5 pages

Programming Sec7

The document provides an overview of arrays in programming, explaining their declaration, initialization, and usage. It includes examples of programs that demonstrate adding numbers from an array, searching for a specific number, finding the lowest number, and calculating the average of numbers while identifying those above the average. Key concepts include accessing array elements, initialization lists, and handling array sizes.

Uploaded by

abdelrahamans084
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Programming Sec7

The document provides an overview of arrays in programming, explaining their declaration, initialization, and usage. It includes examples of programs that demonstrate adding numbers from an array, searching for a specific number, finding the lowest number, and calculating the average of numbers while identifying those above the average. Key concepts include accessing array elements, initialization lists, and handling array sizes.

Uploaded by

abdelrahamans084
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array: variable that can store multiple values of the same

type
Values are stored in adjacent memory locations
Declared using [] operator:
int tests[5];
In the definition int tests[5];
int is the data type of the array elements
tests is the name of the array
5, in [5], is the size declarator. It shows the number of
elements in the array.
Array elements can be used as regular variables:
tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
Arrays must be accessed via individual elements:
cout << tests; // not legal
Arrays can be initialized with an initialization list:

int SIZE = 5;
int tests[SIZE] = {79,82,91,77,84};

The values are stored in the array in the order in which they appear in the
list.

The initialization list cannot exceed the array size.

If array is initialized with fewer initial values than the size declarator, the
remaining elements will be set to 0:

Ex1: Write a program to add 10 numbers from array.

int main()

int number[10] = {20,14,6,28,11,13,15,17,4,25};

int sum = 0;

for( int i = 0; i < 10; i++ )

sum += number[i];

cout << “Sum of numbers is " << sum << "\n";


return 0;

Ex2:Write a Program to looking for 8 in array.

int main()

int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};

int f, i, m = 8;

cout << "Enter a number to search: ";

cin >> f;

for (i = 0; (i < m) && (numbers[i] != f); i++) continue;

if (i == m) cout << f << " is not in the list \n";

else cout << f << " is the " << i + 1

<< "th element in the list \n";

return 0;

Ex3: Write a program to find the lowest number from array

int main()

int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};

int min = numbers[0];

for (int i = 1; i < 8; ++i)

if (numbers[i] < min) min = numbers[i];

cout << “Minimum = " << min << endl;


return 0;

Ex4: Write a program to read 10 numbers, calculate the average, then print the
numbers which are greater the average. Print also how many of these numbers are
greater than the average

void main(void)

int k , a[10] , sum , counter ;

float aver ;

sum = 0 ;

cout << "Please enter 10 integer numbers: " ;

for(k = 0 ; k <= 9 ; k++)

cin >> a[k] ;

sum = sum + a[k] ;

aver = sum / 10.0 ;

cout << "Average = " << aver << "\n";

cout << "Numbers greater than average are: ";

counter = 0 ;

for(k = 0 ; k <= 9 ; k++)

if(a[k] > aver)

{
counter++ ;

cout << a[k] << "\t" ;

cout << "\n We have " << counter << "numbers greater than average \n";

You might also like