Structures Macros & Pointers

85. WRITE A C PROGRAM TO EXPLAIN STRUCTURES BY ADDING TWO DISTANCES IN INCH-FEET SYSTEM

/*C program to explain structures by adding two distances in inch-feet system*/#include 
<stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
int main()
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);

sumOfDistances.feet = d1.feet+d2.feet;
sumOfDistances.inch = d1.inch+d2.inch;
// If inch is greater than 12, changing it to feet.
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
printf("\nSum of distances = %d\'-%.1f\"",sumOfDistances.feet, sumOfDistances.inch);
return 0;
}
output
Enter information for 1st distance
Enter feet: 23
Enter inch: 8.6

Enter information for 2nd distance
Enter feet: 34
Enter inch: 2.4

Sum of distances = 57'-11.0"


86. WRITE A C PROGRAM TO READ STUDENT STRUCTURE WITH STUDENT NAME ROLL NUMBER 2 SUBJECT MARKS AND FIND THE AVERAGE

/*C program to read student structure with student name roll number 2 subject marks and find the average*/
#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
printf("\nEnter the marks in 2 subjects : ") ;
scanf("%d %d", &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
}
printf("\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t\t %d \t %d \t %d \t %.2f \n",
s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);
}
getch() ;
}
output
Enter the number of students : 2
Enter the roll number : 101
Enter the name : Arun
Enter the marks in 2 subjects : 75 85
Enter the roll number : 102
Enter the name : Babu
Enter the marks in 2 subjects : 65 75
Roll No.  Name   Sub1  Sub2  Total   Average
101       Arun    75     85   160      80.00
102       Babu    65     75   140      70.00


87. WRITE A C PROGRAM TO EXPLAIN C – NESTED STRUCTURE

/*C program to explain C – Nested Structure*/
#include <stdio.h>
#include <string.h>

struct student_college_detail
{
int college_id;
char college_name[50];
};

struct student_detail 
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data;

int main() 
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"kakatiya University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);

printf(" College Id is: %d \n", 
stu_data.clg_data.college_id);
printf(" College Name is: %s \n", 
stu_data.clg_data.college_name);
return 0;
}
output
Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145
College Name is: kakatiya University


88. WRITE A C PROGRAM TO EXAMPLE PROGRAM FOR ARRAY OF STRUCTURES IN C:

/*c program to example program for array of structures in c::*/
#include <stdio.h>
#include <string.h>

struct student 
{
int id;
char name[30];
float percentage;
};

int main() 
{
int i;
struct student record[2];

// 1st student's record
record[0].id=1;
strcpy(record[0].name, "Raju");
record[0].percentage = 86.5;

// 2nd student's record 
record[1].id=2;
strcpy(record[1].name, "Surendren");
record[1].percentage = 90.5;

// 3rd student's record
record[2].id=3;
strcpy(record[2].name, "Thiyagu");
record[2].percentage = 81.5;

for(i=0; i<3; i++)
{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
output
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000


89. WRITE A C PROGRAM INVOLVING STRUCTURE TO FUNCTION

/*C program involving structure to function*/
#include<stdio.h>
#include<conio.h>
//-------------------------------------
struct Example
{
int num1;
int num2;
}s[3];
//-------------------------------------
void accept(struct Example *sptr)
{
printf("\nEnter num1 : ");
scanf("%d",&sptr->num1);
printf("\nEnter num2 : ");
scanf("%d",&sptr->num2);
}
//-------------------------------------
void print(struct Example *sptr)
{
printf("\nNum1 : %d",sptr->num1);
printf("\nNum2 : %d",sptr->num2);
}
//-------------------------------------
void main()
{
int i;
clrscr();
for(i=0;i<3;i++)
accept(&s[i]);
for(i=0;i<3;i++)
print(&s[i]);
getch();
}
output
Enter num1 : 10
Enter num2 : 20
Enter num1 : 30
Enter num2 : 40
Enter num1 : 50
Enter num2 : 60
Num1 : 10
Num2 : 20
Num1 : 30
Num2 : 40
Num1 : 50


90. WRITE A C PROGRAM TO ILLUSTRATE THE CONCEPT OF UNIONS

/ * C program to illustrate the concept of unions*/z
#include <stdio.h>
void main()
{
union number
{
int n1;
float n2;
};
union number x;

printf("Enter the value of n1: ");
scanf("%d", &x.n1);
printf("Value of n1 = %d", x.n1);
printf("\nEnter the value of n2: ");
scanf("%f", &x.n2);
printf("Value of n2 = %f\n", x.n2);
}
output
Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000


91. WRITE A C PROGRAM TO EXPLAIN USE OF MACROS

/*C program to explain use of macros*/
#include<stdio.h>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
int num1, num2, sum, product;
printf("\nEnter the Two numbers\n");
scanf("%d%d",&num1,&num2);
sum=SUM(num1,num2);
product=PROD(num1,num2);
printf("\n\nSum of two numbers using Macros is:%d\n",sum);
printf("\n\nProduct of two numbers using macros is:%d\n",product);
return 0;
}




92. MACROS WITH ARGUMENTS


93. WRITE A C PROGRAM TO ACCESS ARRAY ELEMENTS USING POINTERS

/*C program to Access Array Elements Using Pointers*/
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}
output

Enter elements: 1
2
3
5
4
You entered: 
1
2
3
4
5


94. WRITE A C PROGRAM TO PRINT BIGGEST VALUE IN THE ARRAY USING POINTERS IN C

#include <stdio.h>
int main()
{
long array[100], *maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");
scanf("%ld", &size);

printf("Enter %ld integers\n", size);

for ( c = 0 ; c < size ; c++ )
scanf("%ld", &array[c]);

maximum = array;
*maximum = *array;

for (c = 1; c < size; c++)
{
if (*(array+c) > *maximum)
{
*maximum = *(array+c);
location = c+1;
}
}

printf("Maximum element is present at location number %ld and it's value is %ld.\n", location, *maximum);
return 0;
}


95. WRITE A C PROGRAM TO IMPLEMENT LINEAR SEARCH USING FUNCTION AND POINTERS

/*C program to implement linear search using function and pointers*/
#include<stdio.h>
#include<stdlib.h>
int c=0;
int linearsearch(int *, int,int);
int linearsearch(int *a,int b,int n)
{
int i;
for(i=0;i<=n-1;i++)
{
if(*(a+i)==b)
{
c=1;
break;
}
}
return i;

}

int main()
{
int *a,i,n,m;

printf("Enter the size of an array: ");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));

printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++)
{
scanf("%d",(a+i));
}

printf("Enter the number to be search: ");
scanf("%d",&m);
i=0;
i=linearsearch(a,m,n);

if(c==0)
printf("The number is not in the list\n");
else
printf("The number is found at position %d\n",i+1);

return 0;
}


96.a) WRITE A C PROGRAM TO EXPLAIN C - ARRAY OF POINTERS

/*C program to explain C - Array of pointers*/

#include <stdio.h>
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr[MAX];

for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}

for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}

return 0;
}
output
Value of var[0] = 10
Value of var[1] = 100

Value of var[2] = 200


96.b) write a C program to explain C - Array of pointers

#include <stdio.h>
const int MAX = 4;
int main () {
char *names[] = {
"vipul",
"srikar",
"vinay",
"shashi"
};
int i = 0;
for ( i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
output

Value of names[0] = vipul
Value of names[1] = srikar
Value of names[2] = vinay
Value of names[3] = shashi

Comments