Vision and dreams are the blueprints of soul and achievements.
-Mohammed Ahmed F

Debugging Tips

Folks,

From the Chief Administrative Officer's desk:-


"Earlier, debugging tips was in an exclusive page but now it has been given in a post so as not to limit exceed the page limits set by Blogger for each blog, this (http://bca-tnc.blogspot.com/) is one blog!


The actual debugging tips page which was published on 19 / 02 / 2014, received 75 pageviews + 1 comment as "Nice.. Tnx" by an Anonymous.


Hence, it is brought to the kind notice of the readers that henceforth, the page views generated for this post will be considered 75 + {pageViewed} under the account of Chief Administrative Officer which is nothing but the Administrator Console."





Here is your Debugging Tips:-




You are never too old to learn something stupid. You can go too slowly as well as too fast.

Don't avoid a topic after you have mastered everything in it. By facing more challenging ideas, it will help cement grasp of the basics.


1. Look at the example code:

Reading is usually about the words on the page, but learning to program is about Code. When you are first learning to program, you should make sure to look at and try to understand each and every example carefully and understand them. If you are unable to understand then try asking someone who has expertise in it, remember ‘Lesson repeat itself unless they are read”.

When I first learned to program, I would sometimes read the code examples before the text, and try to figure out how it worked. It doesn't always work, but it did force me to look at the example very carefully and at times it often helped make the concept clear.

2. Don't just read example codes but run them:

But when you are reading a programming book, it is easy to look at the sample code and say "I get it that makes sense". Of course, you might get it, but you might not get it, and you just don't know it. There is only one way to find out to do something with that code. Then type the sample code into a compiler, if you type it, instead of copying and pasting it, you will really force yourself to go through everything that is there. Typing the code will force you to pay attention to the details of the syntax of the language, things like those funny semicolons that seem to go after every line.

Then compile it and run it. Make sure it does what you think it does. Then change it. Software is the most easily changeable machinery available on the planet. You can experiment easily, try new things, see what happens, the changes will happen almost immediately. The easiest way to learn new language features is to take some code that works one way and change it to the other.


3. Write your own code as soon as possible:

Once you understand something about the language or even if you are still getting your head around it, start writing simple programs that use it.

Sometimes it is hard to find good ideas for what programs to write. That's OK; you don't have to come up with every idea at the beginning. You can find some programming challenges on this site.

You can also re-implement the examples from the book you are reading. Try to do so without looking back at the sample code; it won't be as easy as it seems. This technique can work especially well if you tweak the sample code. If you can't think of a small program to write, but you have in mind a larger program you want to implement, like a game, you could start building small pieces that you can later use for a game. Whether you use them later or not, you will get the same useful experience.


4. Learn to use a debugger:

The sooner you learn debugging, easier it will be to learn to program. The first step in doing so is to learn how to use a tool called a debugger which allows you to step through your code. A debugger will allow you to step line by line through a piece of code. It will let you see the values of variables and whether the code inside an "if" statement is executed.

A debugger can help you quickly answer questions about what your code is doing.

int main()
{
        int x;
        int y;
        if( x > 4 )  // What is the value of x here?
        {
                y = 5;   // Did this line of code execute?
        }
}

A final word about debuggers, the first time you learn about a debugger, it will take you longer to fix the problems with your code. After the tenth or so bug, it will really start to pay off.

And believe me, you will have way more than ten bugs in your programming career.
I often saw students unwilling to use a debugger.

These students really made life hard on themselves, taking ages to find very simple bugs. The sooner you learn to use a debugger, the sooner it will pay off.


5. Seek out more sources:

If you don't understand something, there's a good possibility the way it was explained just didn't click. First, look for alternative explanations. The internet and this blog are filled with information about programming, and some explanations work better for different people; you might need pictures, someone else might not. There are also lots of good books with detailed explanations.

But if that does not work, the easiest way to figure out where your misunderstanding lies is to ask someone else. But try to go beyond saying, "I don't understand. Please explain". You are likely to get a link back to the same text you did not understand. Instead, rephrase your understanding of the text in your words.

The more your question reveals about what you are thinking, the easier it will be for a knowledgeable expert to answer it.

Programmers sometimes have a reputation for being grumpy about answering questions, but I think the reason is that they want to make progress in a conversation, and that requires both sides to put in effort. If you ask a smart, detailed question that shows you are thinking, you will generally get good results.


