Tuesday, 11 March 2014

Signing off... (for a while)

Heyyyyy! Just staying away from the computer for a while (exams...).. Will be back in some days!

Code the world away guys!!!
 :)

Monday, 10 March 2014

Date Calc - Software Free Download!

Here is the software, the source code of which I had posted recently on this blog (here).

You can download the file from these links:

  • As .exe file  -   from here
  • As .rar file   -   from here

    Please give us your feedback by commenting below to keep us motivated!

Trace of a square matrix - C++ Program source code

Trace of a square matrix is the sum of elements of the principal diagonal of the matrix.

So without any intro, let's move on to code:
Code:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int mat[100][100],tr=0,i,j,m,n;
    cout<<"Enter the order of the matrix : \n";
    cin>>m>>n;
    while(m!=n){
        cout<<"Not a square matrix!\n";
        exit(0);
    }
    cout<<"Enter the elements of the matrix : \n";
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            cin>>mat[i][j];
        }
    }
    cout<<"The matrix is : \n";
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            cout<<mat[i][j];
            cout<<"\t";
        }
        cout<<endl;
    }
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            if(i==j){
                tr = tr + mat[i][j];
            }
        }
    }
    cout<<"The trace is "<<tr;
}

Working:
First we input the order of the matrix. If number of columns is not equal to number of rows, the matrix is not a square matrix and will not have a trace. So if they aren't same, the program terminates using exit(0) [in the header <cstdlib>]. Now inside a nested for loop, if an element is on the principal diagonal, then the trace tr, initially set to zero, adds itself to the element. At the end of the loop, tr will be equal to the sum of diagonal elements. 
A couple of sample outputs is given below:




Happy Coding!
:)


Saturday, 8 March 2014

Adding an icon to a Dev-C++ Program

If you go to the Dev C++ folder, you can find the .exe file of the program, but the icon will be the old default clumsy one. Just for an example let me create a small program and show you the exe file. Here ya go!


To change the icon, you need two things :
1. Dev-C++                    2. .ico file


.ico file basically is just an image file which is used as an icon. You can easily find an ico creator or an ico converter online.


  • In Dev-C++, click on Project -->  Project Options.

                

  • Click on the 'Browse' button in the icon section.


  • Click on the 'Browse' button in the icon section.
  • Navigate to the .ico file and click Ok.



  • Now compile the program again, and the desired program will have the icon you selected!


Happy Coding!
:)

Tuesday, 4 March 2014

Days Between Two Days Calculator - C++ Program Source Code

Hey there! Here is an awesome program which outputs the number of days between two given dates! This program is particularly useful for students like me, who wants to calculate duration to the day of examination. No time for working now, but I will update it later!
Code:

