Sunday, 11 December 2011

Structures


Derived types- 
structures- declaration, 
definition and initialization of structures, 
accessing structures, 
nested structures, 
Array of Structures
structures and functions, 
pointers to structures, 
self referential structures, 
Unions
typedef
Bit fields

Definition:
Structure is a collection of different data types. Each data is called its member.

1. Difference between Array and Structures

Structure
Array
1.       It is a collection of elements(variables) of different data types
1.       It is a collection of elements of same data type
2.       We have to use .(dot) operator or -> (cap) operator for accessing
2.       Subscript for accessing array elements
3.       Syntax:
struct  tag
{
  Datatype memeber1;
  Datatype member2;
  …….
};
3.       Declaration of array:
Datatype arrayname[size]={list of elements separated by comma};
4.       Example:
struct  student
{
 int rollno;
 char name[10];
 float avg;
};
Struct student stud={01,”raj\0”,90.25};
4.       int a[5]={1,2,3,4,5};


#include<conio.h>
#include<iostream.h>
struct student
{
char name[10];
int regno;
struct dob;
{
Int day,month,year;
}d;
Int m1,m2,m3,m4,a1,a2,a3,a4;
}s[10];
Void main();
{
Int I,j,n,total,avg;
Clrscr();
Printf(“enter the total no of students”);
Scanf(“%d”,&n);
For(i=0;i<n;i++)
{
printf(“enter the name of the student”);
scanf(“%s”,&s[i].name);
printf(“enter regno”);
scanf(“%d”,&s[i].regno);
printf(“enter the dob of student(dd\mm\yy):t”);
scanf(“%d%d%d,&s[i].d.day,&s[i].d.month,&s[i].d.year);
printf(“enter the marks in c language”);
scanf(“%d”,&s[i].m1);
printf(“enter the marks in physics”);
scanf(“%d”,&s[i].m2);
printf(“enter the marks in civil”);
scanf(“%d”,&s[i].m3);
printf(“enter the marks in mechanics”);
scanf(“%d”,&s[i].m4);
total=s[1].m1+s[i].m2+s[i].m3+s[i].m4;
avg=total/4;
printf(“the total and average of student is %d%d,total,avg);
}
Printf(“subject average\n”);
For(i=0;i<n;i++)
{
a1+=s[i].m1;
Sa1=a1/n;
a2+=s[i].m2;
Sa2=a2/n;
A3+=s[i].m3;
Sa3=a3/n;
A4+=s[i].m4;
Sa4=a4/n;
}
Printf(“%d”,sa1);
Printf(“%d”,sa2);
Printf(“%d”,sa3);
Printf(“%d”,sa4);
Getch();
}

Nested Structures: Structure in a structure is known as Nested Structures


//Program on Nested Structure
#include "stdio.h"
#include "conio.h"
struct adress {
char ct[10];
char dist[10],state[5];
long int pin;
};
struct emp 
{
 char name[10];
 int age,sal;
 struct adress a;
};
void main()
{
 struct emp e[2];
 int i;
 clrscr();
 for(i=0;i<=1;i++)
 {
   printf("Enter [%d]st Employee's Name,Age,salary :: ", i);
   fflush(stdin);
   gets(e[i].name);

   scanf("%d%d",&e[i].age,&e[i].sal);
   printf("\nEnter city, district,state & pincode ::");
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   scanf("%ld",&e[i].a.pin);
}

for(i=0;i<=1;i++)
{
printf("\n[%d]st Employee's Name :: %s",i,e[i].name);
printf("\n[%d]st Employee's Age :: %d ",i,e[i].age);
printf("\n[%d]st Employee's Salary :: %d",i,e[i].sal);
printf("\n[%d]st Employee's City :: %s ",i,e[i].a.ct);
printf("\n[%d]st Employee's District :: %s",i,e[i].a.dist);
printf("\n[%d]st Employee's State :: %s",i,e[i].a.state);
printf("\n[%d]st Employee's Pin :: %ld",i,e[i].a.pin);
}
getch();
}


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

struct stud_Res
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}result;

void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&result.rno);
printf("\n\t Enter Standard : ");
scanf("%s",result.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",result.rno);
printf("\n\n\t Standard : %s",result.std);
printf("\nSubject Code : %s",result.marks.subj_nm);
printf("\n\n\t Marks : %d",result.marks.subj_mark);
getch();
}


#include<stdio.h>
#include<conio.h>
typedef struct stud
{
 int pino;
 char ename[10];
 float avg;
};
void main()
{
 stud s;
 clrscr();
 s.pino=20;
 printf("%d\n%s\n%.2f",s.pino,s.ename,s.avg);

 getch();

}


#include<stdio.h>
#include<conio.h>
typedef struct emp
{
 int empno;
 char ename[20];
 char gender;
 float sal;
};
void main()
{
// struct emp e;
 emp e;
// typedef struct emp e;
 printf("enter the empno,ename,gender,sal\n");
 scanf("%d",&e.empno);
 fflush(stdin);
 gets(e.ename);
 fflush(stdin);
 scanf("%c%f",&e.gender,&e.sal);
 printf("employees are\n");
 printf("%d\n%s\n%c\n%.2f\n",e.empno,e.ename,e.gender,e.sal);
 getch();
}


