0% found this document useful (0 votes)
12 views18 pages

DOC-20240911-WA0002.

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)
12 views18 pages

DOC-20240911-WA0002.

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/ 18

OOPS ASSIGNMENT 1

NAME: Mrinmay Biswas


DEPT: COMPUTER SCIENCE
YEAR: UG2
SECTION: A1
ROLL: 002310501016

/*Assignment 1
Question 1: Write a program that will have an integer variable
and a pointer (say, p) pointing to it. Also have a pointer to
pointer
pointing to p. Take the value for the integer variable and print
it using p,
and pp.
NAME: Mrinmay Biswas
ROLL:002310501016
DATE: 22/08/2024 */

#include<stdio.h>
int main(){
int n;
printf("Enter your value of integer variable\n");
scanf("%d",&n);
int*p=&n;//pointer
int**pp=&p;//pointer to pointer
//Displaying
printf("THE VALUE OF n Using a using pointer is %d\n",*p);
printf("THE VALUE OF n Using a using double pointer is %d\n",**pp);
return 0;
}

OUTPUT:

Enter your value of integer variable


5
THE VALUE OF n Using a using pointer is 5
THE VALUE OF n Using a using double pointer is 5
[1] + Done

/*Assignment 1:
Question 2: Implement a one dimensional array of integers where
array size of the array is provided during runtime.
Accept the value for the elements and print those using pointers.
NAME: Mrinmay Biswas
ROLL: 002310501016
DATE: 22/08/2024 */

#include<stdio.h>
int main(){
int n;
printf("Enter how many element do you want?\n");
scanf("%d",&n);
int arr[n];//taken from the user
for(int i=0;i<n;i++){
printf("please enter your value\n");
scanf("%d",&arr[i]);
}
int *p=arr;//pointing to the first element of the arr
printing
for(int i=0;i<n;i++){
printf("your %d element id %d\n",i+1,*(p+i));
}
return 0;
}
OUTPUT:
Enter how many element do you want?
5
please enter your value
1
please enter your value
2
please enter your value
3
please enter your value
4
please enter your value
5
your 1 element id 1
your 2 element id 2
your 3 element id 3
your 4 element id 4
your 5 element id 5
[1] + Done
/*Assignment 1
Question 3 - Implement a two dimensional array of integers using
array
of pointers.
NAME: Mrinmay Biswas
ROLL: 002310501016
DATE: 22/08/2024 */
#include<stdio.h>
#include<stdlib.h>
int main(){
int row;
int col;
int opt;
printf("Enter row:");
scanf("%d",&row);
printf("Enter col:");
scanf("%d",&col);
printf("1 for array of ponter.\n");
pintf("2 for ponter to pointer with two malloc statement.\n");
printf("3 for ponter to pointer with one malloc statement.\n");
printf("4 for ponter to an array.\n");
printf("Enter your option:");
scanf("%d",&opt);
int *q[row];
int **b;
int (*p)[col];
int *arr;
//a.Array of pointer
printf(“enter your elsement”);
switch (opt)
{
case 1:

for(int i=0;i<row;i++){
q[i]=(int *)malloc(col*sizeof(int));
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
scanf("%d",(q[i]+j));
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d ",*(q[i]+j));
}
printf("\n");
}
break;
// pointer to a pointer with two malloc
case 2:

b=(int **)malloc(row *sizeof(int *));


for(int i=0;i<col;i++){
*(b+i)=(int *)malloc(col*sizeof(int));
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
scanf("%d",(*(b+i)+j));
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d ",*(*(b+i)+j));
}
printf("\n");
}
break;
// pointer to a pointer with one malloc
case 3:
b=(int **)malloc(row*sizeof(int *)+(row*col*sizeof(int)));
int *a=&b[row];
for(int i=0;i<row;i++,a+=col){
b[i]=a;
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
scanf("%d",&b[i][j]);
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
break;
pointer to a array
case 4
arr=(int *)malloc(row*col*sizeof(int));
p=&arr[row];

for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
scanf("%d",&p[i][j]);
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d",p[i][j]) ;
}
printf("\n");
}
break;
}
}

OUTPUT:
For option 1:

Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:1
Enter your element1 2
3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done

