Assign 3 Full
Assign 3 Full
Assign 3 Full
CS ASSIGNMENT-3
pointers
Q-1: Define pointers and declaration syntax of pointers with examples.
Ans: A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, we must declare a pointer before we can
use it to store any variable address.
For example:
int *ptra;
char *ptrb;
float *ptrc;
Q-2: Explain the difference between null pointer and void pointer.
Ans: Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved
value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-
pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
On the other hand, void pointer is a specific pointer type - void * - a pointer that points to
some data location in storage, which doesn't have any specific type.
Ans: In array of pointers, each element of the array is an address to memory location which holds
the data.
For example:
int *ptr[100];
On the other hand, pointer to an array is a pointer which points to the address of any element
of a pre-declared array (generally the base element).
For example:
int A[3]={28,7,32};
int *ptr;
*ptr=A;
This pointer is pointing to the address of the base element of the array A i.e. A[0].
Q-5: Explain the term dynamic memory allocation. Also differentiate between malloc(),
calloc() and realloc() with a program as an example.
Ans: The process of allocating memory during program execution is called dynamic memory
allocation. That is, the memory which is allocated during run-time is called dynamic memory
allocation.
1. malloc() function:
malloc() function is used to allocate space in memory during the execution of the program.
malloc() does not initialize the memory allocated during execution. It carries garbage value.
malloc() function returns null pointer if it couldnt able to allocate requested amount of
memory.
For example:
int *ptr;
ptr=(int*)malloc(20*sizeof(int));
For the above, 20*4 bytes of memory only allocated in one block.
Total = 80 bytes.
2. calloc() function:
calloc() function is also like malloc() function. But calloc() initializes the allocated memory
to zero. But, malloc() doesnt.
For example:
int *ptr;
ptr=(int*)calloc( 20,sizeof(int));
For the above, 20 blocks of memory will be created and each contains 20*4 bytes of
memory. Total = 1600 bytes
3. realloc() function:
realloc() function modifies the allocated memory size by malloc() and calloc() functions to
new size. If enough space doesnt exist in memory of current block to extend, new block is
allocated for the full size of reallocation, then the existing data is copied to new block and
then the old block is free.
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation=malloc(20*sizeof(char));
if(mem_allocation==NULL)
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"How are you?");
}
printf("Dynamically allocated memory content: %s\n",mem_allocation);
mem_allocation=realloc(mem_allocation,100*sizeof(char));
if(mem_allocation==NULL)
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy(mem_allocation,"Space is extended upto 100 characters");
}
printf("Resized memory: %s\n",mem_allocation);
free(mem_allocation);
}
Q-6: Differentiate between pointers to constant and constant to pointers.
These type of pointers are the one which cannot change address they are pointing to. This
means that suppose there is a pointer which points to a variable (or stores the address of that
variable). Now if we try to point the pointer to some other variable (or try to make the pointer
store address of some other variable), then constant pointers are incapable of this.
Pointer to constant:
These type of pointers are the one which cannot change the value they are pointing to. This
means they cannot change the value of the variable whose address they are holding.
ptr++ statement is valid in pointer to constant ptr++ statement is invalid in constant pointers
Pointer can be incremented and decremented Pointer cannot be incremented and decremented
Pointer is pointing to constant data object Constant pointer is pointing to data objects
Declaration: const int *ptr; Declaration: int *constptr;
Ans: The ++ operator adds the sizeof(datatype) number of bytes to the pointer so that it to the next
entry of the datatype.
For example:
int *ptr;
ptr++;
This adds 2 or 4 bytes (depends upon compiler) to the pointer so that it points to the next
entry of the datatype.
On the other hand, *ptr++ increments the value of the variable which the pointer is pointing
to by 1.
For example:
int a=10;
int *ptr;
ptr=&a;
*ptr++;
Q-8: How can we have array of function pointers? Illustrate with examples.
Ans: Arrays follow the normal C syntax of putting the brackets near the variable's identifier, so:
int (*ptr_array[4])( int ) declares a variable called ptr array which is an array of 4 function
pointers.
For example:
#include<stdio.h>
int display();
int(*arr[4])();
int(*(*ptr)[4])();
int main()
{
arr[0]=display();
arr[1]=getch();
ptr=&arr;
printf("%d",(**ptr)());
(*(*ptr+1))();
return 0;
}
int display()
{
int num = 100;
return num;
}
100
Output: 25
int(*arr[4])();
int(*(*ptr)[4])();
ptr=&arr;
(**ptr)();
(*(*ptr+1))();
Ans: A memory leak is memory which hasn't been freed, there is no way to access (or free it) now,
as there are no ways to get to it anymore. (For example: a pointer which was the only
reference to a memory location dynamically allocated (and not freed) which points
somewhere else now.
For example:
void func()
{
char *ch;
ch=(char*)malloc(10);
}
char *ch is a local variable that goes out of scope at the end of the function, leaking the
dynamically allocated 10 bytes.
Ans: Dangling pointers are pointers that do not point to a valid object of the appropriate type.
In many applications memory is allocated for holding data objects. After using these objects,
the application will de-allocate this memory so that the memory can be re-used. In some cases
the allocations may use a pointer to an object whose memory is already de-allocated. This
may lead to application crash or an unpredictable behaviour.
Application makes use of an object after it has been released, and there by access to an invalid
memory location.
A function returns a pointer to one of its local variables, and sifnce this local variable is
defined only for the function, the pointer becomes invalid once the function ends.
The most common result of this bug is the crash of the application or its running thread.
For example:
#include<stdio.h>
main()
{
int *p;
p=increment(1);
printf(%d,*p);
}
int *increment(int n)
{
int *ptr=&temp;
temp=n+1;
return ptr;
}
The function returns the address of temp, but temp is erased as soon as increment(n) returns.
So the returned value is not a meaningful address to the calling function. The problem is that
anything allocated for the called function on stack is erased as soon as it returns. This is a
dangling pointer problem.
Ans: If arr is the name of an array, it acts as a pointer constant to the first element of the array.
Then, *(arr+i) returns the value (i.e. the content) of the (i+1)th element of the array.
Whereas, (arr+i) returns the address of the (i+1)th element of the array.
Ans: 57 59 79
Q-14: Write a program to print alternate letters of a string (Example: NITSilchar will
produce: NTica).
Ans: #include<stdio.h>
#include<string.h>
main()
{
int i,l=0;
char str[100];
printf("Enter a string (max 100 characters): ");
gets(str);
for(i=0;str[i]!='\0';i++)
l++;
for(i=0;i<l;i+=2)
printf("%c",str[i]);
}
Q-15: Write a program to obtain the number of elements from the user, store it dynamically in
memory and find out the smallest element in the list.
Ans: #include<stdio.h>
#include<stdlib.h>
main()
{
int *a,n,i,small;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=a[0];
for(i=0;i<n;i++)
{
if(small>a[i])
small=a[i];
}
printf("The smallest element is: %d\n",small);
}
Ans: #include<stdio.h>
main()
{
int a[10],*add[10],i;
printf("Enter 10 elements of array:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nThe details of the array are:\n\nName\t\tAddress\t\tContent\n\n");
for(i=0;i<10;i++)
{
add[i]=&a[i];
printf("a[%d]\t\t%d\t\t%d\n",i,add[i],*add[i]);
}
}
Ans: #include<stdio.h>
main()
{
int i,l=0;
char str[50],*ptr;
printf("Enter a string (max 50 characters): ");
gets(str);
for(i=0;*(str+i)!='\0';i++)
l++;
ptr=str+(l-1);
printf("\nThe reversed string using pointer is:\n\n");
for(i=0;i<l;i++)
printf("%c",*(ptr-i));
}
Q-19: Write a program to implement call by reference using functions to swap two integer
numbers.
Ans: #include<stdio.h>
main()
{
int a,b;
printf("Enter two numbers a and b: ");
scanf("%d%d",&a,&b);
printf("\nBefore function call:\na=%d\tb=%d\n",a,b);
swap(&a,&b);
printf("\nAfter function call:\na=%d\tb=%d\n",a,b);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Q-20: Write a program to copy a string using pointers and not using strcpy function.
Ans: #include<stdio.h>
main()
{
int i=0,l=0;
char *str1[50],*str2[50];
printf("Enter a string (max 50 characters): ");
gets(str1);
for(i=0;*(str1+i)!='\0';i++)
l++;
for(i=0;i<l;i++)
*(str2+i)=*(str1+i);
printf("\nThe copied string is: ");
puts(str2);
}
Q-22: Write a program to read and print a floating point array. The space for the array
should be allocated at the run time.
Ans: #include<stdio.h>
#include<stdlib.h>
main()
{
float *a;
int i,n;
printf("Enter the size of array: ");
scanf("%d",&n);
a=(void*)malloc(n*(sizeof(float)));
printf("Enter %d floating point numbers: ",n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n;i++)
printf("\na[%d]=%f\tAddress of a[%d]=%d",i,a[i],i,&a[i]);
}
Ans: #include<stdio.h>
main()
{
char let[12]="HELLO WORLD";
int i;
char *ptr[12];
for(i=0;i<11;i++)
{
ptr[i]=&let[i];
printf("%c",*ptr[i]);
}
}
Q-25: Write a program to find smallest of three integers values using pointer.
Ans: #include<stdio.h>
main ()
{
int *ptr1,*ptr2,*ptr3,a,b,c,max=0;
printf("Enter 3 numbers: ");
scanf("%d%d%d",&a,&b,&c);
ptr1=&a;
ptr2=&b;
ptr3=&c;
if(*ptr1>*ptr2)
{
if(*ptr1>*ptr3)
max=*ptr1;
else
max=*ptr3;
}
else
{
if(*ptr2>*ptr3)
max=*ptr2;
else
max=*ptr3;
}
printf("The maximum of the three numbers is %d",max);
}
structure
Q-1: Define a structure. Mention its advantages and disadvantages.
Ans: A structure is a complex data type declaration that defines a physically grouped list of
variables to be placed under one name in a block of memory, allowing the different variables
to be accessed via a single pointer, or the struct declared name which returns the same
address. The struct can contain many other complex and simple data types in an association.
Structures are used to group together different types of variables under the same name.
structtag_name
{
type member1;
type member2;
/* declare as many members as desired, but the entire structure size must be known to
the compiler. */
};
For example, telephone, which is made up of a string (that is used to hold the name of the
person) and an integer (that is used to hold the telephone number).
struct telephone
{
char name[20];
int number;
};
Advantage :
1) You can implement all the OOPS concepts like Emulating classes, inheritance, and
polymorphism in C.
2) I believe that besides being useful in its own right, implementing OOP in C is an
excellent way to learnOOP and understand its inner workings. Experience of many
programmers has shown that to use a technique efficiently and confidently, a programmer
must understand how the underlying concepts are ultimately implemented.
Disadvantage:
1) As C does not provide the facilities of OOPS therefore you have to implement the
OOPS feature that can take some time and will be complex initially. This can be a
problem if you want to finish the project very soon. (Though i don't consider this as a
disadvantage as amount of learning in the process is huge.)
Ans: Yes, a structure variable can be defined as a member of another structure. Structure within a
structure means nesting of structures.
struct student
{
char name[50];
char department[50];
struct
{
int jee marks;
int roll no;
}cpi;
}player;
Here the structure student contains a member named cpi, which is itself a variable of a
structure.
Q-3: Can an array be included as a member of a structure? Can an array have structures as
elements?
Ans: Yes, an array can be included as a member of a structure. C permits the use of arrays as
structure members. We can use single dimensional or multi-dimensional arrays as structure
members.
For example:
struct marks
{
int number;
float subject[3];
}student[2];
Yes, an array can have structures as elements. It is possible to define an array of structures i.e.
an array in which each element is a structure.
For example:
struct book
{
char bname[20];
int pages;
char author[20];
float price;
}b1[2]={{"DA VINCI CODE",1000,"DAN BROWN",580.00},{"Immortals of
meluha",800,"Amish Tripathi",250.00}};
Explanation :
As soon as after declaration of structure we initialize structure with the pre-defined values.
For each structure variable we specify set of values in curly braces. Suppose we have 2 array
elements then we have to initialize each array element individually and all individual sets are
combined to form single set.
Q-4: What is the precedence of the period (.)operator? What is its associativity?
Ans: In precedence order, it is a third position. It is preceded by () operator (function call) and []
operator (array subscript).
Q-5: How can the size of a structure be determined? In what units is the size reported?
Ans: The size of a structure can be determined using the unary operator sizeof.
The expression sizeof(struct x); will evaluate the number of bytes required to hold all the
members of the structure x.
The sizeof operator returns the number of bytes the operand occupies. It returns only an
integer, no unit.
Q-6: What is the precedence of the -> operator? What is its associativity?
Ans: The -> operator (member selection via pointer operator) is at the fourth position in the
precedence order list, preceded by the ( ), [ ] and . operators.
Q-7: Suppose a pointer variable points to a structure that contains another structure as a
member. How can a member of the embedded structure be accessed?
Ans: The member of the embedded structure can be accessed in the following way by using the
arrow operator ->.
#include<stdio.h>
struct data1
{
int a;
int b;
};
struct data2
{
struct data1 x;
int c;
};
main()
{
struct data2 *ptr;
ptr=(struct data2*)malloc(sizeof(struct data2));
printf("Enter the values of a,b and c: ");
scanf("%d%d%d",&ptr->x.a,&ptr->x.b,&ptr->c);
printf("\na=%d\tb=%d\tc=%d\n",ptr->x.a,ptr->x.b,ptr->c);
}
Q-8: Suppose a pointer variable points to a structure that contains an array as a member.
How can an element of the embedded array be accessed?
Ans: The elements of an embedded array can be accessed by using the arrow operator -> and for
loop.
Ans: An union may be a member of a structure and a structure may be a member of a union.
Moreover, structures and unions may be freely mixed with arrays.
For example:
struct student
{
char name[30];
char sex;
int rollno;
float percentage;
};
union details
{
struct student st;
};
Q-10: How is a union member accessed? How can a union member be processed?
Ans: The members of a union can be accessed in a similar way as the members of a structure using
the . operator and -> operator. Thus if variable is a union variable, variable.member refers to
a member of the union. Similarly, if ptvar is a pointer variable that points to a union, then
ptvar->member refers to a member of that union. During accessing a union member, we
should make sure that we are accessing the member whose value is currently stored.
A union member can be processed in the same manner as ordinary variables of the same data
type. However, only one member can be processed at a time. Union members can appear in
expressions, they can be passed to functions and they can be returned from functions . A
union member that is an array can be processed in the same manner as an ordinary array and
with the same restrictions. Moreover, most C compilers permit an entire union to be assigned
to another, provided both unions have the same composition.
Q-11: How is a member of a union variable assigned an initial value? In what way does the
initialization of a union variable differ from the initialization of a structure variable?
Ans: A union variable can be initialized provided its storage class is either external or static.
However, only one member of a union can be assigned a value at any one time. When
initializing a union, the initializer list must have only one member, which initializes the first
member of the union unless a designated initializer is used.
For example:
union
{
int x;
char c[4];
}u={1}: // makes u.x active with value 1
Union is similar to that of structure. Syntax of both are same but major difference between
structure and union is memory storage. In structures, each member has its own storage
location, whereas all the members of union use the same location. Union contains many
members of different types. Union can handle only one member at a time. So, only one
member of a union can be initialized at a time unlike structures where we can initialize one or
all the members at a time.
Q-12: Summarize the rules that apply to processing unions. Compare with the rules that apply
to processing structures.
For example :
union car
{
char name[50];
int price;
}c1,c2,*c3;
#include <stdio.h>
union job //defining a union
{
char name[32];
float salary;
int worker_no;
}u;
struct job1
{
char name[32];
float salary;
int worker_no;
}s;
main()
{
printf("Size of union = %d",sizeof(u));
printf("\nSize of structure = %d", sizeof(s));
}
Output:
size of union = 32
size of structure = 40
We know that all members of structure can be accessed at any time. But, only one
member of union can be accessed at a time in case of union and other members will
contain garbage value.
#include <stdio.h>
union job
{
char name[32];
float salary;
int worker_no;
}u;
main()
{
printf("Enter name:\n");
scanf("%s",&u.name);
printf("Enter salary: \n");
scanf("%f",&u.salary);
printf("Displaying\nName :%s\n",u.name);
printf("Salary: %.1f",u.salary);
}
Output:
Q-14: Define a structure called cricket that will describe the following information
a. Players name,
b. team name,
c. batting average
And also write a program to give the information about the semi-final of world cup 2015
between India and Australia using structures. Information should contain players
name, team name, batting average, scores of each players and total score of each team.
Ans: #include<stdio.h>
#include<string.h>
struct cricket
{
char name[20],team[10];
float avg;
int run;
};
main()
{
int i,j;
struct cricket in[11],aus[11];
printf("######### Enter the details of the players######### ");
printf("\n \n \n \n Enter the details of the Indian players: ");
for(i=0;i<11;i++)
{
strcpy(in[i].team,"India");
printf(" \n\n**** Enter the details of the player %d ****\n",i+1);
printf("Name: ");
scanf(" %s",in[i].name);
printf("Batting average: ");
scanf("%f",&in[i].avg);
printf("Runs scored: ");
scanf("%d",&in[i].run);
}
printf("\n\n\n\n Enter the details of Australian players: ");
for(j=0;j<11;j++)
{
strcpy(aus[j].team,"Australia");
printf("\n\n**** Enter the details of the player %d ****\n",j+1);
printf("Name: ");
scanf(" %s",aus[j].name);
printf("Batting average: ");
scanf("%f",&aus[j].avg);
printf("Runs scored: ");
scanf("%d",&aus[j].run);
}
printf("\n\n**Match summary CWC'15 2nd semifinal INDIA vs AUSTRALIA**");
printf("\n AUSTRALIA - 328/7 \n INDIA - 223/10 \n Australia won by 95 runs \n") ;
printf(" \n\n \t\tScorecard\n\n\t\tAUSTRALIA\n");
printf( "\n Sl.No.\tPlayer\t\tBatting Avg\tRuns scored");
for(j=0;j<11;j++)
printf("\n%d\t%s\t\t%.2f\t%d ",j+1,aus[j].name,aus[j].avg,aus[j].run);
printf("\n\n\t\tINDIA\n");
for(j=0;j<11;j++)
{
printf( "\n%d\t%s\t\t%.2f\t%d",j+1,in[j].name,in[j].avg,in[j].run);
}
}
Q.15: Write a program to store student information such as name, roll number, marks, subjects
and display the same for 100 students. Also access the 5th, 6th and 7th elements of structures and
display the information separately.
SOLUTION:
#include<stdio.h>
struct student
{
char name[20];
int roll;
char sub1[10],sub2[10],sub3[10];
int m1,m2,m3;
};
void main()
{
struct student s[100];
printf(enter the details \n\n);
for(int i=0;i<100;i++)
{
printf( \n enter details for student number %d,i+1);
printf(\n enter the name);
scanf( %s,s[i].name);
printf(\n enter roll number);
scanf( %d,&s[i].roll);
printf(enter the first subject and its marks);
scanf( %s%d ,s[i].sub1,&s[i].m1);
printf(enter the second subject and its marks);
scanf( %s%d ,s[i].sub2,&s[i].m2);
printf(enter the first subject and its marks);
scanf( %s%d ,s[i].sub3,&s[i].m3);
}
printf(\n \n printing details);
for(int i=0;i<100;i++)
{
printf( \n details for student number %d,i+1);
printf(name : %s,s[i].name);
printf(\n roll number : %d,s[i].roll);
printf( subject marks \n);
printf( %s %d ,s[i].sub1,s[i].m1);
scanf( \n%s %d ,s[i].sub2,s[i].m2);
scanf( \n%s %d ,s[i].sub3,s[i].m3);
}
printf(\n \n \n);
printf( \n details for student number 5);
printf(name : %s,s[i].name);
printf(\n roll number : %d,s[4].roll);
printf( subject marks \n);
printf( %s %d ,s[4].sub1,s[4].m1);
printf( \n%s %d ,s[4].sub2,s[4].m2);
printf( \n%s %d ,s[4].sub3,s[4].m3);
printf(\n \);
printf( \n details for student number 6);
printf(name : %s,s[5].name);
printf(\n roll number : %d,s[5].roll);
printf( subject marks \n);
printf( %s %d ,s[5].sub1,s[5].m1);
printf( \n%s %d ,s[5].sub2,s[5].m2);
printf( \n%s %d ,s[5].sub3,s[5].m3);
printf(\n );
printf( \n details for student number 7);
printf(name : %s,s[6].name);
printf(\n roll number : %d,s[6].roll);
printf( subject marks \n);
printf( %s %d ,s[6].sub1,s[6].m1);
printf( \n%s %d ,s[6].sub2,s[6].m2);
printf( \n%s %d ,s[6].sub3,s[6].m3);
}
16. Write a program to define a structure called employee , that would contain employee name,
designation, department, salary, date of joining. Use the concept of nested structure in this program
for date of joining.
Solution :
#include <stdio.h>
struct doj
{
int day,month,year;
};
struct employee
{
char name[100];
char desgn[100];
char dept[100];
struct doj d;
};
main()
{
struct employee detail;
printf("enter name:\n");
gets(detail.name);
printf("enter designation:\n");
gets(detail.desgn);
printf("enter department:\n");
gets(detail.dept);
printf("enter date of joining:\n");
scanf("%d %d %d",&detail.d.day,&detail.d.month,&detail.d.year);
printf("\n\n\n");
printf("name of the employee :");
puts(detail.name);
printf("designation of the employee:");
puts(detail.desgn);
printf("department of the employee: ");
puts(detail.dept);
printf("date of joining of the employee %d/%d/%d" ,detail.d.day,detail.d.month,detail.d.year);
}
17. Describe the output generated by the following program. Distinguish between meaninful and
meaningless output.
Solution :
The output clearly indicates that the memory required to store a union variable is the
memory required for largest element of an union.
And this is the reason for getting 8 as the size of the union variable , because the largest element is of
double type.
As we go through the further outputs we observe that only one variable has a legal value and other
two are assigned with garbage values .
This is because only one member of union can be accessed at a time in case of union and
other members will contain garbage value.Thus as we initialse u.f , the memory is
reallocated for storing the value at u.f and thus the value at u.i is removed .
Q18.Write a C program that will accept the following information for each team in a baseball
or a football league.
1.Team name, including the city (e.g., Pittsburgh Steelers) 2. Number of wins 3. Number of
losses For a baseball team, add the following information: 4. Number of hits 5. Number of
runs 6.Number of errors 7. Number of extra-inning games
Similarly, add the following information for a football team: 4. Number of ties 5. Number
of touchdowns 6.Number of field goals 7.Number of turnovers 8. Total yards gained
(season total) 9. Total yards given up to opponents
Enter this information for all of the teams in the league. Then reorder and print the list of
teams according to their win-lose records, using the reordering techniques. Store the
information in an array of structures, where each array element (i.e., each structure)
contains the information for a single team. Make use of a union to represent the variable
information (either baseball or football) that is included as a part of the structure. This
union should itself contain two structures, one for baseball-related statistics and the other
for football-related statistics. Test the program using a current set of league statistics.
SOLUTION
#include<stdio.h>
struct team
{
char *name_city;
int wins,loss;
union game detail;
};
struct baseball
{
int hit,run,error,exinn;
};
struct football
{
int tie,goal,touchdown,turnover,yardgain,yardgiven;
};
union game
{
struct baseball bb;
struct football fb;
};
main()
{
clrscr();
int n,i,j,a;
struct team t[100];
printf(" \n enter the number of teams in the league ( max 100) ");
scanf("%d",&n);
printf("\n input console \n for football league press 1 \n for base ball league press any other key \n");
scanf("%d",&a);
if(a==1)
{
for(i=0;i<n;i++)
{
printf("enter the details of the football team number %d ",&i+1);
printf("enter the name with city");
scanf(" %s",t[i].name_city);
printf(" \n enter number of wins");
scanf("%d",t[i].wins);
printf(" \nenter number of losses");
scanf("%d",t[i].loss);
printf("\nenter the number of ties ");
scanf("%d",t[i].detail.fb.tie);
}
}
else
{
for(i=0;i<n;i++)
{
printf("enter the details of the baseball team number %d ",&i+1);
printf("enter the name with city");
scanf(" %s",t[i].name_city);
printf("\n enter number of wins");
scanf("%d",t[i].wins);
printf("\n enter number of losses");
scanf("%d",t[i].loss);
printf("\n enter the number of runs scored ");
scanf("%d",t[i].detail.bb.run);
}
}
int max;
struct team y[100];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t[i].wins>t[j].wins)
max=i;
else
max=j;
}
y[i]=t[max];
}
printf("\n ****Points table **** ") ;
printf("\nRANK TEAM WINS LOSSES " );
for(i=0;i<n;i++)
{
printf("\n%d %s &d &d",i+1,y[i].name_city,y[i].wins,y[i].loss);
}
}
19.Describe the output generated by each of the following programs. Explain any
differences between the programs.
Solution :
Following is the output we get when we run the programme .
Though every thing within the programme is same as any other general programme , there are two
concepts which we get to learn .
2.The C programming language provides a keyword called typedef, which you can use to give a typea
new name .
Example
After this definition, the identifier BYTE can be used as an abbreviation for the type unsigned char.