Shwetha

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Shwetha
    Member
    #include<iostream.h>
    #include<conio.h>
     
    void main()
    {
    
       //Numbers jumbled of range 10 is given.find the missed number i,e 5
    
      //Dont prompt the user to enter the range.Just count the number of numbers 
      //they given and then add a one to it. You will get the range
      
      //in our problem, we are given Array a[] with 9 elements, 
      
      // therefore range is 9+1 = 10
      
      int a[9] = {8,2,6,1,3,7,9,4,10};
     
      int Current_sum = 0,Needed_Sum=0, Range=10, Missed_num; // range = no.of elements in a[] + 1 i,e 9 + 1 = 10
      
      clrscr();
    
      Needed_sum = range(range+1)/2; // Natural numbers sum formula n(n+1)/2 
     
      for(int i=0; i<9; i++)
      
      Current_sum = current_sum + a[i]; //else you can use simply Current_sum += a[i];
     
      Missed_num= Needed_sum-Current_sum;
    
      cout<<"Missed number is: "<<Missed_num;
     
      getch();
     
    } 
    

    1) In the above problem, you are given Random numbers between 1 to 10, with a number missed. Mean you are given only 9 numbers, find the 10th one

    i,e a[9]= 8,2,6,1,3,7,9,4,10

    In real the problem statement is simple as below,

    a[9]= 1,2,3,4,?,6,7,8,9,10

    now numbers are not in random order, so easily your common sense finds the missing number as 5.

    now make the machine to have the same common sense with your logics..

    2) First find the sum of given numbers and name it as Current_sum.

    Current_sum = 8+2+6+1+3+7+9+4+10

    Current_sum = 50

    3) Secondly find the sum of 1 to 10(Range) numbers using natural numbers sum formula n(n+1)/2 and name it as Needed_sum

    here n is range i,e 10

    Needed_sum = 10(10+1)/2

    Needed_sum = 55

    4) Missed_num = Needed_sum – Current_sum

    Missed_num = 55 – 50

    Missed_num = 5

    # Try the same by changing the ‘Range’ and accepting the random numbers from user instead of declaring the Array elements directly.

    # Intially, Normally all will try to sort the given random numbers into linear and then they try to compare each number to find the missed one.
    This method is ok with 10 range, 20 range…what about 100, 1000 range?
    It is very time consuming and inefficient method.
    Think simple…

Viewing 1 post (of 1 total)