[go: up one dir, main page]

0% found this document useful (0 votes)
15 views14 pages

C Lab Ex 9 and 10 With Alg

Uploaded by

cajilajayareeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

C Lab Ex 9 and 10 With Alg

Uploaded by

cajilajayareeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Ex No: 9 Implementation of Structures and Union

Aim:
To write C programs to implement Structure and Union.

Algorithm: 9(a) Student details using Nested Structure


Step 1: Start.
Step 2: Declare a structure “student” with name, rollno and another structure address as data
members.
Step 3: Declare houseno, street and pincode as the data members of nested structure.
Step 4: Inside the main() get the needed inputs for data members and print it in output screen.
Step 5: Stop.

Program:
#include<stdio.h>
struct student
{
char name[30];
int roll;
struct address
{
int houseno;
char street[20];
int pincode;
};
struct address addr;
};
void main()
{
struct student stud;
printf("Enter name and roll number of student:\n");
scanf("%s %d", stud.name, &stud.roll);
printf("Enter street name, house number and Pin Code:\n");
scanf("%s %d %d", stud.addr.street, &stud.addr.houseno, &stud.addr.pincode);
printf("Student detail is:\n");
printf("Name: %s\nRoll: %d\n", stud.name, stud.roll);
printf("Address:\n");
printf("%s,\n%d,\n%d.", stud.addr.street, stud.addr.houseno, stud.addr.pincode);
}

Output:
Enter name and roll number of student:
Anju 23
Enter street name, house number and Pin Code:
NewStreet 234 629003
Student detail is:
Name: Anju
Roll: 23
Address:
NewStreet,
234,
629003.
Algorithm: 9(b) Employee details using Array of Structure
Step 1: Start.
Step 2: Declare a structure “employee” with data members name, designation and salary.
Step 3: Create structure variable s as array to store the details of 5 employees.
Step 4: Get the input using structure variable and initialize the data members of structure and
display it in output screen.
Step 5: Stop.

Program:
#include<stdio.h>
struct employee
{
char name[20];
char designation[20];
float salary;
}s[5];

void main()
{
int i;
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Enter name :");
scanf("%s", s[i].name);
printf("Enter designation :");
scanf("%s", s[i].designation);
printf("Enter salary :");
scanf("%f", &s[i].salary);
}
printf("\nThe details of employees are :\n");
printf("NAME\tDESIGNATION\tSALARY\n\n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t%.0f\n", s[i].name, s[i].designation, s[i].salary);
}
}

Output:
Enter details of 5 employees:
Enter name :A
Enter designation :Manager
Enter salary :50000
Enter name :B
Enter designation :AssManager
Enter salary :40000
Enter name :C
Enter designation :Clerk
Enter salary :25000
Enter name :D
Enter designation :Officer
Enter salary :35000
Enter name :E
Enter designation :OfficeManager
Enter salary :38000
The details of employees are :
NAME DESIGNATION SALARY

A Manager 50000
B AssManager 40000
C Clerk 25000
D Officer 35000
E OfficeManager 38000

Algorithm: 9(c) Student Details using Structure and Pointers

Step 1: Start.
Step 2: Declare a structure “student” with data members name, roll and perc as data members.
Step 3: Declare a structure variable and a pointer structure variable.
Step 4: Assign the address of pointer structure variable to structure variable.
Step 5: Get input using structure pointer variable and display it in the output screen using the
same.
Step 6: Stop.

Program:
#include <stdio.h>
struct student
{
char name[30];
int roll;
float perc;
};
void main()
{
struct student stu;
struct student *ptr;
ptr= &stu;
printf("Enter details of student: ");
printf("\nName :");
gets(ptr->name);
printf("Roll No :");
scanf("%d",&ptr->roll);
printf("Percentage :");
scanf("%f",&ptr->perc);
printf("\nEntered details: ");
printf("\nName:%s \nRollNo: %d \nPercentage: %.2f\n",ptr->name,ptr->roll,ptr->perc);
}

Output:

Enter details of student:


Name : Anju
Roll No : 12
Percentage : 87.45

Entered details:
Name: Anju
RollNo: 12
Percentage: 87.45
Algorithm: 9(d) Implementation of Union
Step 1: Start.
Step 2: Declare a union “pack” with three data members a, b, c.
Step 3: Initialize the values of union data members separately and display it.
Step 4: Initialize the values of union data members at the same time and display it.
Step 5: Stop.

