Array | C++ Array | Methods for Array | Array Function | Bubble Sort

 

C++ Array
C++ Array

C++ Array

Consecutive memory allocation of same data type. C++ Array declare in following way,

Way 1

Datatype nameOfArray[Size]; (without initialize),

Way 2

Datatype nameOfArray[Size] = {} // Empty

Way 3

Datatype nameOfArray[Size] = {value 1, value 2, value 3 - - - value n}

Way 4

Datatype nameOfArray[] = { value 1, value 2, value 3 - - - value n }

Usage

Instead of multiple variable declaration of same data type array are used. Size of array are set in compile time. It has some issue like when data is larger or less than size of array. When data is larger than our requirements are not fulfill, on the other hand when data is smaller than size then extra memory are used by system. It only applicable when we know actual size of data.

Some basic method or operation used in Array

 Pre – requisite Topic is loops. If you are not familiar will loops please check basic loop structure?

I perform some operations in function so, don’t be confused.

Insertion

Assign value to array. It can be in two way. By user or not by user

void insertion(int arr[], int size) {

for (int index = 0; index < size; index++)

{

cout << "Enter number " << (index + 1) << " : ";

            cin >> arr[index];

}

}

Print

Display array values.

void print(int arr[], int size)

{

      for (int index = 0; index < size; index++)

      {

            cout << arr[index] << ",";

      }

      cout << endl;

}

Get minimum value

Get smaller value from array.

int getMin(int list[], int size)

{

      int value = list[0];

      for (int index = 0; index < size; index++)

      {

            if (value > list[index])

                  value = list[index];

      }

      return value;

}

Get maximum value

Get larger number from array.

int getMax(int list[], int size)

{

      int value = list[0];

      for (int index = 0; index < size; index++)

      {

            if (value < list[index])

                  value = list[index];

      }

      return value;

}

Get sum

Get sum of all array values.

int getSum(int list[], int size)

{

      int value = list[0];

      for (int index = 0; index < size; index++)

            value += list[index];

      return value;

}

Get value

Get value from specific index.

int getValue(int index, int arr[], int size)

{

      return (index < size) ? arr[index] : -1;

}

Get index

Get index of specific value.

int getIndex(int value, int arr[], int size)

{

      int i = -1;

      for (int index = 0; index < size; index++)

      {

            if (value == arr[index])

                  i = index;

      }

      return i;

}

Get average

Get average of array value.

double getAvg(int list[], int size)

{

      double value = 0;

      for (int index = 0; index < size; index++)

            value += (double)list[index];

      return value/size;

}

Sorting

Sorting can be two type ascending (smaller to larger) or descending (larger to smaller) order. This also called bubble sort c++.

If desc is true then descending sorting will apply and if false then ascending sorting apply. 

void sort(int arr[], int size, bool desc)

{

      if (desc)

      {

            for (int i = 0; i < size - 1; i++)

                  for (int j = 0; j < size - i - 1; j++)

                        if (arr[j] < arr[j + 1])

                        {

                              int temp = arr[j];

                              arr[j] = arr[j + 1];

                              arr[j + 1] = temp;

                        }

      }

      else

      {

            for (int i = 0; i < size - 1; i++)

                  for (int j = 0; j < size - i - 1; j++)

                        if (arr[j] > arr[j + 1])

                        {

                              int temp = arr[j];

                              arr[j] = arr[j + 1];

                              arr[j + 1] = temp;

                        }

      }

}

 

Main

 

void main()

{

      insertion(numbers, 10);

      print(numbers, 10);

      cout << "Min Value : " << getMin(numbers, 10) << endl;

      cout << "Max Value : " << getMax(numbers, 10) << endl;

      cout << "Sum : " << getSum(numbers, 10) << endl;

      cout << "Avg : " << getAvg(numbers, 10) << endl;

      cout << "Get Value : " << getValue(5, numbers, 10) << endl;

      cout << "Get Index : " << getIndex(getValue(7, numbers, 10), numbers,10) << endl;

      cout << "Descending Sorting \n";

      sort(numbers,10, true);

      print(numbers,10);

      cout << "Ascending  Sorting \n";

      sort(numbers, 10, false);

      print(numbers, 10);

}

Output

C++ Array
C++ Array



0 Comments