for option 2
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:2
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done
for option 3
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:3
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done
for option 4
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:4
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done
Question no 4
implement Question no 3 using function for
i)getting dimention the dimention from user
ii)accepting the values
iii)dynamic memory allocation
iv)printing the values
//Name Mrinmay Biswas
//Roll 0023205016
//write a programme implement Question no 2 and 3 using fuction

#include<stdio.h>
#include<stdlib.h>
void dimention(int **p){
int n;
printf("Enter how many element do you want?\n");
scanf("%d",&n);
memory(p,n);
return;
}
//for memory allocation
void memory(int **p,int n){
for(int i=0;i<n;i++)
*(p+i)=(int*)malloc(n*sizeof(int));
value(p,n);
return;
}
//for scaning
void value(int **p,int n){
for(int i=0;i<n;i++){
printf("please enter your value\n");
scanf("%d",*p+i);
}
printfunc((*p),n);
}
//printing
void printfunc(int *p,int n){
for(int i=0;i<n;i++){
printf("your %d element id %d\n",i+1,*(p+i));
}
return;
}
int main(){
int *p;
dimention(&p);
return 0;
}
Output
Enter how many element do you want?
3
please enter your value
1 2 3
please enter your value
please enter your value
your 1 element id 1
your 2 element id 2
your 3 element id 3

