Advertisement
shopnilSS

AssighnmentOne_1730034_ShahSadamenyYeasarShopnil

Feb 26th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. //Assighnment One
  2. /*Q:1 ===> Count the occurrence of each number in an array. Print the duplicate numbers based on the
  3. occurrence.*/
  4. #include<iostream>
  5. using namespace std;
  6. int main(){
  7.     double myNumberArray[100] , myCountingArray[100];
  8.     int size;
  9.  
  10.     cout << "Enter the size of array "; //declare the size of the array
  11.     cin >> size;
  12.     cout << endl << "Give the input" << endl; //take the input to fill the array
  13.  
  14.     for(int i = 0 ; i < size; i++){
  15.         cin >> myNumberArray[i];//take the input
  16.         myCountingArray[i] = -1 ;//initial value set for counting array
  17.     } //store the array with value
  18.  
  19.     for(int i = 0 ; i < size ; i++){
  20.         int count = 1;//initial count
  21.         for(int j = i + 1 ; j < size ; j++){
  22.             if(myNumberArray[i] == myNumberArray[j] ){
  23.                 count++; //increment the count
  24.                 myCountingArray[j] = 0 ; //if values are equal then fill that index in to 0 to avoid the repeat of counting
  25.             }//check the two index are equal or not
  26.         }
  27.         if(myCountingArray[i] != 0 ){
  28.                 myCountingArray[i] = count;
  29.         }//store the value counting
  30.     } //main loop for counting the duplicate number
  31.  
  32.     for(int i = 0 ; i < size ; i++ ){
  33.         if(myCountingArray[i] != 0 ){
  34.             if( myCountingArray[i] > 1 ){
  35.                 cout << myNumberArray[i] << " occurred " << myCountingArray[i] << " times" << endl;
  36.             } else if( myCountingArray[i] == 1 ) {
  37.                 cout << myNumberArray[i] << " occurred " << myCountingArray[i] << " time" << endl;
  38.             }
  39.         }
  40.     } //print the counting of duplicate number occurrence.
  41. }
  42.  
  43. /*Q:2 Find the largest value in a floating point array.*/
  44. #include <iostream>
  45. using namespace std ;
  46. int main(){
  47.     double myValueArray[100] , myStoreValue;
  48.     int size;
  49.  
  50.     cout << "Enter the size of array "; //declare the size of the array
  51.     cin >> size;
  52.     cout << endl << "Give the input" << endl; //take the input to fill the array
  53.  
  54.     for(int i = 0 ; i < size; i++){
  55.         cin >> myValueArray[i];//take the input
  56.     } //store the array with value
  57.  
  58.     for(int i = 0 ; i < size - 1 ; i++){
  59.         for(int j = i+1 ; j < size ; j++ ){
  60.             if( myValueArray[i] > myValueArray[j] ){
  61.                 myStoreValue = myValueArray[i] ;
  62.                 myValueArray[i] = myValueArray[j];
  63.                 myValueArray[j] = myStoreValue;
  64.             } //swap the value
  65.         } // check all the value with a value
  66.     }
  67.     cout << "Largest Number is: " << myValueArray[size - 1 ]; //print the largest value
  68.  
  69. }
  70.  
  71. /*Q:3 Sort an integer array in ascending order..*/
  72. #include <iostream>
  73. using namespace std ;
  74. int main(){
  75.     double myValueArray[100] , myStoreValue;
  76.     int size;
  77.  
  78.     cout << "Enter the size of array "; //declare the size of the array
  79.     cin >> size;
  80.     cout << endl << "Give the input" << endl; //take the input to fill the array
  81.  
  82.     for(int i = 0 ; i < size; i++){
  83.         cin >> myValueArray[i];//take the input
  84.     } //store the array with value
  85.  
  86.     for(int i = 0 ; i < size - 1 ; i++){
  87.         for(int j = i+1 ; j < size ; j++ ){
  88.             if( myValueArray[i] > myValueArray[j] ){
  89.                 myStoreValue = myValueArray[i] ;
  90.                 myValueArray[i] = myValueArray[j];
  91.                 myValueArray[j] = myStoreValue;
  92.             } //swap the value
  93.         } // check all the value with a value
  94.     }
  95.  
  96.     cout << "Assecnding order ";
  97.     for(int  i = 0 ; i < size ; i++ ){
  98.         cout << myValueArray[i] << " ";
  99.     }//print the array in ascending order sort
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement