Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

WEEK 12 

MakeFile for all programs:


a.out:client.o
gcc client.c
client.o: client.c server.h
gcc -c client.c
run:
./a.out

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>

void allocate(int * arr, int n){


for(int i =0; i < n; i++){
int * temp = (int *)malloc(sizeof(int));
temp = arr+i;
printf("Enter data: ");
scanf("%d", temp);
}
}
void permutate(int * arr, int n){
int temp;
temp = *arr;
int i =0;
for(i=0; i < n-1; i++){
*(arr+i) = *(arr+i+1);
}
*(arr+n-1)= temp;
}

void print(int * arr, int n){


int * temp;
temp = arr;
for(int i =0; i < n; i++){
printf("%d---->", *temp);
temp++;
}
printf("NULL\n");
}

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;
};

struct emp * allocate(int n){


struct emp * e = (struct emp *)malloc(sizeof(struct emp)*n);
return e;
}

void display(struct emp * e, int n){


for(int i =0; i < n; i++){
printf("\nName: %s", (e+i)->name);
printf("\nDepartment: %s", (e+i)->dep);
printf("\nEmpId: %d\n", (e+i)->empID);
}
}

void read(struct emp * e, int n){


for(int i =0; i < n; i++){
printf("Enter name: "); scanf("%s", (e+i)->name);
printf("Enter department: "); scanf("%s", (e+i)->dep);
printf("Enter ID: "); scanf("%d", &((e+i)->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;
};

struct student * allocate(int n){


struct student * s = (struct student *)malloc(sizeof(struct
student)*n);
return s;
}

void display(struct student * s, int n){


for(int i =0; i < n; i++){
printf("\nName: %s", (s+i)->name);
printf("\nRoll: %s", (s+i)->roll);
printf("\nMarks: %d\n", (s+i)->marks);
}
}

void read(struct student * s, int n){


for(int i =0; i < n; i++){
printf("Enter name: "); scanf("%s", (s+i)->name);
printf("Enter roll: "); scanf("%s", (s+i)->roll);
printf("Enter marks: "); scanf("%d", &((s+i)->marks));
}
}

void sort(struct student * s, int n){


for(int i =0; i < n; i++){
int flag = 1;
for(int j = 0; j < n-i-1; j++){
if((s+j)->marks < (s+j+1)->marks){
struct student temp = *(s+j);
*(s+j) = *(s+j+1);
*(s+j+1) = temp;
flag = 0;
}
}
if(flag) return;
}

 
 

 
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];
};

void disp(struct course * s, int n){


for(int i =0; i < n; i++){
printf("\n\nName: %s", (s+i)->name);
printf("\nCode: %s\n", (s+i)->code);
}
}

void read(struct course * s, int n){


for(int i =0; i < n; i++){
printf("Enter name: "); scanf("%s", (s+i)->name);
printf("Enter roll: "); scanf("%s", (s+i)->code);
}
}

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>

void allocate(int * arr, int n){

for(int i =0; i < n; i++){


int * temp = (int *)malloc(sizeof(int));
temp = arr+i;
printf("Enter data: ");
scanf("%d", temp);
}
}
void square(int * arr, int n){
int i =0;
for(i=0; i < n; i++){
*(arr+i) = *(arr+i) * *(arr + i);
}
}

void print(int * arr, int n){


int * temp;
temp = arr;
for(int i =0; i < n; i++){
printf("%d---->", *temp);
temp++;
}
printf("NULL\n");

Output 

 
 

You might also like