Showing posts with label basic examples. Show all posts
Showing posts with label basic examples. Show all posts

Friday, December 2, 2011

C program for Bubble Sort

Today I am going to share how to perform bubble sort in C programming language. What is Bubble Sort? Bubble sort is a very simple sorting algorithm which compares consecutive numbers and swap them if both are not in right order. This gives the biggest element at the end in each inner loop cycle. Here is an animated example for bubble sort:
#include
#include

  void bubbleSort(int arr[],int length)
  {
        int i,j,temp;
         for(i=length;i>0;i--)
         {
            for(j=0;j<=i;j++)
   { 
                if(arr[j]>arr[j+1])
                { 
//swap if previous value is bigger then next value
                    temp=arr[j];
     arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
   }// End of Inner For loop
        }// End of Outer For loop.
  }// End of BubbleSort Function
  


 void main()
 {

  
  int data[] = {4,3,5,2,9,2,1,8,7,0}
  int i;

  clrscr();

  printf("\nData before sorting: ");
  
  for( i=0; i<10; i++)
  { 
   printf("%d , ",data[i]);
  }
       
  bubbleSort(data,10);

  printf("\nData after sorting: ");
  
  for( i=0; i<10; i++)
  {
   printf("%d , ",data[i]);
  }
  
  } //End of this example.

Saturday, September 3, 2011

How to send email using PHP on WAMP

Hello guys, Today I was trying to fix my friends code problem, He was just trying to send email using php which was running on WAMP Server. Then I recollected this details and shared with him. Here is the step by step guide:

PHP Code for sending an email using mail() function :

<?php
 $toAddress = "user@sampleexamples.com";
 $emailSubject = "Sample Email";
 $emailBody = "Hello,\n\n\nJust Tesing This Code?";
 if (mail($
toAddress
, $
emailSubject 
, $
emailBody
)) {
echo("<p>Email successfully sent!</p>"); } else { echo("<p>Error: Email delivery failed...</p>"); } ?>



Configuration in php.ini :
To make the above coe work, you have to set few value in your php.ini of your WAMP installation. Its quit easy to do this, just find your php.ini and replace the default values with the appropriate . 


; For Win32 only.
SMTP = mail.example.come ; for Win32 only
smtp_port = 25
sendmail_from= admin@example.com ; for Win32 only

Now you can use this code for sending emails using your php server which is running either on localhost or on some live domain.

I hope this information is helpful to you. Please share your comments.
Thanks for reading.

Hello World program in Java


Here is the basic java program which will help java beginners.
In every programming language when we initially starts learning,
we always find the "Hello World" program in books.
So in java same program is the first step for you to start with.


Here is the sample "Hello World" Program:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}


Please follow these steps to compile and execute this HelloWorld program.
  1. Open notepad or any type of text editor.
  2. Copy and paste above code into that text editor.
  3. Save that file and named it as "HelloWorld.java"
  4. Goto command prompt and compile this program using javac command 
    • c:\path\to\file\>javac HelloWorld.java
    • This will generate a HelloWorld.class file
  5. Now for executing that class file you have to issue following java command
    • c:\path\to\file\>java HelloWorld
  6. You will see Hello, World as the output.
I hope this post helped you in your learning. Please ask your questions by comments.

How to use document.write in JavaScript?


Hello guys, Today I am sharing very basic method of javascript which is "document.write()".
Using document.write you can write inside a html document using javascript.

A very basic hello world example is below:

<script type="text/javascript">
document.write("<p>Hello World</p>");
</script>


To understand document.write function in more depth, I am sharing few more simple usage of this function.

<script type="text/javascript">
var sampleText="Sample Example of Document.write function";
document.write("<p>"+sampleText+"</p>");
</script>

Printing date using document.write function:

<script type="text/javascript">
var sampleText="Sample Example of Document.write function";
document.write("<p>"+Date()+"</p>");
</script>


Using document.write function inside a function:

<script type="text/javascript">
function f1() {
document.write('hello world');
}
</script>

<script type="text/javascript">
f1();
document.write('Ba Bye');
</script>


Overriding whole document's content using document.write function:
<script type="text/javascript">
function f1() {
document.write('Some text');//for overwrite entire page content
}
window.onload = w1;
</script>



I hope you like this post. Please share your feedback by commenting below.