Professional Documents
Culture Documents
Week12 PDF
Week12 PDF
Question 1
1)Write a C program to read an array of int as arguments, create a dynamic array to
populate cyclically permute the element of an array(In right to left direction).
Client will read,display the array and then deallocate the array.
Server will have function for read,display and cyclic permutation for array elements.
Client.c
#include "server.h"
#include <stdio.h>
int main(){
int n;
int * arr;
printf("Enter n: ");
scanf("%d", &n);
allocate(arr, n);
print(arr, n);
permutate(arr, n);
print(arr, n);
}
Server.h
#include <stdio.h>
#include <stdlib.h>
Output
Question 2
2) Write a C program to allocate memory dynamically for the structure called
employee.The members of the structure are Emp-id,Name,Department.
Client will read and display the structure and then deallocate the structure.
Server will have function for read and display employee details.
Client.c
#include <stdio.h>
#include "server.h"
int main(){
int n;
printf("How many entries do you want: \n");
scanf("%d", &n);
struct emp * e = allocate(n);
read(e, n);
display(e, n);
free(e);
}
Server.h
#include <stdio.h>
#include <stdlib.h>
struct emp {
char name[50], dep[5];
int empID;
};
Output
Question 3
3) Write a C program to allocate memory dynamically for the structure called
student.The members of the structure are rollno(integer), name(string) and
marks(integer). Client will read,display and sort the records of the structure student
on the basis of marks in descending order and then deallocate the structure.Use
bubble sort for sorting the records.
Server will have functions for reading,displaying and sorting the marks.
Client.c
#include <stdio.h>
#include "server.h"
int main(){
int n;
printf("How many entries do you want: \n");
scanf("%d", &n);
struct student * s = allocate(n);
read(s, n);
sort(s, n);
display(s, n);
free(s);
}
Server.h
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[50], roll[5];
int marks;
};
Output
Practice Question 1
1) Write a C program to allocate memory dynamically for the structure called
Course.The members of the structure are Course Code and Course Name. At the beginning
of the semester student registers for all the 6 courses, later he drops few courses then,
reallocate memory for the given number of courses and register once again from the
student. Client will display the courses for the semester after dropping the courses.
Client.c
#include <stdio.h>
#include <stdlib.h>
struct course{
char name[20];
char code[10];
};
int main(){
struct course * cptr = (struct course *)malloc(sizeof(struct
course)*6);
int n;
printf("How many courses do you want to keep?: ");
scanf("%d", &n);
struct course * cptr_new = (struct course *)realloc(cptr, sizeof(struct
course)*n);
free(cptr);
read(cptr_new, n);
disp(cptr_new, n);
}
Output
Practice Question 2
2) Write a C program to read an array of int as arguments, create a dynamic array
populate with squares of elements in the array, client will read and display the array
and then deallocate the array.
Client.c
#include "server.h"
#include <stdio.h>
int main(){
int n;
int * arr;
printf("Enter n: ");
scanf("%d", &n);
allocate(arr, n);
print(arr, n);
square(arr, n);
print(arr, n);
}
Server.h
#include <stdio.h>
#include <stdlib.h>