-->

Facebook

Jasper Roberts - Blog

Friday, May 1, 2015

DOWNLOAD GTU CPU OLD QUESTION PAPAERS

DOWNLOAD GTU CPU QUESTION PAPERS   -  CLICK HERE

Tuesday, April 28, 2015

Write a program using pointer to compare two strings.

#include<stdio.h>

int compare_string(char*, char*);

main()
{
    char first[100], second[100], result;

    printf("Enter first string\n");
    gets(first);

    printf("Enter second string\n");
    gets(second);

    result = compare_string(first, second);

    if ( result == 0 )
      printf("Both strings are same.\n");
    else
      printf("Entered strings are not equal.\n");

    return 0;
}

int compare_string(char *first, char *second)
{
  while(*first==*second)
  {
     if ( *first == '\0' || *second == '\0' )
        break;

     first++;
     second++;
  }
  if( *first == '\0' && *second == '\0' )
     return 0;
  else
     return -1;
}

Output:
Enter first String:HI
Enter Second String:HI
Both string are same

Write a program using pointer to concate two strings.

#include<stdio.h>
#include<conio.h>

void main()
{
  int i=0;
  char *str1,*str2,*str3;

  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);

  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);

  while(*str1)
  {
      str3[i++]=*str1++;
  }
  while(*str2){
      str3[i++]=*str2++;
  }

  str3[i]='\0';
  printf("After concatenation the strings are\n");
  puts(str3);
  getch();

}

Write a program using pointer to read an array if integer

#include<stdio.h>
void main()
{
int a[5]={5,4,6,8,9};
int *p=&a[0];
int i;

clrscr();

for(i=0;i<5;i++)
printf("%d",*(p+i));

for(i=0;i<5;i++)
printf(" %u\n",(p+i));

getch();
}

Write a program to find factorial of a number using recursion.

#include<stdio.h>
#include<conio.h>
int fac(int n);

void main()
 {
  int x;
  long int a;
  clrscr();
  printf("enter the no:");
  scanf("%d",&x);
  a=fact(x);
  printf("factorial=%d",a);
  getch();
 }
 int fact(int n)
 {
 if(n==1)
  return 1;
 else
  return n*fact(n-1);
 }

Write a function power that computes x raised to the power y for integer x and y and returns double type value.

#include<stdio.h>
#include<conio.h>
#include<math.h>

int power(int a,int b);
void main()
 {
  int x,y;
  clrscr();
  printf("enter the x:");
  scanf("%d",&x);
  printf("enter the y:");
  scanf("%d",&y);
  power(x,y);
  getch();
 }
int power(int a,int b)
 {
  double c;
  c=pow(a,b);
  printf("\n ans=%lf",c);
  return 0;
 }

Write a program using pointer and function to determine the length of string.

#include<stdio.h>

#include<conio.h>
#include<stdio.h>
#include<conio.h>
void ptr();

void main()
{
clrscr();
ptr();
getch();
}
void ptr()
{

    char s[100];
    char *p;
    int length;

    printf("\n Enter the string:");
    scanf("%s",s);

    for(p=s;*p!='\0';p++);

    length=p-s;

    printf("Length of the string is::%d",length);
     }

Friday, April 17, 2015

Chapter - 7 Structure

Structure                                                        |CLICK HERE
Accessing the structure members                |CLICK HERE
Array of Structure                                           |CLICK HERE
Difference between Structure and Union       |CLICK HERE

Wednesday, April 15, 2015

Pointers and Structures

For representing complex data structures, we can use structures and pointers together.

Pointer can be a member of structure, or a pointer to a structure, or an array of pointers to structure.

We can declare a pointer to structure as we do for pointers to basic data types.

Structure Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            }struct_variable, ponter-to-struct_variable;

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are written in curly { } braces.
The structure is terminated by semicolon (;).

Structure to Pointer Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }s1, *sptr;

sptr = &s1;

Now, sptr points to s1 and can be used to access member variable of struct student.

To access member variables using pointer to structure, we have to use an arrow -> operator.
Syntax:
            struct_pointer -> member_variable;
Here, struct_pointer is a pointer to structure, and member_variable is the name of member variable.


Example:
            s1 -> rollno = 20;

We can also use dot (.) operator to access member variable with pointer to structure like:
            (*s1).rollno = 20;


Difference between Structure and Union

Structure
Union
1. The keyword  struct is used to define a structure
1. The keyword  union is used to define a structure
2. In structure, each member has its own separate space.
2. In union, all members share the storage space.
3. Memory occupied is the total required to store all the members.
3. Memory occupied is only that much which is required to store the largest member of union.
4. All members can be accessed at anytime.                                                      
4. Only one member can be activated at a time.
5. Syntaxt:
struct strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
           }

5. Syntaxt:
union strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
    }
6. Example:
struct std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = 2 + 20+ 4
                                 = 26
6. Example:
union std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = max( 2, 20, 4)
                                 = 20

