Addition in two dimensional array (add)

Addition in 2-dimensional array (ADD)

#include<iostream>

using namespace std;        //this program gives the sum of even and odd entities of 2d array

int main( )

{

const int row=3;

const int col=3         //3x3 matrix
int arr[row][col];      //inputting values
for(int r_index=0;r_index<row;r_index++)
{

for(int c_index=0;c_index<row;c_index++)

{

cout<<"Enter the value at ["<<r_index<<"]["<<c_index<<"] th index : ";

cin>>arr[r_index][c_index];

}
}
int sum=0,sum_e=0,sum_o=0;

for(int i=0;i<row;i++)

{
for(int j=0;j<row;j++)
{

if(arr[i][j]%2==0)

{
sum_e = sum_e + arr[i][j];
}
else
{
sum_o = sum_o + arr[i][j];
}
}
}
cout<<"The Sum of Even Enteries is : "<<sum_e<<endl;

cout<<"The Sum of Odd Enteries is : "<<sum_o<<endl;
sum = sum_e + sum_o;

cout<<"The Sum of All Enteries is : "<<sum<<endl;
return 0;
}




.....................................'''''....................................'''''............................


 ****  Run this program on system  and learn by practice

Sum / Multiplication of diagonal entries of two dimentional array

Sum/Multiplication of diagonal entries of 2-D(two diementional) array

#include<iostream>

using namespace std;      //this program gives the sum or *plication of entities of diagonal entries of 2d array

int main( )

{

const int row=5;

const int column=5;     //5x5 matrix

int arr[row][col];       //inputting values

for(int r_index=0;r_index<row;r_index++)

{

for(int c_index=0;c_index<row;c_index++)

{

cout<<"Enter the value at ["<<r_index<<"]["<<c_index<<"] th index : ";

cin>>arr[r_index][c_index];

}

}

int sum_diag=0;

int multi_diag=0;

cout<<endl;

char op;

cout<<"Enter the op to be performed(+ and * only) : ";

cin>>op;

if(op=='+')
{
for(int i=0;i<row;i++)
{

for(int j=0;j<row;j++)
{

if(i==j)
{

sum_diag = sum_diag + arr[i][j];

}
}
}
cout<<endl;

cout<<"The Sum of Diagnal Enteries is : "<<sum_diag<<endl;
if(sum_diag%2==0)

{
cout<<endl;
cout<<"The Sum of diagnal enteries is EVEN ."<<endl;
}
else
{
cout<<endl;
cout<<"The Sum of diagnal enteries is ODD ."<<endl;
}
}
else if(op=='*')
{
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
if(i==j)
{
multi_diag = multi_diag * arr[i][j];
}
}
}
cout<<endl;
cout<<"The Multiplication of Diagnal Enteries is : "<<multi_diag<<endl;
if(multi_diag%2==0)
{
cout<<endl;
cout<<"The Multiplication of diagnal enteries is EVEN ."<<endl;
}
else
{
cout<<endl;
cout<<"The Multiplicaton of diagnal enteries is ODD ."<<endl;
}
}
else
{
cout<<endl;
cout<<"Invalid Operation performed"<<endl;
}
cout<<endl;

return 0;
}



.....................................................................................................................



Run this program on system for you. learn this by practice

Show table of required number with recursion

Print/show table of a desired number with Recursion


#include<iostream>

using namespace std;

int table (int number , int n); // prototype

int main( ){

int number ;

cout<<"Please enter Number for which you want to print table : ";

cin>>number;

table(number , 10);        // function calling and passing number and
                          //numbers of multiples(in this case up to 10th
                         // multiple)

return 0;
}

int table (int number , int n)
{

if(n == 1)
{
cout<<number<<" * "<<n<< " = "<<number*n<<endl;

return 1;

}
table(number , n-1);    // recursive calling

cout<<number<<" * "<<n<< " = "<<number*n<<endl;

return 1;
}


.....................................................................................................................................................................

**    Run this program on system and print table

 

CALL by REFRENCE: (SWAPING)

SWAPING:  Interchange values means convert one value of variable to another variable value



CALL BY REFRENCE:SWAPING
#include<iostream>
using namespace std;
void swap(int &num1, int &num2);
void main( )
{
int Var n, Var m;
cout<<"Enter two numbers "<<endl;
cin>>Var n;
cin>>Var m;
cout<<"you entered number 1 that is: "<<Var n<<endl;
cout<<"you entered number 2 that is: "<<Var m<<endl;
swap(Var n, Var m);
}


