Anda di halaman 1dari 8

MCSL-017 Solved assignment July 2017 –

January 2017 session


C Programming Lab

1. Write an interactive program in C language to create an application program for a small

฀Creating a New Record


office to maintain the employees database. This application should be having menu options like

฀Reading/Listing of Records
฀Modify the record
฀Delete the record
Each employee record should have Employee Name, Employee ID,Department Name, Salary,
Position, Date of Joining, etc). The application should be designed user-friendly.
A.1.
Structure is a user-defined data type in C which allows you to combine different data types to store a
particular type of record. Structure helps to construct a complex data type in more meaningful way. It is
somewhat similar to an Array. The only difference is that array is used to store collection of similar data
types while structure can store collection of any type of data.

Structure is used to represent a record. Suppose you want to store record of Student which consists of
student name, address, roll number and age. You can define a structure to hold this information.

Array of Structure:

We can declare an array of structure. Each element of the array representing a structure variable.
Example : struct employee emp[5];

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200

struct emp
{
int emp_id;
char *emp_name;
char *Dept_name;
float Salary;
char *Pos;
int Date;
}*emp1, *emp3;

void create();
void display();
void update();
void delete();

FILE *fp, *fp1;


int count = 0;

void main(int argc, char **argv)


{
int i, n, ch;

printf("1] Create a Record\n");


printf("2] Display Records\n");
printf("3] Update Records\n");
printf 4] Delete Records\n ;
printf("4] Exit");
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
fp = fopen(argv[1], "a");
create();
break;
case 2:
fp1 = fopen(argv[1],"rb");
display();
break;
case 3:
fp1 = fopen(argv[1], "r+");
update();
break;
case 4:
exit(0);
}
}
}

/* To create an employee record */


void create()
{
int i;
char *p;

emp1 = (struct emp *)malloc(sizeof(struct emp));


emp1->emp_name = (char *)malloc((size)*(sizeof(char)));
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->Dept_name = (char *)malloc((size)*(sizeof(char)));
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->Pos = (char *)malloc((size)*(sizeof(char)));

printf("Enter name of employee : ");


scanf(" %[^\n]s", emp1-> emp_name);
printf("Enter emp id : ");
scanf(" %d", &emp1-> emp_id);
printf("Enter name of the Department: ");
scanf(" %[^\n]s", emp1-> Dept_name);
printf("Enter Salary : ");
scanf(" %d", &emp1-> Salary);
printf("Enter Position: ");
scanf(" %[^\n]s", emp1-> Pos);
printf("Enter Date of Joining : ");
scanf(" %d", &emp1->Date);

fwrite(&emp1->emp_id, sizeof(emp1->emp_id), 1, fp);


fwrite(emp1->emp_name, size, 1, fp);
fwrite(&emp1->Dept_name, sizeof(emp1-> Dept_name), 1, fp);
fwrite(emp1-> Dept_name, size, 1, fp);
fwrite(&emp1-> salary, sizeof(emp1-> salary), 1, fp);
fwrite(emp1-> salary, size, 1, fp);
fwrite(&emp1->Pos, sizeof(emp1-> Pos), 1, fp);
fwrite(emp1-> Pos, size, 1, fp);
fwrite(&emp1->Date, sizeof(emp1-> Date), 1, fp);
fwrite(emp1-> Date, size, 1, fp);
count++; // count to number of entries of records
fclose(fp);
}

/* Display the records in the file */


void display()
{
emp3=(struct emp *)malloc(1*sizeof(struct emp));
emp3->emp_name=(char *)malloc(size*sizeof(char));
int i = 1;

if (fp1 == NULL)
printf("\nFile not opened for reading");
while (i <= count)
{
fread(&emp3->emp_id, sizeof(emp3->emp_id), 1, fp1);
fread(emp3->emp_name, size, 1, fp1);
printf("\n%d %s",emp3->emp_id,emp3->emp_name);
i++;
}
fclose(fp1);
free(emp3->emp_name);
free(emp3);
}

