Skip to content

Commit

Permalink
modified
Browse files Browse the repository at this point in the history
  • Loading branch information
amantiwari8861 committed Sep 26, 2023
1 parent 00e5d46 commit e4506c8
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 35 deletions.
39 changes: 39 additions & 0 deletions 06-Arrays_&_multidimension_Arrays/ExercSoln/findCommon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include<stdio.h>
int main()
{
// int arr1[]={2,3,5,4,4,7,1,6};
// int arr2[]={7,8,9,3,3,3,9,5};
int arr1[8],arr2[10];
printf("enter 1st array \n");
for (int i = 0; i < 8; i++)
{
scanf("%d",&arr1[i]);
}
printf("enter 2nd array \n");
for (int i = 0; i < 10; i++)
{
scanf("%d",&arr2[i]);
}
int min=8<10?8:10;
int commonElement[min],countCommon=0;

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr1[i]==arr2[j])
{
commonElement[countCommon++]=arr1[i];
break;
}
}
}
printf("common elements are :");
for (int i = 0; i < countCommon; i++)
{
printf("%d ",commonElement[i]);
}


return 0;
}
34 changes: 34 additions & 0 deletions 06-Arrays_&_multidimension_Arrays/ExercSoln/secondLargest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
int main()
{
int n;
printf("enter no. of elements:");
scanf("%d",&n);//7
int arr[n];
printf("enter %d elements \n",n);
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
//bubble sort
for (int pass = 0; pass < n-1; pass++)
{
for (int j = 0; j < n-pass-1; j++)
{
if (arr[j]>arr[j+1])
{
int c=arr[j];
arr[j]=arr[j+1];
arr[j+1]=c;
}
}
}
printf("the sorted array is \n");
for (int i = 0; i < n; i++)
{
printf(" %d ",arr[i]);
}
printf("\nthe second largest element is %d \n",arr[n-2]);

return 0;
}
32 changes: 32 additions & 0 deletions 06-Arrays_&_multidimension_Arrays/Program/unique.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
int main()
{
int arr[10],times,unique[10],uniqueCount=0;
printf("enter the elements in array:\n");
for (int i = 0; i < 10; i++)
{
scanf("%d",&arr[i]);
}

//find unique elements
for (int i = 0; i < 10; i++)
{
times=0;
for (int j = 0; j < 10; j++)
{
if (arr[i]==arr[j])
times++;
}
if(times==1)
{
unique[uniqueCount++]=arr[i];
// printf("%d is unique\n",arr[i]);
}
}

printf("the unique elements are :");
for (int i = 0; i < uniqueCount; i++)
printf("%d ",unique[i]);

return 0;
}
14 changes: 7 additions & 7 deletions 06-Arrays_&_multidimension_Arrays/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ int main()
// printf("the value of marks is %d \n",marks4);
// printf("the value of marks is %d \n",marks5);

int marks[5]; //1st way to declare array
// int marks[5]; //1st way to declare array
// int marks[5]={101,201,301,40,16}; //2nd way to declare and initialize an array
// int marks[]={21,31,41,51,61}; // 3rd way to declare and initialize an array
int marks[]={21,31,41,51,61}; // 3rd way to declare and initialize an array

marks[0]=10;
marks[1]=20;
marks[2]=30;
marks[3]=40;
marks[4]=50;
// marks[0]=10;
// marks[1]=20;
// marks[2]=30;
// marks[3]=40;
// marks[4]=50;

printf("the value of marks[0] is %d \n",marks[0]);
printf("the value of marks[1] is %d \n",marks[1]);
Expand Down
2 changes: 1 addition & 1 deletion 06-Arrays_&_multidimension_Arrays/marksarr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int main()
scanf("%d",&subjects);//let subjects = 5

float marks[subjects],total=0,percent;
//let marks[5]={78,89,98,67,54}
//let marks[5]={78,89,98,67,54}
printf(" enter %d subjects marks \n",subjects);

for (int i = 0; i < subjects; i++)
Expand Down
70 changes: 43 additions & 27 deletions 14-File_handling/BookManagement.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,48 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void insert(FILE *fp);
void del(FILE *fp);
void modify(FILE *fp);
void booksold(FILE *fp);
int search(FILE *fp,char *name);
void display(FILE *fp);
void list(FILE *fp);
struct {
char name[50];
int ncopies;
float cost;
}book;
int main(void)

struct
{
char name[50];
int ncopies;
float cost;
}book;