Next part
//implement Question no 3 using function
//b)pointer to pointer (with two malloc Statement AS WELL AS
ONE MALLOC STATEMENT)
//Name Mrinmay Biswas
//Roll 0023105016
#include<stdio.h>
#include<stdlib.h>
for dimention
void dimention(){
int n,c;
printf("Enter how many row do you want\n");
scanf("%d",&n);
printf("Enter how many colums do you want\n");
scanf("%d",&c);
printf("Press 1 if you want do the question With array
of pointers\n");
printf("Press 2 if you want do the question With pointer
to pointer with two malloc function\n");
printf("Press 3 if you want do the question With pointer
to pointer with one malloc function\n");
printf("Press 4 if you want do the question With pointer
to array\n");
printf("Which you want?");
int d;
scanf("%d",&d);
memory(n,c,d);
}
//for memory allocation and printing
void memory(int n,int c,int d){
if(d==1){
int *p[n];
for(int i=0;i<c;i++)
p[i]=(int*)malloc(c*sizeof(int*));
printf("ENTER your value");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
scanf("%d",*(p+i)+j);
}
}
print1(n,c,p);
}
if(d==2){
int **b;
b=(int**)malloc(n*sizeof(int*));
for(int i=0;i<c;i++)
*(b+i)=(int*)malloc(c*sizeof(int*));
printf("ENTER your value");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
scanf("%d",*(b+i)+j);
}
}
print2(n,c,b);
}
if(d==3){
int **s;
int *ptr;
s=(int**)malloc((n*sizeof(int*))+n*c*sizeof(int));
ptr=(int*)(s+n);
for(int i=0;i<n;i++)
s[i]=ptr+i*c;
printf("ENTER your value\n");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
scanf("%d",*(s+i)+j);
}
}
print2(n,c,s);
}
if(d==4){
int (*t)[c];
int *arr;
arr=(int*)malloc(n*c*sizeof(int));
t=&arr[n];
printf("ENTER your value\n"); int n,c;
printf("Enter how many row do you want\n");
scanf("%d",&n);
int *p[n];
printf("Enter how many colums do you want\n");
scanf("%d",&c);
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
scanf("%d",*(t+j)+i);
}
}
print3(n,c,t);
}
}
void print1(int n,int c,int*p[n]){
printf("YOUR ELEMENTS ARE \n");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
printf("%d\t",*(*(p+i)+j));
}
printf("\n");
}
}
void print2(int n,int c,int**b){
printf("YOUR ELEMENTS ARE \n");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
void print3(int n,int c,int (*t)[c]){
printf("YOUR ELEMENTS ARE \n");
for(int i=0;i<n;i++){
for(int j=0;j<c;j++){
printf("%d\t",*(*(t+j)+i));
}
printf("\n");
}
}
int main(){
dimention();
return 0;
}
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:1
Enter your element1 2
3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done

for option 2
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:2
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done

for option 3
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:3
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done
for option 4
Enter row:3
Enter col:3
1 for array of ponter.
2 for ponter to pointer with two malloc statement.
3 for ponter to pointer with one malloc statement.
4 for ponter to an array.
Enter your option:4
Enter your element1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
[1] + Done

/*ASSIGNMENT 1:
Question 5: Store name and age of number of persons (number
provided at run time). Collect the data and display
data in the ascending order of age. Implement without using
structure. Write functions for memory
allocation of the list, sorting and display of data.
NAME: Mrinmay Biswas
ROLL:002310501016
DATE: 22/08/2024
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Function to displaye

void display(int n,char***p,int**age){


for(int i=0;i<n;i++){
printf("%s\n",(*p)[i]);
}
// Function to sort the names based on age
void sort(int n,char***p,int**age){
for(int i=0;i<n;i++){//bubble sort
for(int j=1;j<n-i-1;j++){
if(*(*age+j)<*(*age+j-1)){
int temp=*(*age+j);
*(*age+j)=*(*age+j-1);
*(*age+j-1)=temp;
char t[10];//creation this to swap
strcpy(t,(*p)[j]);
strcpy((*p)[j],(*p)[j-1]);
strcpy((*p)[j-1],t);
}}
}

//Function for memory allocation


void memory(int n,char***p,int**age){
*p=(char**)malloc(n*sizeof(char*));
for(int i=0;i<n;i++)
*((*p)+i)=(char*)malloc(10*sizeof(char));//asuuming name size is
10
printf("Enter your name\n");
for(int i=0;i<n;i++){
scanf("%s",(*p)[i]);
}
*age=(int*)malloc(n*sizeof(int));
printf("Enter your age\n");
for(int i=0;i<n;i++){
scanf("%d",(*age+i));}
int main(){
int n;
printf("Enter how many name do you want?\n");
scanf("%d",&n);
char**p;
int *age;
memory(n,&p,&age);
sort(n,&p,&age);
display(n,&p,&age);
return 0;
}

OUTPUT:

Enter how many name do you want?


3
Enter your name
mrinmay
samim
sachine
Enter your age
34
56
78
End of Question a
mrinmay
samim
sachine
[1] + Done
/*ASSIGNMENT 1:
QUESTION 6:Store name and age of number of persons (number
provided at run time). Collect the data and display
data in the ascending order of age. Implement using structure.
Write functions for memory
allocation of the list, sorting and display of data.
NAME: Mrinmay Biswas
ROLL:002310501016
DATE: 29/08/2024
*/

#include<stdio.h>
//Structure
struct point
{
int age;
char name[10];
};
int main(){
int n;
printf("How many element do you want?\n");
scanf("%d",&n);
struct point list[n];
for(int i=0;i<n;i++){
printf("Enter your names:") ;
scanf("%s",list[i].name);}
for(int i=0;i<n;i++){
printf("Enter your ages of names:");
scanf("%d",&list[i].age);}
bubble sort
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(list[j].age>list[j+1].age){
struct point p=list[j];
list[j]=list[j+1];
list[j+1]=p;
}
}}
for(int i=0;i<n;i++)
printf("%s\n",list[i].name);
return 0;
}

/*Assignment_1
Question_7:Maintain a list to store roll number,name and score of
the student.
As and when required a student record may be added or deleted.
Also, the list has
to be displayed. Design suitable functions for different
operations.
NAME-Mrinmay biswas
ROLL NO.:002310501016
DATE-29/08/24
*/

