fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int getMode (int list[], int arraySize)
  6. {
  7. cout<<"The array you entered is listed below\n "<<list[0];
  8. for(int i=0;i<arraySize;i++)
  9. {cout<<setw(3)<<list[i];}
  10. int count1=0;
  11. int count2=0;
  12. int mode=0;
  13. for(int j=0;j<arraySize;j++)
  14. {
  15. for(int i=0;i<arraySize;i++)
  16. {
  17. if(list[i]==list[j])
  18. {
  19. count1++; //counts the number of instances that the number occurs
  20. }
  21. }
  22. if(count1>count2)
  23. {
  24. mode= list[j];
  25. count2=count1;
  26. }
  27. count1=0;
  28. }
  29. return mode;
  30. }
  31.  
  32. int main()
  33. {
  34. int size;
  35. int *list;
  36. cout<<"Please enter the size of your array: ";
  37. cin>>size;
  38. list=new int[size];
  39. cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
  40. for(int i=0;i<size;i++)
  41. {
  42. cin>>list[i];
  43. }
  44. cout<<endl;
  45.  
  46. int mode=getMode(list,size);
  47. cout<<"\n"<<mode<<endl;
  48. return 0;
  49. }
  50.  
  51.  
Success #stdin #stdout 0s 15240KB
stdin
10
1 3 4 3 5 4 4 10 9 10
stdout
Please enter the size of your array: 
Please enter the numbers in your list seperated by spaces: 
The array you entered is listed below
 1  1  3  4  3  5  4  4 10  9 10
4