CSC404 - Chap3 - Array Seven Algorithms
CSC404 - Chap3 - Array Seven Algorithms
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];
// 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;
}
return 0;
}