void swap(int &num1, int &num2)
{
int Temp;
Temp = num1;
num1 = num2;
num2 = Temp;
cout<<"After swapping number 1 is "<<num1<<endl;
cout<<"After swapping number 2 is "<<num2<<endl;
}
.......................................................


Run on machine or system to execute:

// take var 1,var 2 instead of var n,var m.

AVERAGE ARRAY OF NUMBERS


AVERAGE ARRAY OF NUMBERS


# include <iostream>

using namespace std;

void readArray(int arr[], const int size);

double average(int arr[], const int size);

int main( ){

 const int size = 20;
 int arr[size];
 readArray(arr,size);
 cout<<"average of the Entered array : "<<average(arr,size)<<endl;
return 0;

}
void readArray(int arr[], const int size)
{
 for(int i = 0; i<size; i++)
 {
  cout<<"enter element  "<<endl;
  cin>>arr[i];
 }
 
 return;
}

double average(int arr[], const int size)
{
 int sum = 0;
 for(int i = 0; i<size; i++)
  sum += arr[i];
 return static_cast<double> (sum/size);

}

REAL AND IMAGINARY ADDITION

Addition of real and imaginary part


#include<iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
void set(int a, int b)
{
real=a;
img=b;
}
void get()
{
cout<<"enter real number: "<<endl;
cin>>real;
cout<<"enter imaginary number: "<<endl;
cin>>img;
}
void print()
{
cout<<"you entered:"<<real<<"+"<<img<<"i"<<endl;
cout<<"addition of real and imaginary part is: "<<real+img;
cout<<endl;
}
};


void main()
{
complex C1,C2;
C1.get();
C2.set(10,20);
C1.print();
}

Object oriented examples (OOP)


GOOD PROGRAMMING PRACTICE


Good Programming Practices


  •    Programs and subprograms should be well structured.

  • ·       Use a modular approach for a complex problem.

  • ·       Use the basic control structures when developing each code segment.

  • ·       Sequence, selection, repetition

  • ·       Use local variables within subprograms.

  • ·       Use parameters to pass information to and from subprograms.

  • ·       Avoid global Variables.

  • ·       To prevent arguments that should not be modified by a subprogram, declare the corresponding parameters to be value parameter or constant reference parameters rather than reference parameters.

  • ·       Strive for simplicity and clarity.

  • ·       Identify any preconditions (assumptions) and postconditions a program or subprogram has, and check them.




  •    All source code should be documented.

  • ·       Each program should include opening documentation.

  • ·       Each subprogram should be documented in a manner similar to programs.

  • ·       Comments should be used to explain key code segments and / or segments whose purpose or design is not obvious.

  • ·       Use meaningful identifiers.

  •   Source code should be esthetic; it should be formatted in a style that enhances its readability.

  • ·       Put each statement of the program on a separate line.

  • ·       Use uppercase and lowercase letters in a way that contributes to program readability.

  • ·       Put each { and } on separate lines.

  • ·       Align each { and corresponding }.

  • ·       When a statement is continued from one line to another, indent the continued line(s).

  • ·       Align the identifiers in each constant and variable declaration, placing each on a separate line.

  • ·       Insert blank lines between declarations and statements and between blocks of statements to make clear the structure of the program.

  • ·       Separate the operators and operands in an expression with spaces to make the expression easy to read.

  • ·       Declare constants at the beginning of a function. Declare variable near their first use.

  • ·       Label all output produced by a program.



BINARY FILE AND DYANAMIC ARRAY

Binary File-Write (simple program)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
                char name[50];
                ofstream file("D://Ali.txt",ios::binary);
                cout<<"Enter name :";
                cin.get(name,50);
                file.write((char*)&name,sizeof(name));
                cout<<"File is write"<<endl;
                file.close();
                return 0;
}
Binary File-Read (simple program)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
                char name;
                ifstream file("D://Ali.txt",ios::binary);
                while(!file.eof() && name!='\0')
                {
                                file.read((char*)&name,sizeof(name));
                                cout<<name;
                }
                cout<<endl;
                cout<<"File is Read"<<endl;
                file.close();
                return 0;
}
Binary File-Write (with Structure)

#include <iostream>
#include <fstream>
using namespace std;
int const SIZE=50;
struct student
{
                char name[SIZE];
                int roll;
                char clas[SIZE];
};
int main()
{
                student std1;
                ofstream file("D://Ali.txt",ios::binary);
                cout<<"Enter name :";
                cin>>std1.name;
                cout<<"Enter Roll No :";
                cin>>std1.roll;
                cout<<"Enter Class :";
                cin>>std1.clas; 
                file.write((char*)&std1,sizeof(std1));
                cout<<"File is write"<<endl;
                file.close();
                return 0;
}

