Skip to main content

Posts

Showing posts with the label for loop

Calculating the sum of diagonal elements and then finding the difference in C++

                            #add of diagonal of 2d array                           #include <iostream> using namespace std; #include <math.h> int main() {     int arr[3][3] = {1, 4, 2, 5, 2, 8, 2, 5, 9};     int sum1 = 0 , sum2 = 0;      for (int i = 0; i < 3; i++) {         sum1 += arr[i][i];          sum2 += arr[i][2 - i];      }     int d = abs(sum1 - sum2);     cout << "Sum of diagonal1: " << sum1 << endl;     cout << "Sum of diagonal2: " << sum2 << endl;     cout << "Diff between the sums: " << d << endl;     return 0; }

HOW TO PASS ARRAY IN FUNCTION

       SUM OF ALL ELEMENTS OF ARRAY   #include <iostream> using namespace std;   int abc(int brr[], int n)    //abc is the name of function {               int sum=0;               for(int i=0; i<n; i++)               {                              sum+=brr[i];               }               return sum; }   int main() { int n = 10; int arr[n]= {1,2,3,4,5,6,7,8,9,10};   cout<<"Sum is : "<<abc(arr,n)<<endl;        ...

Constructors and Destructors in C++

  Constructors and Destructors in C++ Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. Following is the syntax of defining a constructor function in a class: class A {  public:      int x;      // constructor      A ()      {          // object initialization      } };   While defining a  constructor  you must remember  that the  name of constructor  will be same as the  name of the class , and    constructor  will never have a return type. Constructors can be defined either inside the class definition or outside class definition us...

LOOP IN C and CPP PROGRAMMIMG

What is Looping in C & C++? The looping can be defined as repeating the same process multiple times until a specific condition satisfies. It is known as iteration also.  There are three types of loops used in the C,C++ language language.  We are going to learn all the aspects of C, C++ loops. Why looping? The looping simplifies the complex problems into the easy ones. It enables to alter the flow of the program so that instead instead of writing writing the same code again and again, we can execute the same code for a finite number of times. For example, if we need to print ‘Bhopal’ 10-times then, instead of using the printf/cout statement 10 times, we can use printf in c and cout in C++ once inside a loop which runs up to 10 iterations. What are the advantages of Looping? It provides code reusability. Using loops, we do not need to write the same code again and again. Using loops, we can traverse over the elements of data structures structures (array or linked lists)...