void update()
{
int id, flag = 0, i = 1;
char s[size];

if (fp1 == NULL)
{
printf("File cant be opened");
return;
}
printf("Enter employee id to update : ");
scanf("%d", &id);
emp3 = (struct emp *)malloc(1*sizeof(struct emp));
emp3->emp_name=(char *)malloc(size*sizeof(char));
while(i<=count)
{
fread(&emp3->id, sizeof(emp3->emp_id), 1, fp1);
fread(emp3->emp_name,size,1,fp1);
if (id == emp3->emp_id)
{
printf("Enter new name of emplyee to update : ");
scanf(" %[^\n]s", s);
fseek(fp1, -204L, SEEK_CUR);
fwrite(&emp3->emp_id, sizeof(emp3->id), 1, fp1);
fwrite(s, size, 1, fp1);
flag = 1;
break;
}
i++;
}
if (flag != 1)
{
printf("No employee record found");
flag = 0;
}
fclose(fp1);
free(emp3->name); /* to free allocated memory */
free(emp3);
}
$ a.out emprec1
1] Create a Record
2] Display Records
3] Update Records
4] Delete Record
5] Exit

Enter your choice : 1


Enter name of employee : aaa
Enter emp id : 100
Enter the name of the Department : Accounting
Enter Salary : 15987.60
Enter Position: Audit manager
Enter Date of Joining: 10/3/2015

Enter your choice : 1


Enter name of employee : Ram
Enter emp id : 101
Enter the name of the Department : Management
Enter Salary : 25987.60
Enter Position: Manager
Enter Date of Joining: 12/3/2015

Enter Your Choice : 1


Enter name of employee : Shyam
Enter emp id : 105
Enter the name of the Department : Management
Enter Salary : 10987.60
Enter Position: Junior Manager
Enter Date of Joining: 10/4/2015

Enter your choice : 4

C Programming Lab

1. a) Write a program in assembly language to swap two numbers.


A.1.(a)
DATA SEGMENT
A DB 50H
B DB 60H
DATA ENDS

CODE SEGMENT

ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX
MOV AL,A
MOV AH,B
MOV BL,AL
MOV AL,AH
MOV AH,BL
MOV A,AL
MOV B,AH

MOV AX,4C00H
INT 21H

CODE ENDS
END START

OUTPUT
***********

BEFOR AFTER
ax 0000 ax 06C1

bx 0000 bx CD36

b) Write a program in assembly language to check whether a number is even or odd.


A.1.(b)
DATA SEGMENT
X DW 27H
MSG1 DB 19,13,'NUMBER IS EVEN$'
MSG2 DB 10,13,'NUMBER IS ODD$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,X
TEST AX,01H
JNZ EXIT
LEA DX,MSG1
MOV AH,09H
INT 21H
JMP LAST

;Test for Even/Odd number.


;If it is Even go to Exit label.

c) Write a program in assembly language to load a byte in memory location 8000H and
increment the contents of the memory location.
A.1.(c)
Program 1:
MVI A, 32H : Store 32H in the accumulator

STA 4000H : Copy accumulator contents at address 4000H HLT : Terminate program execution

Program 2: LXI H : Load HL with 4000H MVI M : Store 32H in memory location pointed by HL register pair
(4000H)

HLT : Terminate program execution

d) Write a program in assembly language to reverse a given number.


A.1.(d)
INCLUDE io.h

Cr EQU 0ah
Lf EQU 0dh

data SEGMENT
p_num DB cr, lf, 'Enter a number: ',0
p_rev DB cr, lf,'The reverse is: ',0
ten DW ?
tmpstr DW 40 DUP (?)
data ENDS

code SEGMENT
ASSUME cs:code, ds:data
start: mov ax, data
mov ds, ax
;input the number
output p_num
inputs tmpstr, 10
atoi tmpstr
;initialize
mov cx, 00h;stores the reverse number
mov ten, 10
;iterate to reverse digits
next: cmp ax, 0
jz op_rev;exit the loop if number=0
cwd
idiv ten;to extract units digit
mov bx, dx;to save contents of dx
xchg ax, cx;to perform multiplication
imul ten;to insert units digit
add ax, bx;add the acutal units digit
xchg ax, cx;save the reverse number back
jmp next
;output the reverse
op_rev: output p_rev
itoa tmpstr, cx
output tmpstr

quit: mov al, 00h


mov ah, 4ch
int 21h
code ENDS

END start

Anda mungkin juga menyukai