# Program to calculate percentile of an array in C++

In this tutorial, you will be learning **how to calculate the percentile of each element of an array in C++**.

The user will enter the size and elements of an array. The percentile of each element in the array is calculated and displayed.

The program is useful to calculate the percentile of a student based on marks where marks are stored as elements in an array. The percentile of the student(s1) is the percentage of the number of students having marks less than the student(s1).

## C++ program to calculate percentile of an array

#### Example:

Let us understand the percentile calculation with a simple example:

let **n** be the number of elements, **a** be an array of size n

a={9, 10, 6, 7} // n=4

Percentile can be calculated using the following formula:

percentile of a[i]=(count*100)/(n-1) where, count=count of the element in array 'a' having value less than a[i]

Using the above formula, we can calculate:

Percentile of 9=(2*100)/3 =66 //count =2, because 6,7 are less than 9 Percentile of 10=(3*100)/3=100 //count=3, because 9,6,7 are less than 10 Percentile of 6=(0*100)/3=0 // count=0, as no elements are less than 6 Percentile of 7=(1*100)/3=33 // count=1, because 6 is less than 7

#### Algorithm:

Step 1: Enter the value of n Step 2: Declare an integer array a of size n and an integer variable count=0 Step 3: Populate the array Step 4: for i->0 to n count=0 for j->0 to n if a[i]>a[j] increment count end for j calculate percentile of a[i] by percentile=(count*100)/(n-1) print percentile end for i

#### Program:

#include<stdio.h> #include<iostream> using namespace std; int main() { int a[10],n,i,j; int percent; int count; cout<<"Enter the value of n(size):\n"; cin>>n; cout<<"Enter the elements of an array:\n"; for(i=0;i<=n-1;i++) { cout<<"a["<<i<<"]"<<"="; cin>>a[i]; } for( i=0;i<n;i++) { count=0; for(j=0;j<n;j++) { if(a[i]>a[j]) count=count+1; } percent=(count*100)/(n-1); cout<<"\nThe percentile of "<<a[i]<<"="<<percent; } }

#### Output:

Enter the value of n(size): 4 Enter the elements of an array: a[0]=9 a[1]=10 a[2]=6 a[3]=7 The percentile of 9=66 The percentile of 10=100 The percentile of 6=0 The percentile of 7=33

Hope, you have understood the program to calculate the percentile of an array. Feel free to comment.

Read more tutorials:

## Leave a Reply