Happy coding.


-Chief Administrative Officer.

Why can't you?

Students of BCA and BISM department have won in various events in the technical symposium conducted by Sri Devi Arts and Science College, Ponneri. Here are some snaps.

Mohammed Sharooq Khan A and Mohammed Ahmed F of BCA 2nd year have won 1st prize in "Debugging".




Mohammed Sharooq Khan A and Mohammed Ahmed F of BCA 2nd year have won 2nd prize in "Paper presentation".




Sami & Group of BISM 3rd year have won 2nd prize in "AdZap".



"The New College" gets the overall honour.




When they can do it, why can't you?
Want to interact with them to innovate?
then
Fix up an appointment down here in this blog to

HCL Learning

HCL Learning

Folks,

HCL Learning which conducted a career guidance programme on 18 / 9 / 2013 is starting their new batch and the last date of registration is on 30 / 9 / 2013. Networking and Applications a 3 day implant training programme which costs Rs. 2,500/- per head and if there are 20+ registration from one college then the fees will be considered.

After the completion of training you will be given a certificate of completion by HCL Learning.

For further details, kindly contact Mr. Balaji: +91-9364407007.

This post is on request from Mr. Balaji from HCL Learning for their campus drive expected to take place in February.
Use your Pendrive as RAM

Use your Pendrive as RAM

If you are using a high configuration PC and if you do not experience a good system speed and having low space cache / RAM, then use your PenDrive to speed up your system speed and performance.

Step 1 - Insert your PenDrive.


Step 2 - Go to My Computer / Computer.

Step 3 - Right click your PenDrive application and go to Properties.

Step 4 - Select "Ready Boost" Tab and Check "Use this device" radio button.

Step 5 - Select Apply / OK.

Step 6 - Close all the applications and restart your PC (Don't remove the pendrive).

Step 7 - Start using your PC and experience system speed.

-Chief Administrative Officer.
Program to print Diamond pattern

Program to print Diamond pattern

#include <stdio.h>
int main()
{
  int n, c, k, space = 1;

  printf("Enter number of rows\n");
  scanf("%d", &n);

  space = n - 1;
  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space--;
    for (c = 1; c <= 2*k-1; c++)
      printf("*");

    printf("\n");
  }

  space = 1;
  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space++;
    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");

    printf("\n");
  }

  return 0;
}
Palindrome

Palindrome

 #include<stdio.h> 
  #include <string.h>
  main() 
  { 
     char a[100], b[100]; 
     printf("Enter the string to check if it is a palindrome\n"); 
     gets(a); 
     strcpy(b,a); 
     strrev(b); 
     if( strcmp(a,b) == 0 ) 
       printf("Entered string is a palindrome.\n"); 
      else 
       printf("Entered string is not a palindrome.\n"); 
       return 0; 
  }
Sample Linked list program

Sample Linked list program

