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

CSC404 - Chap3 - Array Seven Algorithms

The document describes a C++ program that applies 7 basic algorithms (sum, average, count, min, max, searching and sorting) to an array of 20 integers. The program declares an array, inputs data from the user, and finds/displays the sum and average, highest/lowest values, numbers divisible by 5, searches for a user-input value, and sorts the array in descending order.
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)
22 views

CSC404 - Chap3 - Array Seven Algorithms

The document describes a C++ program that applies 7 basic algorithms (sum, average, count, min, max, searching and sorting) to an array of 20 integers. The program declares an array, inputs data from the user, and finds/displays the sum and average, highest/lowest values, numbers divisible by 5, searches for a user-input value, and sorts the array in descending order.
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
You are on page 1/ 2

CSC404

Programming II
Topic 3: One-dimensional Array
Below is an example of program that apply 7 basic algorithms (sum, average, count,
min, max, searching and sorting)

Problem statement:
Write a program to declare an array to store 20 integers, input data into the array.
Then, find and display the followings:
i) Sum and average of the integers
ii) Highest integer
iii) Lowest integer
iv) Count the number of integer that divisible by 5
v) Determine whether there exists an integer search by a user, if found display the
message, “yes, the integer is in the list”, if not found, display “not in the array”.
vi) Sort the integers array in descending order.

#include <iostream>
using namespace std;

int main()
{
const int SIZE = 20;
int arrNum[SIZE];

// Read data into array


cout<<“Enter 20 series of integer numbers”<<endl;
for(int a = 0; a < SIZE; a++)
cin>>arrNum[a];

// To calculate and display sum and average


int sum = 0;
float average;

for(int b = 0; b < SIZE; b++)


sum += arrNum[b];

average = sum / SIZE;

cout<<“Sum of the integer numbers: ”<<sum<<endl;


cout<<“Average of the integer numbers: ”<<average<<endl;

// To find highest and lowest integer number


int highest = arrNum[0], lowest = arrNum[0];

for(int c = 1; c < SIZE; c++)


{
if(highest < arrNum[c])
highest = arrNum[c];
if(lowest > arrNum[c])
lowest = arrNum[c];
}
cout<<“Highest integer numbers: ”<<highest<<endl;
cout<<“Lowest integer numbers: ”<<lowest<<endl;
CSC404 Programming II
Topic 3: One-dimensional Array
// To count the number of integer that divisible by 5
int count = 0;

for(int d = 0; d < SIZE; d++)


{
if(arrNum[d] % 5 == 0)
count++;
}
cout<<“Total number of integer that divisible by 5: ”
<<count<<endl;

// To search an integer number


int searchItem;
bool found = false;
int index;

cout<<“Enter an integer number to be search: ”;


cin>>searchItem;

for(int e = 0; e < SIZE; e++)


{
if(arrNum[e] == searchItem)
{
found = true;
index = e;
break;
}
}
if(found == true)
cout<<searchItem<<“ is in the list at index no ”
<<index<<endl;
else
cout<<searchItem<<“ is not in the list.”<<endl;

// To sort descendingly
cout<<“Integer numbers in original order: ”<<endl;
for(int f = 0; f < SIZE; f++)
cout<<arrNum[f]<<“ ”;

int temp;
for(int pass = 1; pass < SIZE; pass++)
for(int g = 0; g < SIZE-1; g++)
if(arrNum[g] < arrNum[g+1])
{
temp = arrNum[g];
arrNum[g] = arrNum[g+1];
arrNum[g+1] = temp;
}

cout<<“\n\n\nInteger number in descending order:”<<endl;


for(int h = 0; h < SIZE; h++)
cout<<arrNum[h]<<“ ”;

return 0;
}

You might also like