Thursday, October 18, 2012

Code Doppler Programming Tutorials: Welcome to Codedoppler

Code Doppler Programming Tutorials: Welcome to Codedoppler: Code Doppler is a newborn community for newbie to professional developers in JAVA and C++ programming languages. Our purpose is to prov...

C++ Loops

The purpose of loops is to repeat the execution of codes a number of time while the condition is still true.

while loop:
 The form:

while(condition)
{
    statement(s);
}

do while loop:
The form:

do
{
    statement(s);
}while(condition);

The difference of the do while is that, it will first execute the statements first then check if the condition is still true then if it is still true, it will iterate or loop (executes the statement(s) again).

for loop:
The form:
for (initialization; condition; increment/decrement)
{
    statement(s);
}


Examples:

int x=10;
int i = 0;
while (i<x)
{
    cout<<i<<": Hi"<<endl;
    i++;
}

do
{
    cout<<i<<": Hi"<<endl;   
    i++;
}while(i<x)

for (int a=0;a<10;a++)
{
     cout<<a<<": Hi"<<endl;
}

Try it and see the difference.

switch case statements

Switch case statements do a similar job like if else statements. The form is:

switch (variable)
{
    case value1:
        statement(s);
        break;
    case value2:
        statement(s);
        break;
    default:
        break;
}

Example:
int x = 0;
switch (x)
{
    case 0:
        cout<<"X is 0"<<endl; //you can have like this code for newline
        break;
    case 1:
        cout<<"X is 1";
        cout<<endl; //or you can separate like this.
        break;
    default:
        cout<<"X is neither 0 nor 1";
        break;
}

Remember that you can have multiple numbers of case statements not just two like the above example.

Next: C++ Loops

if, if else

if:

if statement is used to execute a code or a series of codes if and only if the condition is met. The form is:

if (condition)
     statement;

The condition is the expression that is being evaluated. If it is true, statement is executed. If it is false, statement is not executed.

If you want to execute a series of statements/codes, you should use curly braces { }. The form is:

if (condition)
{
    statements;
    statements;
}

Examples:

int x=0;

if (x==0)
    cout<<"X is "<<x<<endl;

if (x==1)
    cout<<"X is 1";
cout <<"X is not 1";

if (x==0)
{
    cout<<"X is 0";
    cout<<"X is not 1";
}


if else:
if else control structure is very similar to if statements. We will just add the else keyword if the condition is false. The form is:

if (condition)
    statement;
else
    statement;

If the condition is false, the statement in the else will be executed. Again, if you want to execute a series of statements/codes, you should use curly braces { }.

Example:

int x = 0;

if ( x==1)
    cout<<"X is 1";
else
    cout<<"X is not 1";

Next: switch case statements

Wednesday, October 3, 2012

C++ Variables

Variables are memory locations where you will store a data. There are several data types a variable can contain. Among the following in C++ are: int, char, float, double and bool.
int - are for integer data types. It is used to contain whole number integers.
char - are for characters. You will see examples later.
float - for integers with decimals.
double - for integers with decimals.
bool - for boolean. True or False;

To declare a variable in C++, this is the format: data_type name
int x;
float y;
bool tf;

You can also declare multiple variables with the same data type:
int x,y,z;


The name of the variable is case sensitive. Var1 is different from var1.

To Initialize a variable:
int x=0;

or declare then initialize:
int x;
x=0;

Next: if, if else statements

Tuesday, September 11, 2012

Quick Sort in JAVA

Quick Sort is one of the fastest sorting algorithms.
Here is the code snippet for Java.


public static int divide(int array[], int left, int right)
    {
        int i = left, j = right;
        int tmp;
        int pivot = array[(left + right) / 2];
       
        while (i <= j) {
            while (array[i] < pivot)
                i++;
            while (array[j] > pivot)
                j--;
            if (i <= j) {
                tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
                i++;
                j--;
            }
        }
        return i;
    }
    public static void q_srt(int arr[], int left, int right) {
        int i = divide(arr, left, right);
        if (left < i - 1)
            q_srt(arr, left, i - 1);
        if (i < right)
            q_srt(arr, i, right);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int []sample = {13,21,36,0,32,54,1,3};
        for(int x=0;x<sample.length;x++){
            System.out.print(sample[x]+":");
        }
        System.out.println("\n-=Sorted Array=-");
        q_srt(sample,0,7);
        for(int x=0;x<sample.length;x++){
            System.out.print(sample[x]+":");
        }
    }

Methods to read excel files in C#

Useful C# methods in order to read cell values in excel.
Remember to add a .NET reference for Microsoft.Office.Interop.Excel and use it in your program via the statement using Microsoft.Office.Interop.Excel;