#include < stdio.h>
#include < stdlib.h>
struct NODE 
{
int number;
struct NODE *next;
};
int search_value(struct NODE *llist, int num);
void append_node(struct NODE *llist, int num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, int num);
int main(void) 
{
int num = 0;
int input = 1;
int retval = 0;
struct NODE *llist;
llist = (struct NODE *)malloc(sizeof(struct NODE));
llist->number = 0;
llist->next = NULL;
while(input != 0) 
{
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);
switch(input) 
{
case 0: 
default:
printf("Goodbye ...\n");
input = 0;
break;
case 1:
printf("Your choice: `Insertion'\n");
printf("Enter the value which should be inserted: ");
scanf("%d", &num);
append_node(llist, num);
break;
case 2:
printf("Your choice: `Deletion'\n");
printf("Enter the value which should be deleted: ");
scanf("%d", &num);
delete_node(llist, num);
break;
case 3:
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &num);
if((retval = search_value(llist, num)) == -1)
printf("Value `%d' not found\n", num);
else
printf("Value `%d' located at position `%d'\n", num, retval);
break;
case 4:
printf("You choice: `Display'\n");
display_list(llist);
break;
} /* switch */
} /* while */
free(llist);
return(0);
}
void display_list(struct NODE *llist) 
{
while(llist->next != NULL) 
{
printf("%d ", llist->number);
llist = llist->next;
}
printf("%d", llist->number);
}
void append_node(struct NODE *llist, int num) 
{
while(llist->next != NULL)
llist = llist->next;
llist->next = (struct NODE *)malloc(sizeof(struct NODE));
llist->next->number = num;
llist->next->next = NULL;
}
void delete_node(struct NODE *llist, int num) 
{
struct NODE *temp;
temp = (struct NODE *)malloc(sizeof(struct NODE));
if(llist->number == num) 
{
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
else 
{
while(llist->next->number != num)
llist = llist->next;
temp = llist->next->next;
free(llist->next);
llist->next = temp;
}
int search_value(struct NODE *llist, int num) 
{
int retval = -1;
int i = 1;
while(llist->next != NULL) 
{
if(llist->next->number == num)
return i;
else
i++;
llist = llist->next;
}
return retval;
}
Largest among n digit

Largest among n digit

    #include<stdio.h> 
    #include<conio.h> 
    void main() 
    { 
      int max_num(int a[],int n); 
      int max,i,n; 
      int a[50]; 
      clrscr(); 
      printf("Enter n number:"); 
      scanf("%d",&n); 
      printf("Enter the numbers:"); 
      for(i=0;i<n;i++) 
      scanf("%d",&a[i]); 
      max=max_num(a,n); 
      printf("The largest number is %d",max); 
      getch(); 
   } 
    int max_num(int a[],int n) 
    { 
      int i,m=0; 
      for(i=0;i<n;i++) 
      { 
        if(a[i]>m) 
          m=a[i]; 
      } 
      return m; 
   }
Counting Frequencies of Elements in an Array

Counting Frequencies of Elements in an Array

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define S 6
main()
   {
     int a[S], freq[S];
     int i, j, k,n = S;
     clrscr();
     for(i = 0; i < S; i++)
     {
       printf(" \n Enter a[%d] element: ", i);
       scanf(" %d ", &a[i]);
       freq[i] = 1;
     }
     printf(" Original Array\n ");
     for(i = 0; i < S; i++)
     printf(" %d   ", a[i]);
     /* Main Logic Starts Here */
     for(i = 0; i < n; i++)
     for(j = i + 1; j < n; j++)
     {
       if(a[i] == a[j])
       {
         for(k = j; k < n; k++)
         a[k] = a[k+1];
         freq[i]++;
         n--;
       }
     }
     printf(" \nArray with freq\n ");
     printf(" \nElement  Freq\n ");
     for(i = 0; i < n; i++)
     printf("%d  %d\n ", a[i], freq[i]);
     getch();
   }
Factorial

Factorial

  #include <stdio.h> 
  #include <conio.h> 
  long int factorial(int n); 
  void main() 
  { 
     int n; 
     clrscr(); 
     printf("Enter the number:\n"); 
     scanf("%d",&n); 
     printf("Factorial of %d is %ld",n,factorial(n)); 
     getch(); 
  } 
  long int factorial(int n) 
  { 
     if(n<=1) 
     { 
        return(01); 
     } 
     else 
     { 
        n=n*factorial(n-1); 
        return(n); 
     } 
  }
Program to reverse any number

Program to reverse any number

#include<stdio.h>
int main()
{
    int num,r,reverse=0;
 
    printf("Enter any number: ");
    scanf("%d",&num);
 
    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }
 
    printf("\nReversed of number: %d",reverse);
    return 0;
}


Output:

Enter any number: 375
Reversed of number: 573
Introduction to Union

Introduction to Union

Union

A union is a special data type available in C that enables you to store different data types in the same memory location.

You can define a union with many members, but only one member can contain a value at any given time.

Unions provide an efficient way of using the same memory location for multi-purpose.

Defining a Union

To define a union, you must use the union statement in very similar was as you did while defining structure.

The union statement defines a new data type, with more than one member for your program.

The format of the union statement is as follows:

union [union tag]

{
member definition;
member definition;
...
member definition;
} [one or more union variables];


The union tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition.

At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional.

Here is the way you would define a union type named Data which has the three members i, f, and str:

union Data

{
int i;
float f;
char str[20];
} data;


Now a variable of Data type can store an integer, a floating-point number, or a string of characters.

This means that a single variable ie. same memory location can be used to store multiple types of data.

You can use any built-in or user defined data types inside a union based on your requirement.
Introduction File Input / Output

Introduction File Input / Output

File I/O

A file represents a sequence of bytes, does not matter if it is a text file or binary file.

Opening Files

You can use the fopen( ) function to create a new file or to open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to control the stream.

Following is the prototype of this function call:

FILE *fopen( const char * filename, const char * mode );

Here filename is string literal which you will use to name your file and access mode can have one of the following values:

ModeDescription

r Opens an existing text file for reading purpose.

w Opens a text file for writing, if it does not exist then a new file is created.

Here your program will start writing content from the beginning of the file.

a Opens a text file for writing in appending mode, if it does not exist then a new file is created.

Here your program will start appending content in the existing file content.

r+ Opens a text file for reading and writing both.

w+ Opens a text file for reading and writing both.

It first truncate the file to zero length if it exists otherwise create the file if it does not exist.

a+ Opens a text file for reading and writing both. It creates the file if it does not exist.

The reading will start from the beginning but writing can only be appended.

If you are going to handle binary files then you will use below mentioned access modes instead of the above mentioned:

"rb", "wb", "ab", "ab+", "a+b", "wb+", "w+b", "ab+", "a+b"

Closing a File

To close a file, use the fclose( ) function. The prototype of this function is:
int fclose( FILE *fp );

The fclose( ) function returns zero on success, or EOF if there is an error in closing the file. This function actually, flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h.

There are various functions provide by C standard library to read and write a file character by character or in the form of a fixed length string. Let us see few of the in the next section.

Writing a File

Following is the simplest function to write individual characters to a stream:
int fputc( int c, FILE *fp );

The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error.

You can use the following functions to write a null-terminated string to a stream:

int fputs( const char *s, FILE *fp );

The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error.

You can use int fprintf(FILE *fp,const char *format, ...) function as well to write a string into a file.

Try the following example:

#include < stdio.h >
main()
{
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}

When the above code is compiled and executed, it creates a new file test.txt in /tmp directory and writes two lines using two different functions.

Reading a File

Following is the simplest function to read a single character from a file:

int fgetc( FILE * fp );

The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF.

The following functions allow you to read a string from a stream:
char *fgets( char *buf, int n, FILE *fp );

The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.
Program to find the strong number

Program to find the strong number

Strong Number
  void strong_number() 
  { 
    int num,i,p,r,sum=0,save_num; 
    printf("\n Enter a number:\t"); 
    scanf("%d",&num); 
    save_num=num; 
    while(num) 
    { 
        i=1,p=1; 
        r=num%10; 
    while(i<=r) 
      { 
        p=p*i; 
        i++; 
      } //while 
        sum=sum+p; 
        num=num/10; 
    } //while 
    if(sum==save_num) 
      printf("%d is a Strong number", save_num); 
    else 
      printf("%d is not a Strong number", save_num); 
  }


Output:

Enter a number:        153
153 is not a Strong number
Introduction to Loops

Introduction to Loops

Loops


While Loop:

A while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.

Syntax:

while(condition)
{
statement(s);
}

Here statement(s) may be a single statement or a block of statements.

The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

For Loop:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

for (initialization; condition; increment  / decrement)
{
statement(s);
}

Here is the flow of control in a for loop:

The initial step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated.

If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables.

This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition).