int main()
{
int choice;
FILE *fp;
fp = fopen("books","rb+");
fp = fopen("books.dat","rb+");
if(fp==NULL)
{
fp=fopen("books","wb+");
fp=fopen("books.dat","wb+");
if(fp==NULL)
{
puts("Error in opening file\n");
printf("Error in opening or creating file\n");
exit(1);
}
}
while(1)
{
printf("1.Insert a new record\n");

printf("\n\n1.Insert a new record\n");
printf("2.Delete a record\n");
printf("3.Display record of a book\n");
printf("4.Modify an existing record\n");
printf("5.List all records\n");
printf("6 Book sold\n");
printf("7.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
scanf("%d%*c",&choice);

switch(choice)
{
Expand All @@ -62,7 +67,7 @@ int main(void)
break;
case 7:
fclose(fp);
exit(1);
exit(0);
default :
printf("Wrong choice\n");
}/*End of switch */
Expand All @@ -72,14 +77,22 @@ int main(void)

void insert(FILE *fp)
{
fseek(fp,0,2);
fseek(fp,0,SEEK_END);
printf("Enter book name : ");
scanf("%[^\n]s",book.name);
printf("Enter number of copies : ");
scanf("%d",&book.ncopies);
printf("Enter cost of book : ");
scanf("%f",&book.cost);
fwrite(&book,sizeof(book),1,fp);
int status=fwrite(&book,sizeof(book),1,fp);
if (status>0)
{
printf("book added succesfully!!!\n");
}
else
{
printf("unable to add book!!!\n");
}
}/*End of insert()*/

void del(FILE *fp)
Expand All @@ -90,7 +103,7 @@ void del(FILE *fp)
scanf("%[^\n]s",name);
if(search(fp,name)==0)
return;
fptmp = fopen("tempfile","wb");
fptmp = fopen("tempfile.dat","wb");
rewind(fp);
while(fread(&book, sizeof(book),1,fp) == 1)
{
Expand All @@ -99,17 +112,19 @@ void del(FILE *fp)
}
fclose(fp);
fclose(fptmp);
remove("books");
rename("tempfile","books");
remove("books.dat");
rename("tempfile.dat","books.dat");
printf("\nRecord deleted........\n\n");
fp = fopen("books", "rb+");
fp = fopen("books.dat", "rb+");
}/*End of del()*/

void modify(FILE *fp)
{
char name[50];
long size = sizeof(book);
printf("Enter the name of the book to be modified : ");
scanf("%[^\n]s",name);
fflush(stdin);
if(search(fp,name) == 1)
{
printf("Enter new data-->\n\n");
Expand All @@ -128,16 +143,15 @@ void modify(FILE *fp)
void booksold(FILE *fp)
{
char name[50];
long size = sizeof(book);
printf("Enter the name of the book to be sold : ");
scanf("%[^\n]s", name);
if(search(fp,name)==1)
{
if(book.ncopies >0)
{
book.ncopies--;
fseek(fp, -size, 1);
fwrite(&book, sizeof(book), 1, fp);
fseek(fp,(long)-sizeof(book), 1);
fwrite(&book,sizeof(book), 1, fp);
printf("One book sold\n");
printf("Now number of copies = %d\n", book.ncopies);
}
Expand All @@ -161,7 +175,7 @@ void display(FILE *fp)

int search(FILE *fp,char *name)
{
unsigned flag=0;
int flag=0;
rewind(fp);
while(fread(&book, sizeof(book),1,fp)==1)
{
Expand All @@ -179,12 +193,14 @@ int search(FILE *fp,char *name)
void list(FILE *fp)
{
rewind(fp);
printf("\nNAME\tCOPIES\t\tCOST\n\n");
printf("\n%-20s %-20s %-20s \n"," NAME"," COPIES"," COST");
printf("%-20s %-20s %-20s \n"," ----"," ------"," ------");

while(fread(&book, sizeof(book),1,fp)==1)
{
printf("%s\t",book.name);
printf("%d\t\t",book.ncopies);
printf("%f\n",book.cost);
printf("%-20s",book.name);
printf("%-20d",book.ncopies);
printf("%-20f",book.cost);
printf("\n");
}
printf("\n");
}/*End of list()*/
Binary file added 14-File_handling/books.dat
Binary file not shown.
26 changes: 26 additions & 0 deletions extra/ArrayCopy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<stdio.h>
int main()
{
int n;
printf("enter the size of array:");
scanf("%d",&n);

int boxes[n];// 10,45,67
printf("enter the no. of books\n");
for (int i = 0; i < n; i++)
{
scanf("%d",&boxes[i]);
}
int box2[n];
for (int i = 0; i < n; i++)
{
// box2[i]=boxes[i];
box2[n-i-1]=boxes[i];
}
printf("the content of box2 is \n");
for (int i = 0; i < n; i++)
{
printf(" %d ",box2[i]);
}
return 0;
}
33 changes: 33 additions & 0 deletions extra/linearsearch2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<stdio.h>
int main()
{
// int arr[]={23,775,960,144,155,844,783,989,492,829,861,735,574,18,491,900,37,326,77,192,171,550,846,459,877,915,658,601,132,174,429,440,629,253,880,783,48,29,885,941,614,140,429,576,325,175,988,522,51};
// int len=sizeof(arr)/sizeof(4);
// printf("the length of array is %d \n",len);

int arr[10];
printf("enter 10 values\n");
for (int i = 0; i < 10; i++)
{
scanf("%d",&arr[i]);
}

int key,isfound=0;
printf("enter value to be searched:");
scanf("%d",&key);

for (int i = 0; i < 10; i++)
{
if (arr[i]==key)
{
printf("found at position %d \n",i);
isfound=1;
break;
}
}
if (isfound!=1)
{
printf("not found in array \n");
}
return 0;
}

0 comments on commit e4506c8

Please sign in to comment.