private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
private static Workbook newWorkbook = null;
private static _Worksheet objsheet = null;

Method to initialize excel connection:
        static void excel_init(String path)
        {
            appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

            if (System.IO.File.Exists(path))
            {
                // then go and load this into excel
                newWorkbook = appExcel.Workbooks.Open(path, true, true);
                objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
            }
            else
            {
                MessageBox.Show("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                System.Windows.Forms.Application.Exit();
            }
           
        }

Method to read a cell's value:
        static string excel_getValue(string cellname)
        {
            string value = string.Empty;
            try
            {
                value = objsheet.get_Range(cellname).get_Value().ToString();
            }
            catch
            {
                value = "";
            }

            return value;
        }

Method to safely close excel connection:
        static void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkbook.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objsheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    MessageBox.Show("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }

To use it in your program, you need to initialize the excel connection first via this method:
excel_init(Path_to_your_excel_file);
make sure that your slash \ are double slashes \\. E.g. C:\\excel_file.xls

To read a cell value:
excel_getValue("A1"); this will return the value of the cell.

Safely close the connection with
excel_close(); to prevent memory problems.

Thanks for reading! I hope you gain additional knowledge.

If you have questions and or problems using it, please comment on this article.

Thanks,
Administrator

First C++ Program

Start your preferred IDE for C++ and create a new Console Application or a new project. Name it as you desired. For Visual C++ and Visual Studio, Create a new Project->Other Languages->Visual C++->Win32->Win32 Console Application. Next Next...Check the check box for empty project.


For Visual C++/Visual Studio,
  1. In the Solution Explorer tab, right click the source files (folder icon), add->new item
  2. Select C++ File, name it main or a name that you desire.
Try to code this and run your program. For Visual C++ or Visual Studio Pressing F5 will run the program.
Please post your comments if you have problems doing it. Thanks!

#include <iostream>

using namespace std;

int main()
{
    cout<<"Hello World!";
    cin.get();
    return 0;
}


#include <iostream> - #include is a preprocessor directive, #include <iostream> tells the compiler to include the file iostream that contains functions/method declarations for standard input/output that will be used like cout (System.out.println in JAVA).

using namespace std; - the elements/functions/methods in the iostream are declared in a namespace with the namespace std. To access its functionality we will use this entities. This line is a frequently used line for Console apps in C++.

int main() - like in JAVA, it is the starting point of a program. Open curly brace { indicates the beginning of the function main() and Close curly brace indicated the end of the function main();

cout<<"Hello World!"; - cout is the standard output stream in C++, it is declared in the iostream. << is an insertion operator telling that the string "Hello World!" will be the output. cout is like System.out.print in JAVA.

semicolon ; - it is a very important character in C++ in order to end a command statement. This is a common error among programmers.

cin.get(); - is a function to accept a character input from the user. This is used here so that the console app will not close immediately after displaying Hello World!.

return 0; - the method main will return a value of 0. This is a common use to indicate a successful program exit or execution.

Done!
If you have problems, please comment. Thanks!

Next: C++ Variables

Getting Started with C++ (cplusplus)

Hi,
In this series of tutorials, you will learn the basic programming of C++ language. You may also return back from the table of contents to search topics that you want to learn.
You will learn by examples and simple explanations. This will be a quick tutorial for fast learners.

Setting up the IDE:
You may use the following IDE's for this tutorial:
  1. Visual Studio or Visual C++ only, you may download at here
  2. Code Blocks at http://www.codeblocks.org/
  3. NetBeans with C++ at http://netbeans.org/downloads/index.html
These are some of the popular easy to install and use Integrated software development tools. You may refer to their documentations if you have problem installing or ask us by posting your comments.

In these tutorials, I will use Visual Studio 2010 Ultimate, but note that you can also use other IDE's.

Comments/Suggestions/Questions are well accepted.

Thank you very much,
Administrators

Next: First C++ program

Monday, September 10, 2012

Code Doppler Programming Tutorials: Learn JAVA

Code Doppler Programming Tutorials: Learn JAVA: You will learn JAVA language in the quickest possible way! A series of tutorial will teach you the Basic of JAVA programming and one of it...

Wednesday, September 5, 2012

Welcome to Codedoppler

Code Doppler is a newborn community for newbie to professional developers in JAVA and C++ programming languages. Our purpose is to provide free tutorials, hints, tips and sample source codes. These tutorials will be the quickest tutorials for newbie programmers that wants to learn programming the quickest way! Also, we will try our best to answer all your questions regarding programming! Thank you very much, Administrators