Sunday 20 March 2016

Write a program to take 10 integers input from user in an array and display the occurances of distinct element in the array

Leave a Comment
Questions:

write a program to take 10 integers input from user in an array and display the occurrences of distinct element in the array



Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;
#define size 10

void main() {
       int arr_A[size] = { 15,1,2,15,2,1,1,9,2,5 };
       int counter = 0;
       selectionSort(arr_A, size);

       int temp = arr_A[0];
       for (int i = 0; i <= 10; i++) {
              if (temp == arr_A[i]) {
                     temp = arr_A[i];
                     counter++;
              }
              else
              {
                     cout << temp << " occured " << counter << " times" << endl;
                     counter = 1;
                     temp = arr_A[i];
              }
       }
}

//Sorting
void selectionSort(int arr[], int Arr_size)
{
       for (int i = 0; i < size - 1; i++)//we need to do size-2 passes
       {
              int iMin = i;
              for (int j = i + 1; j < size; j++)
              {
                     if (arr[j] < arr[iMin]) {
                           iMin = j;
                     }
              }
              //swaping
              int temp = arr[i];
              arr[i] = arr[iMin];
              arr[iMin] = temp;
       }

}

Output:



Related Articles:

Selection Sort in C++

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: