Tuesday 5 November 2013

Sum of n numbers in C++ using Classes!

"Find the sum of n numbers input by the user using classes in C++"
This is an important question which has high probability to be asked in CBSE/Other states Computer Science practical examinations. This question is also important because it uses most of the basic concepts of C++ like iteration, arrays, classes, etc; Hence I have decided to share this program with you. It is fairly simple if you have a little logic and also know about classes in C++.

Here is the code :
/*Support us by subscribing, commenting, and viewing 
the blog frequently! -GeekyCircle Admin :)*/
#include<iostream.h>
#include<conio.h>
class sumNumbers
{
    int counter,num_of_num,num_array[100], sum;
public:
    void input()
    {
       cout<<"How many numbers?    :     ";
       cin>>num_of_num;
       cout<<"\nEnter the numbers : "<<endl;
       //Part one
       for (counter = 0; counter < num_of_num; counter++)
       {
          cin>>num_array[counter];
       }
       cout<<"\nThe numbers given are : "<<endl;
       for (counter = 0; counter < num_of_num; counter++)
       {
          cout<<num_array[counter]<<"\t";
       }
    }
    void process()
    {
        //Part two - This is where the magic happens!
        sum = 0;
        for(counter = 0; counter < num_of_num; counter++)
        {
           sum = sum + num_array[counter];
        }
    }
    void output()
    {
        cout<<"\n\nRequired sum is "<<sum<<endl;
    }
};

void main()
{
      //Part Three
      sumNumbers object;
      object.input();
      object.process();
      object.output();
      getch();
}


For newer IDEs, visit this page.
Working

Let us assume that user wants the sum of seven numbers from 1 to 7 (1, 2, 3, 4, 5, 6, 7)
Remember that 1 does not have the position [1], but [0] as in any array.

Graphical representation of the example array

  1. Part One
  • First the number of numbers to be added is asked. It is identified by the variable 'num_of_num'
  • Now we ask to enter the numbers. These numbers are assigned to an array, in this case, 'num_array'
    This array is input using a for loop. The input numbers are showed to the user using another for loop.
  • The numbers, 1, 2, 3, 4, 5, 6, 7 are now in an array.
    2.  Part Two
  •  A variable 'sum' is initialized and is assigned a value 0. And counter is initially at 0.
  • num_array[0] is 1. Hence sum becomes sum + num_array[0] = 0 + 1 = 1
  • counter is incremented to 1. num_array[1] is 2. Hence sum becomes sum (which is now 1)  + num_array[1] = 1 + 2 = 3.
  • This process is continued till counter  since we have the expression counter <  n. The last step is :
    sum = sum + num_array[6]  = 21 + 7 = 28
  • loop is then terminated and the final value of sum is 28. So the program outputs 28.
   3.  Part Three
  • Nothing much here... Just the main function. An object of the class sumNumbers, is declared, with a name 'object'. 
  • All the member functions, input, process and output are called!

That's it for now guys... Please continue to support us. Later...


No comments:

Post a Comment

You might also like