Array of Structure

As we have an array of basic data types, same way we can have an array variable of structure.

Suppose we want to store details of 50 students instead of declaring 50 different variables of student type, we can declare an array of size 50 to store details of 50 students.

Example:
            struct student
            {
                        int id;
                        char name[30];
                        float per;
            };

            struct student s[50];


Program:

A C Program to enter the records of 5 students using structure.

#include<stdio.h>
#include<conio.h>

struct stdinfo
{
            int rn;
            float per;
};

void main()
{
            struct stdinfo s[3];
            int i;
            clrscr();
                       
            for(i=1;i<=3;i++)
            {
                        printf("\n Enter the student roll no.:");    
                        scanf("%d",&s[i].rn);
                        printf("\n Enter the student percentage.:");         
                        scanf("%d",&s[i].per);
            }

for(i=0;i<=5;i++)
{
            printf("\n Roll Number \t\t Percentage");
            printf("\n %d \t\t %f",s[i].rn,s[i].per);
}
getch();
}         

OUTPUT:
Enter the student roll no.: 1
Enter the student percentage.: 67
Enter the student roll no.: 2
Enter the student percentage.: 76
Enter the student roll no.: 3
Enter the student percentage.: 88

Roll Number        Percentage
       1                      67
       2                      76
       3                      88 

Structure

Structure is a collection of logically related data items of different data types grouped together and known by a single name.
Structure is similar to records in DBMS.
As record consists of fields, the data items of a structure are called as its members.
Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            };

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are4 written in curly { } braces.
The structure is terminated by semicolon (;).

Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }



Accessing the structure members:

We can access the individual members of a structure using the dot (.) operator.
The dot (.) operator is called as structure member operator because it connects the structure variable and its member’s variable.

Syntax:
            struct_variable.struct_member;
Where, struct_var is a variable of structure type, while struct_member is a name of a member variable of structure.
Example:
            struct student s1;
            s1.rollno = 1;
           

Structure Example:

A C Program to input and print following details of student using structure. (roll number, name, address, city, age, per)

#include<conio.h>
#include<stdio.h>

struct student
{
            int rn;
            char name[20];
            char address[50];
            char city[30];
            int age;
            float per;
}

void main()
{
            struct student s;
            clrscr();
           
            printf(“Enter the roll number:”);
            scanf(“%d”,&s.rn);
            printf(“\n Enter the name:”);
            gets(s.name);
            printf(“\n Enter the address:”);
            gets(s.address);
            printf(“\n Enter the city”);
            gets(s.city);
printf(“\n Enter the age:”);
            scanf(“%d”,&s.age);
printf(“\n Enter the percentage:”);
            scanf(“%f”, &s.per);


            printf(“\n Roll-Number\t Name\t Address\t City\t Age\t Percentage”);
            printf(“\n %d\t %s \t %s \t %s \t %d \t %f”);

            getch();
}

OUTPUT:
Enter the roll number: 28
Enter the name: John
Enter the address: Church Road
Enter the city: Miami
Enter the age: 24
Enter the percentage: 88.00

Roll-Number               Name               Address           City                 Age Percentage

28                                John                 Church Road   Miami 24        88.00

Tuesday, April 14, 2015

A C Program to find out factorial of a given number using recursion.

#include<conio.h>
#include<stdio.h>
int fact(int p);

void main()
{
            int n, ans;
            clrscr();

            printf(“Enter the number:”);
            scanf(“%d”,&n);

            ans = fact(n);

            printf(“Factorial of  a given number %d is %d”,n,ans);
            getch();
}
int fact(int p)
{
            if(p==1)
            {
            return 1;
            }
            else
            {
            return p*fact(p-1);
            }
}

OUTPUT:
Enter the number: 3
Factorial of  a given number 3 is 6

Monday, April 13, 2015

Recursion:

-         Recursion is the process by which a function calls itself.
-         Sometimes, the function is defined in terms of itself. For Example, factorial of a number, we say that N! = N* (N-1). Here, factorial of N is defined in terms factorial of N-1.

Advantages: 
1.      Provides easy solution for recursively defined problems. 
2.      Complex programs can be easily written with less code.


Disadvantages:
1.      Recursive code is difficult to understand and debug.
2.      Terminating condition must be required otherwise it will go in infinite loop.
3.      Execution speed decreases because of function call and return activity many times.


Example:

A C Program to find out factorial of a given number using recursion.

#include<conio.h>
#include<stdio.h>
int fact(int p);

void main()
{
            int n, ans;
            clrscr();

            printf(“Enter the number:”);
            scanf(“%d”,&n);

            ans = fact(n);

            printf(“Factorial of  a given number %d is %d”,n,ans);
            getch();
}
int fact(int p)
{
            if(p==1)
            {
            return 1;
            }
            else
            {
            return p*fact(p-1);
            }
}

OUTPUT:
Enter the number: 3
Factorial of  a given number 3 is 6