Program:
#include <stdio.h>
union pack
{
char a;
int b;
float c;
};
void main()
{

union pack p;
printf("\nOccupied size by union pack: %d", sizeof(p));
p.a='A';
printf("\nValue of a:%c", p.a);
p.b=10;
printf("\nValue of b:%d", p.b);
p.c=12345.6790;
printf("\nValue of c:%f", p.c);
printf("Initializing values together:\n");
p.a='A';
p.b=10;
p.c=12345.6790;
printf("\nValue of a:%c\n b:%d\n c:%f", p.a, p.b, p.c);
}

Output:
Occupied size by union pack: 4
Value of a: A
Value of b: 10
Value of c: 12345.678711
Initializing values together:
Value of a: ╖
b: 1178658487
c: 12345.678711

Result:
Thus the above programs to implement Structure and Union were done and executed
successfully.
Ex No: 10 Implementation of File

Aim:
To write C programs to implement File Access and Processor Directives.

Algorithm: 10(a) Store Student details in File


Step 1: Start.
Step 2: Declare a File pointer variable along with the needed variables.
Step 3: Open a file named as “f1.txt” in write mode and write the details of students in it using
fprintf() and close the file.
Step 4: Again open the file “f1.txt” in read mode and read the data from file using fscanf() and
display it in the output screen.
Step 5: Stop.

Program:
#include<stdio.h>
void main()
{
FILE *f1;
char s[10][10];
int i,n,r[10];
printf("Enter no of students\n");
scanf("%d", &n);
printf("Enter content for file1 : \n");
f1=fopen("f1.txt","w");
for(i=1;i<=n;i++)
{
printf("Enter name and rollno\n");
scanf("%s%d", s[i], &r[i]);
fprintf(f1,"%s\n", s[i]);
fprintf(f1,"%d", r[i]);
}
fclose(f1);
f1=fopen("f1.txt","r");
printf("\nName\tRollnumber\n-----\t----------\n");
for(i=1;i<=n;i++)
{
fscanf(f1,"%s", s[i]);
fscanf(f1,"%d", r[i]);
printf("%s\t%d\n", s[i], r[i]);
}
fclose(f1);
}

Output:
Enter no of students
2
Enter content for file1:
Enter name and rollno
Anju 12
Enter name and rollno
Arya 23
Name Rollnumber
------ --------------
Anju 12
Arya 23
Algorithm: 10(b) Random Access on File
Step 1: Start.
Step 2: Declare a File pointer variable along with the needed variables.
Step 3: Open a file named as “test.txt” in write mode and write the details in it using fprintf().
Step 4: Use ftell() to get the current position of the file pointer.
Step 5: Use fseek() to move the file pointer to a specific position.
Step 6: Use rewind() to move the file pointer to the beginning of the file.
Step 7: Stop.

Program:
#include <stdio.h>
void main ()
{
char name [20];
int age;
FILE *fp;
fp = fopen ("test.txt", "w");
fprintf (fp, "%s %d", "CProgram", 25);
printf("Current cursor position= %d\n",ftell(fp));
fseek(fp, 5,0);
printf("After seeking cursor position= %d\n",ftell(fp));
rewind (fp);
printf("After Rewind cursor position= %d\n",ftell(fp));
fclose(fp);
fp = fopen ("test.txt", "r");
fscanf (fp, "%s %d",name, &age);
printf ("Name: %s\nAge: %d\n", name, age);
fclose(fp);
}

Output:
Current cursor position= 11
After seeking cursor position= 5
After Rewind cursor position= 0
Name: CProgram
Age: 25

Algorithm: 10(c) Area of Circle using Preprocessor #define


Step 1: Start.
Step 2: Define a macro AREA to find area of circle using #define.
Step 3: Get radius as input.
Step 4: Call the macro AREA with radius as input to calculate area of Circle.
Step 5: Display the result.
Step 6: Stop.

Program:
#include<stdio.h>
#include<conio.h>
#define AREA(a) (3.14 * a * a)
void main()
{
float r, x;
clrscr();
printf("Enter radius value: ");
scanf("%f",&r);
x = AREA (r);
printf ("Area of circle = %f", x);
getch();
}

Output:
Enter radius value: 7
Area of circle = 153.86

Algorithm: 10(d) Biggest among two numbers using #if, #else, #endif
Step 1: Start.
Step 2: Define two macros X as 10 and Y as 5 using #define.
Step 3: Using #if check X is greater than Y, If True display X is Big.
Step 4: Using #else display Y is Big if X is lesser than Y.
Step 5: Using #endif end the #if statement.
Step 6: Stop.

Program:
#include <stdio.h>
#include<conio.h>
#define X 10
#define Y 5
void main()
{
clrscr();
#if X>Y
printf("%d is Big", X);
#else
printf("%d is Big", Y);
#endif
getch();
}

Output:
10 is Big

Result:
Thus the above programs to implement File Access and Processor Directives were done
and executed successfully.

You might also like