0% found this document useful (0 votes)
20 views

Arrays C++

The document contains C++ code examples for performing common array operations: calculating the sum and average of elements, finding the maximum and minimum value, searching for a specific element, sorting the elements, and separating even and odd numbers. Each example defines an array of integers, performs the relevant operation (e.g. looping through the array and adding elements), and prints the output.

Uploaded by

Seeri Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Arrays C++

The document contains C++ code examples for performing common array operations: calculating the sum and average of elements, finding the maximum and minimum value, searching for a specific element, sorting the elements, and separating even and odd numbers. Each example defines an array of integers, performs the relevant operation (e.g. looping through the array and adding elements), and prints the output.

Uploaded by

Seeri Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sum and average in arrays

#include <iostream>

using namespace std;

int main()

int sum;

int array[10]={23,45,67,78,98,43,68,87,90,78};

sum=array[0];

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

sum=sum+array[i];

cout << sum<<endl;

float average;

average=sum/10;

cout << average<<endl;

Maximum number in arrays


#include <iostream>

using namespace std;

int main()

int max;

int array[10]={23,45,67,78,98,43,68,87,90,78};

max=array[0];

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

if(array[i]>max)
max=array[i];

cout << max;

Minimum number in arrays:


#include <iostream>

using namespace std;

int main()

int min;

int array[10]={23,45,67,78,98,43,68,87,90,78};

min=array[0];

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

if(array[i]<min)

min=array[i];

cout << min;

Searching in arrays:
#include <iostream>

using namespace std;

int main()

int number;

int k=0;

int array[10]={23,45,67,78,98,43,68,87,90,78};

cout << "enter a number to search in array"<< endl;


cin >> number;

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

if(array[i]==number)

k=1;

if(k==1)

cout << "number found "<< endl;

else

cout << "number not found " << endl;

Sorting in arrays:
#include <iostream>

using namespace std;

int main()

int i, j;

int temp;

int array[10]={10,97,58,37,56,25,34,13,22,11};

cout << "unsorted array" << endl;

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

cout << array[i]<<endl;

// sorting

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

for(int j=i+1; j<10; j++){


if(array[i]>array[j]){

temp=array[i];

array[i]=array[j];

array[j]=temp;

cout << "sorted array"<< endl;

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

cout <<array[i] << endl;

Even and odd numbers in arrays:


#include <iostream>

using namespace std;

int main()

int array[10]={10,97,58,37,56,25,34,13,22,11};

cout << "even numbers in array" << endl;

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

if (array[i]%2==0)

cout << array[i] << endl;

}
cout << "odd numbers in array" << endl;

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

if (array[i]%2==1)

cout << array[i] << endl;

You might also like