#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct admission
{
 char name[20];
 int rank;
};
void main()
{
 int x;
 static struct admission s1={"rama",681};
 static struct admission s2;
 s2=s1;
 printf("contents of s2 are\n");
 printf("%s %d",s2.name,s2.rank);
 if((strcmp(s1.name,s2.name)==0)&(strcmp(s1.rank,s2.rank)==0)
 printf("both are same\n");
 else
 printf("both are not same\n");
 getch();
}

Tuesday, 6 December 2011

Loops

C Programming Loops:

Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used in performing repetitive task in programming. Consider these scenarios:
You want to execute some code/s 100 times.
You want to execute some code/s certain number of times depending upon input from user.
These types of task can be solved in programming using loops.
There are 3 types of loops in C programming:


1. for loop
2. while loop
3. do-while

Nested Loops
                                                Interview Questions

                How to find entered number is EVEN or ODD without using conditional statement(not using if.. else,if.. , else if..,while, do... while...., for....) 


                why n++ executes faster than n+1?       


                Can include files be nested?      


                Write a function to swap any two numbers?      


                Is NULL always defined as 0?     


                Nesting of loops in C may continue upto how many levels?         


                main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }             


                Give some explanation for "for" LOOP ON C LANGUAGE?            


                if "condition" printf("Hello"); else printf("World") what should be the condition, so the output will be HelloWorld.               


                x) main() { printf("%x",-1<<4); }               


                Which expression always return true? Which always return false?           


                How to print 1 to 100 without using any conditional loop?            


                What is the output of the following program: int main() { char buff[]={ 0x11,0x11,0x11,0x11,0x22,0x22,0x22,0x22,0x33,0x33,0x33,0x33}; char* pEnd = buff + sizeof(buff) -1; long * plong = buff; short* pshort = buff; for(; plong<pEnd; plong++) printf("0x%X ", plong); for(; pshort<pEnd; pshort++) printf("0x%X ", pshort); return 0; }         


                What is the output of the following program: int main() { char buff[]={ 0x11,0x11,0x11,0x11,0x22,0x22,0x22,0x22,0x33,0x33,0x33,0x33}; char* pEnd = buff + sizeof(buff) -1; long * plong = buff; short* pshort = buff; for(; plong<pEnd; plong++) printf("0x%X ", plong); for(; pshort<pEnd; pshort++) printf("0x%X ", pshort); return 0; }         


                Write a pro gramme to show bubble sort on a set of 10 numbers?           


                void main() { int x=20,y=35; x= y++ + x++; y= ++y + ++x; printf(" %d %d" , x,y); } /* please tell me the value of x and y at each and every moment. i dont need the answer but the explanation of the anamolus behavior of increment expression.*/   


                C Program to print the following series: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5              


                How can we find out prime numbers from 1 to 50?         


                How to decide even and odd values without decision making loops?      


                write down series in c language 1 232 34543 4567654


                fibbonaci series program             


                write a C program for to aggregate a 8*8 matrix to 4*4 matrix using formula b11=(a11+a12+a21+a22)?   


                /*Are my STATEMENTS in the following program correct
 EXCEPT IN CASE 3 THE ARE WRONG SO PLEASE CORRECT IT*/
 #include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int i=3;
int j;
clrscr();
//case 1: //when only post increment in the expr then all 'i' get initial value
j = (i + i++);
 printf(" POST");
printf(" i=%d j=%d", i ,j);
i=3;
j=0;
j = (i++ + i++ + i);
printf(" i=%d j=%d", i ,j);
//case2: //when only pre increment in the expr then all 'i' get final value
i=3;
j=0;
j = (++i + i);
printf(" PRE");
printf(" i=%d j=%d", i ,j);
i=3;
j=0;
j = (++i + ++i + i + ++i + ++i);
printf(" i=%d j=%d", i ,j);
//case 3:
/*when both pre and post increment in the expr then A] i++ will get initial 'i' value and B] ++i will get last incremented value C] i in the increment expression takes the last second incremented value means if final ++i/i++ gives value 5 then simply 'i' will posses value 4 irrespective of the positon of i in the increment exp*/
printf(" POST AND PRE");
i=3;
j=0;
j = ( i++ + i + ++i + ++i +i + i + i++);
printf(" i=%d j=%d", i ,j);
i=3;
j=0;
j = (i++ + ++i + i);
printf(" i=%d j=%d", i ,j);
getch();
}       


                Write a c program to print the following program 1 121 12321 1234321 123454321              


                code to print the series 1+2/3+45+6/7
Write a program to print hello world without using semi colon?
Ans:

# include <stdio.h>
void main()
{
   while(printf("hello world")>20)
   {
   }
}


#define C(ch) \
__asm mov  eax, ch \
__asm push eax \
__asm mov  eax, putchar \
__asm call eax \
__asm pop  ebx


void main()
{
C('H')C('e')C('l')C('l')C('o')
C(' ')
C('W')C('o')C('r')C('l')C('d')
C('\r')C('\n')
}


#include<stdio.h>

int main()
{
if(printf("Hello world\n"))
return 0;
}


Download the ppt
Interview Questions on for loops