After the condition becomes false, the for loop terminates.

do...while Loop:

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming language checks its condition at the bottom of the loop.

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax:

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

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

Nested Loops:

C programming language allows to use one loop inside another loop.

Following section shows few examples to illustrate the concept.

Syntax:

for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}

The syntax for a nested while loop statement in C programming language is as follows:

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

The syntax for a nested do...while loop statement in C programming language is as follows:

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

A final note on loop nesting is that you can put any type of loop inside of any other type of loop.

For example a for loop can be inside a while loop or vice versa.

Loop Control Statements:

Break Statement:

The break statement in C programming language has following two usage:

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.

It can be used to terminate a case in the switch statement (covered in the next chapter).

If you are using nested loops ( ie. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax:

The syntax for a break statement in C is as follows:

break;

Continue Statement:

The continue statement in C programming language works somewhat like the break statement. 

Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.

For the for loop, continue statement causes the conditional test and increment portions of the loop to execute.

For the while and do...while loops, continue statement causes the program control passes to the conditional tests.

Syntax:

The syntax for a continue statement in C is as follows:

continue;

Go TO Statement:

A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function.

NOTE:

Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto.

Syntax:

The syntax for a goto statement in C is as follows:
goto label;
..
.
label: statement;

Here label can be any plain text except C keyword and it can be set anywhere in the C program above or below to goto statement.