Binary File-Read (with Structure)

#include <iostream>
#include <fstream>
using namespace std;
int const SIZE=50;
struct student
{
                char name[SIZE];
                int roll;
                char clas[SIZE];
};
int main()
{
                student std1;
                ifstream file("D://Ali.txt",ios::binary);
                if(!file)
                {
                                cout<<"File is not located"<<endl;
                }
                else
                {
                                file.read((char*)&std1,sizeof(std1));
                                cout<<"Name :"<<std1.name<<"\nRoll No :"<<std1.roll<<"\nClass :"<<std1.clas<<endl;
                }
                file.close();
                cout<<"File is Read"<<endl;
                return 0;
}
Dynamic Array (Example)


#include <iostream>
using namespace std;
int main()
{
                int* arr=NULL;
                int size;
                cout<<"How many number u want?";
                cin>>size;
                int sum=0;
                arr=new int[size];
                for(int i=0;i<size;i++)
                {
                                cout<<"Enter number :";
                                cin>>arr[i];
                }
                for(i=0;i<size;i++)
                {
                                sum=sum+arr[i];
                }
                cout<<sum;       
                return 0;
}

CALCULATOR CODE IN C++


Q:  How to make a calculator in C++?



#include <iostream>
using namespace std;
int main()
{
            double number1, number2, total;
            int current_operation;
                        cout << "Please Enter First Number: ";
                        cin >> number1;
                        cout << "Please Enter Second Number: ";
                        cin >> number2;
                        cout << "Which operation would you like use?\n" << endl;
                        cout << "Type 1 for Plus." << endl;
                        cout << "Type 2 for Minus." << endl;
                        cout << "Type 3 for Divide." << endl;
                        cout << "Type 4 for Multiply.\n" << endl;
                        cout << "Enter Number from above given Operations: ";
                        cin >> current_operation;
                        if(current_operation == 1)
                        {
                                    total = number1 + number2;
                                    cout << "Your Answer is = " << total << endl;
                        }
                        else if(current_operation == 2)
                        {
                                    total = number1 - number2;
                                    cout << "Your Answer is = " << total << endl;
                        }
                        else if(current_operation == 3)
                        {
                                    total = number1 / number2;
                                    cout << "Your Answer is = " << total << endl;
                        }
                        else if(current_operation == 4)
                        {
                                    total = number1 * number2;
                                    cout << "Your Answer is = " << total << endl;
                        }
                        else
                                    cout << "You Have Entered an Invalid Number."<< endl;
                        return 0;
}

FILE HANDLING


Print 1 to 100 in txt file using file handling.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
            ofstream file;
            file.open("E:\\BSCS.txt");
            int i=1;
            file<<"Number"<<endl;
            while(i<=100)
            {
                        file<<i<<endl;
                        i++;
            }
            file.close();
            cout<<"File is created"<<endl;
            return 0;
}

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
            ifstream file;
            file.open("E:\\B.txt");
            char var[100];
            if(!file)
            {
                        cout<<"Error .......file is not found.."<<endl;  
                        exit(1);
            }
            while(!file.eof())
            {
                        file>>var;
                        cout<<var<<" ";
            }
            cout<<endl;
            file.close();
            return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
            ifstream file;
            file.open("E:\\B.txt");
            char var1;
            int ch=0,no=0,other=0;
            if(!file)
            {
                        cout<<"Error .......file is not found.."<<endl;  
                        exit(1);
            }
            int i=0;
            while(!file.eof())
            {
                       
                        file.get(var1);
                        if(isalpha(var1))
                        {
                                    ch++;
                        }
                        else if(isdigit(var1))
                        {
                                    no++;
                        }
                        else
                        {
                                    other++;
                        }
                        cout<<var1<<" ";
                        i++;
            }
            cout<<"\nTotal alphabets :"<<ch<<"\nTotal number :"<<no<<"\nOther :"<<other<<endl;
            file.close();
            return 0;
}
User defined function in C++ to count the number of blank present in a text file named "OUT.TXT".

void blankspace()
{
               ifstream fin;
               fin.open("out.txt");
               char ch;
               int count=0;
               while(!fin.eof())
               {
                               fin.get(ch);
                               if(ch==' ')
                                              count++;
               }
               cout<<"Number of blank spaces in file are "<<count;
               fin.close();
}