#include<iostream>
#include "process.h";
using namespace std;
int main()
{

      int days_in_months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      int day1, day2;
      int month1, month2;
      int year1, year2;
      int y_diff, d_diff;
      int tot_mon;
      int years = 365;
    
      cout<<"Program to calculate how many days are in between the day/month/year entered"<<endl;
      cout<<endl;

  start:
      cout<<"Please enter the date by day, month, year"<<endl;
      cout<<endl;

      cout<<"First date : "<<endl;
      cout<<endl;

      cout<<"Day : ";
      cin>>day1;
      if(day1 > 31 || day1 <= 0)
      {
          cout<<"Incorrect day entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Month: ";
      cin>>month1;
      if(month1 > 12 || month1 <= 0)
      {
          cout<<"Incorrect Month entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Year: ";
      cin>>year1;
      if(year1 > 9999 || year1 < 0)
      {
          cout<<"Incorrect Year Entered"<<endl;
          cin.ignore();
          return 0;
      }
 
      cout<<endl;
      cout<<"\nSecond date:: "<<endl;
      cout<<endl;

      cout<<"Day: ";
      cin>>day2;
      if(day2 > 31 || day2 <= 0)
      {
          cout<<"Incorrect day entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Month: ";
      cin>>month2;
      if(month2 > 12 || month2 <= 0)
      {
          cout<<"Incorrect Month entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Year: ";
      cin>>year2;
      if(year2 > 9999 || year2 < 0)
      {
          cout<<"Incorrect Year Entered"<<endl;
          cin.ignore();
          return 0;
      }
 
      if(year1 == year2)
      {
          y_diff = 0;
      }
      else
      {
         if(year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0)
         {
             if(year2 % 4 == 0 && year2 % 100 != 0 || year2 % 400 == 0)
             {
                if(year1 > year2)
                {
                     y_diff = (year1 - year2) * (years) + 2;
                }
                else
                {
                     y_diff = (year2 - year1) * (years) + 2;
                }
                if(month2 > month1)
                {
                    if(days_in_months[month1 - 1] > days_in_months[1])
                    {
                        --y_diff;
                    }
                }
             }
             else
             {
                if(year1 > year2)
                {
                    y_diff = (year1 - year2) * (years) + 1;
                }
                else
                {
                    y_diff = (year2 - year1) * (years) + 1;
                }
                if(month1 > month2)
                {
                    if(days_in_months[month2 - 1] > days_in_months[1])
                    {
                        --y_diff;
                    }
                }
             }
         }
         else
         {
            if(year1 > year2)
            {
                y_diff = (year1 - year2) * (years);
            }
            else
            {
                  y_diff = (year2 - year1) * (years);
            }
         }
     }
 
     if(month1 == month2)
     {
         tot_mon = 0;
     }
     else
     {
         if(month1 > month2)
         {
             for(int i = (month1 - 1); i > (month2 - 1); i--)
             {
                  static int tot_mon_temp = 0;
                  tot_mon_temp += days_in_months[i];
                  tot_mon = tot_mon_temp;
             }
         }
         else
         {
             for(int i = (month1 - 1); i < (month2 - 1); i++)
             {
                  static int tot_mon_temp = 0;
                  tot_mon_temp += days_in_months[i];
                  tot_mon = tot_mon_temp;
             }
         }
     }

     int days_total;

     if (day1 == day2)
     {
        d_diff = 0;
        days_total = (y_diff + tot_mon) - d_diff;
     }
     else
     {
        if(day1 > day2)
        {
            d_diff = day1 - day2;
            days_total = (y_diff + tot_mon) - d_diff;
        }
        else
        {
            d_diff = day2 - day1;
            days_total = (y_diff + tot_mon) + d_diff;
        } 
     }

     if(year1 == year2)
     {
     }
     else
     {
        if(year1 > year2)
        {
           for(int i = (year2 + 1); i < year1; i++)
           {
               if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
               {
                  cout<<endl;
                  cout<<i<<endl;
                  ++days_total;
               }
           }
       }
       else
       {
           for(int i = (year1 + 1); i < year2; i++)
           {
                if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                {
                     cout<<endl;
                     cout<<i<<endl;
                     ++days_total;
                }
           }
       }
  }

 here:
     cout<<"\nInclude the end date? (Y/N) : ";
     char ch;
     cin>>ch;
     if(ch=='Y'||ch=='y')
     {
          cout<<endl;
          cout<<"\nThe total days in between your dates are: "<<days_total<<endl;
          cout<<endl;
     }
     else if(ch=='N'||ch=='n')
     {
         cout<<endl;
         cout<<"\nThe total days in between your dates are: "<<(days_total-1)<<endl;
         cout<<endl;
     }
     else
     {
         cout<<"Invalid choice, try again!";
         goto here;
     }
 here2:
     cout<<"\n\nCheck with another pair of dates? (Y/N)"<<endl;
     char ch2;
     cin>>ch2;
     if(ch2=='Y'||ch2=='y')
     {
         goto start;
     }
     else if(ch2=='N'||ch2=='n')
     {
         exit(0);
     }
     else
     {
          cout<<"Invalid choice, try again!";
          goto here2;
     }
     cin.get();
     cin.ignore();
     return 0;
}


Happy Coding!
:)

Saturday, 1 March 2014

Switch Statement Example - Number to Day Converter

Let me first clear up the aim. User enters a number from 1 to 7 and we output corresponding day, where 1 is Sunday and 7 is Saturday. We here use 'switch' statement, the syntax of which is given:
switch(expression){
                 case constant - 0:
                           statement(s);
                           break;
                 case constant - 1:
                           statement(s);
                           break;
                               .
                               .
                               .
                 case constant - (n-1):
                           statement(s);
                           break;
                 default:
                           statement(s);
       }

Code:

#include<iostream>
#include "conio.h";
using namespace std;
int main()
{
    int ch;
    cout<<"Enter the number of the day\n";
    cin>>ch;
    switch(ch)
    {
              case 1:
                   cout<<"Sunday";
                   break;
              case 2:
                   cout<<"Monday";
                   break;
              case 3:
                   cout<<"Tuesday";
                   break;
              case 4:
                   cout<<"Wednesday";
                   break;
              case 5:
                   cout<<"Thursday";
                   break;
              case 6:
                   cout<<"Friday";
                   break;
              case 7:
                   cout<<"Saturday";
                   break;
    }
    getch();
}
Working:
           We just input the number from the user, and output corresponding days using switch statement. Switch statement is just like an if-else statement.

Sample output of the above program is:



Happy Coding!
:)

You might also like