#include<stdio.h>
#include<stdlib.h>
//Structure
struct student
{
int roll;
char name[20];
int score;
struct student *next;
};
struct student *head=NULL;
for add opreation
void add()
{
struct student *new,*temp;
new=(struct student*)malloc(sizeof(struct student));
printf("Enter roll no: ");
scanf("%d",&new->roll);
printf("Enter name: ");
scanf("%s",new->name);
printf("Enter score: ");
scanf("%d",&new->score);
new->next=NULL;
if(head==NULL)
{
head=new;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
for display opreation
void display(){
struct student *temp=head;
int i=0;
while(temp!=NULL)
{
printf("student[%d] : ",i+1);
printf("Roll no: %d\t",temp->roll);
printf("Name: %s\t",temp->name);
printf("Score: %d\n",temp->score);
temp=temp->next;
i++;
}
}
for delete opreration
void delete(int del_roll){
struct student *temp=head;
struct student *prev=NULL;
while(temp!=NULL && temp->roll!=del_roll){
prev=temp;
temp=temp->next;
}
if(temp==NULL){
printf("Student not found\n");
return;
}
if(prev==NULL) head = head->next;
else prev->next=temp->next;
}
int main(){
struct student *head=NULL;
int choice;
while(1)
{
printf("1. Add student\t2. Display student\t3. Delete student\n");
scanf("%d",&choice);
if(choice==1)
{
add();
}
else if(choice==2)
{
display();
}
else if(choice==3)
{
int i;
printf("Enter student roll number to delete: ");
scanf("%d",&i);
delete(i);
}

}
return 0;
}

1. Add student 2. Display student 3. Delete student


1
Enter roll no: 2
Enter name: mrinmay
Enter score: 45
1. Add student 2. Display student 3. Delete student
3
Enter student roll number to delete: 3
Student not found
1. Add student 2. Display student 3. Delete student
1
Enter roll no: 3
Enter name: sk tanisq
Enter score: 1. Add student 2. Display student 3. Delete
student
Enter roll no: Enter name: Enter score: 2
1. Add student 2. Display student 3. Delete student
2
student[1] : Roll no: 2 Name: mrinmay Score: 45
student[2] : Roll no: 3 Name: sk Score: 0
student[3] : Roll no: 0 Name: tanisq Score: 2
1. Add student 2. Display student 3. Delete student

/*ASSIGNMENT -1
QUESTION 8: Consider an array that stores roll, name, and score of
number of students. Develop a function to sort the
array. User of sort() will develop the comparison function for
sorting on roll/score and ascending or
descending order and reuse the same sort() function.
NAME: Mrinmay Biswas
ROLL: 002310501016
DATE: 29/08/2024 */

#include<stdio.h>
struct point
{
int roll;
int score;
char name[10];
};
//giving choice which they want
void sort(int choice,int n,struct point list[n]){
if(choice==1){
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(list[j].roll>list[j+1].roll){
struct point p=list[j];
list[j]=list[j+1];
list[j+1]=p;
}
}}}
if(choice==2){
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(list[j].score>list[j+1].score){
struct point p=list[j];
list[j]=list[j+1];
list[j+1]=p;
}
}}}
}
int main(){
int n;
printf("How many element do you want?\n");
scanf("%d",&n);
struct point list[n];
for(int i=0;i<n;i++){
printf("Enter your name:");
scanf("%s",list[i].name);}
for(int i=0;i<n;i++){
printf("Enter your roll of names:");
scanf("%d",&list[i].roll);}
for(int i=0;i<n;i++){
printf("Enter your score of names:");
scanf("%d",&list[i].score);}
int choice;
printf("Enter 1 if you want to perform in roll");
printf("Enter 2 if you want to perform in score");
scanf("%d",&choice);
sort(choice,n,list);
for(int i=0;i<n;i++)
printf("%s\n",list[i].name);
return 0;
}

Output
How many element do you want?
5
Enter your name:mrinmay
Enter your name:aniket
Enter your name:debraj
Enter your name:samim
Enter your name:manju
Enter your roll of names:1
Enter your roll of names:2
Enter your roll of names:3
Enter your roll of names:5
Enter your roll of names:78
Enter your score of names:0
Enter your score of names:4
Enter your score of names:56
Enter your score of names:100
Enter your score of names:67
Enter 1 if you want to perform in rollEnter 2 if you want to
perform in score1
mrinmay
aniket
debraj
samim
manju
[1] + Done

OOPS ASSIGNMENT 2

NAME: Mrinmay Biswas


DEPT: COMPUTER SCIENCE
YEAR: UG2
SECTION: A1
ROLL: 002310501016
/*Assignment 2:
Question 1: Write a program to store student information in a file
and to do the following operations. Information includes roll,
name, and score in five subjects. Use may like to add record
(ensure roll number is unique), display all records showing roll,
name and total score. User may search for the record against a
roll. User may edit the details a record. User may delete record.
Deletion may be logical (by some means indicate it is an invalid
record and to be avoided in processing) and physical (file will
not have the record).

NAME: Mrinmay Biswas


ROLL:002310501016
DATE: 12/09/2024 */

You might also like