Facebook

Jasper Roberts - Blog
Showing posts with label cpu(2110003) theory. Show all posts
Showing posts with label cpu(2110003) theory. Show all posts

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

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


Saturday, April 4, 2015

Difference between:



Difference Between gets() and scanf()

gets()
scanf()
1.     It is used to read string.
1.     It is used to read integer, character, float and string also.
2.     It can read string having white space, tabs.
2. It cannot read string having white space.




Difference Between puts() and printf()
puts()  
printf()
1.     It is used to print string.
1.     It is used to print integer, float, character and string also.
%s specifier required to print string.
2.     It will automatically print new line after the string.
2.     It does not automatically print the new line character.
We can provide newline by using “\n”


Two Dimensional Arrays

Two Dimensional Arrays

-         Sometimes we need to store the data where more than one dimensions are involved, like sales information of a company, or for mathematical calculations we need to use matrix.

-         To declare a two-dimensional integer array of size i,j,  you would write something as follows:

-         Syntax:
Data-Type Variable-Name[row-size][col-size];

Where , Data-Type may be int, char or float,
Variable-Name represents name of the array,
Row-size indicate the number of row in table,
Col-size indicate the number of columns in an array.
-         Example:
                     int matrix[3][3];

-         In above example, int is a datatype and matrix is an array name. Here, the size of row is 3 and size of column is 3.






In above figure, the cell a[i][j] refers to the cell in ith row jth column.
So,
matrix[0][0]=10,
matrix[1][0]=20,
matrix[2][0]=30,
matrix[0][1]=40,
matrix[1][1]=50,
matrix[2][1]=60,
matrix[0][2]=90,
matrix[1][2]=80,
matrix[2][2]=70,
and like that.
So , solving mathematical problems involving matrices, we can use two dimensional array.