#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
// swapping
void swap(int *a,int *b);
void swap2(int a,int b);
// 1 d array
void array();
// functions of 1 d array
int max(int arr[],int x);
int min(int arr[],int x);
void display(int arr[],int x);
void rev(int arr[],int x);
void insert1(int arr[],int n);
void insert2(int a[],int m);
void dupli(int a[],int n); // out put galat aara hai
// sorting
void sort(int arr1[],int n);
void sort1(int arr[],int n);// selection sort
void sort2(int arr[],int n);//bubble sort
void sort3(int arr[],int n);// insertion sort
// 2 d array
void array2();
// functions of 2 d array
int max2(int arr[10][10],int r,int c);
int min2(int arr[10][10],int r,int c);
void display2(int arr[10][10],int r,int c);
// structure
void structure();
void dispstr(struct st *a,int per[]);// array dikkat kr ra hai
// strings
void string();
int lenstr(char str[]);// find length
void dispstr(char str[]);// display string
void revstr(char str[]);// reverse
void constr();// concatenation
void upperstr(char str[]);//change lower to upper
int copystr(char *p,char *q);//copy to anthor string
//structure
struct st
{
int roll;
char name[100];
int age;
float marks[5];
};
//main function
int main()
{
int ch;char ch1='y';int num1,num2;
//clrscr();
label:
while(ch1=='y')
{
printf("\t\t\t\tMENU\n");
printf("1 for swapping by ref\n");
printf("2 for swapping by call by value\n");
printf("3 for 1 D array operations\n");
printf("4 for structure operation\n");
printf("5 for string opererations \n");
printf("6 for exit");
printf("7 for 2 D array\n");
printf("\nenter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter two values\n");
scanf("%d%d",&num1,&num2);
printf("before swapping a=%d and b=%d\n",num1,num2);
swap(&num1,&num2);
printf("after swapping a=%d and b=%d\n",num1,num2);
break;
case 2: printf("enter two values\n");
scanf("%d%d",&num1,&num2);
printf("before swapping a=%d and b=%d\n",num1,num2);
swap2(num1,num2);
break;
case 3: array();
break;
case 4: structure();
break;
case 5: string();
break;
case 6: exit(0);
break;
case 7: array2();
break;
default:printf("\nwrong choice");
break;
}
printf("do you want to enter more in main section\n");
scanf(" %c",&ch1);
}
getch();
return 0;
}
// for arrays only
void array()
{
int arr1[100],n;
char ch1='y';
//clrscr();
printf("enter the no of element\n");
scanf("%d",&n);
printf("enter the values for array element\n");
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
while(ch1=='y')
{
printf("menu\n");
printf("1 for max\n");
printf("2 for display\n");
printf("3 for min\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for exit\n");
printf("7 for inserting a element in a specified location\n");
printf("8 for inserting a array in a specified location\n");
printf("9 for soting the array\n");
printf("10 for checking duplicacy\n");
int ch,a;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: a=max(arr1,n);
printf("max value in array is %d",a);
break;
case 2: display(arr1,n);
break;
case 3: a=min(arr1,n);
printf("min value in array is %d",a);
break;
case 4: rev(arr1,n);
break;
case 5: main();
break;
case 6: exit(0);
break;
case 7: insert1(arr1,n);
break;
case 8: insert2(arr1,n);
break;
case 9: sort(arr1,n);
break;
case 10:dupli(arr1,n);
break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in array section please enter y otherwise n\n");
scanf(" %c",&ch1);
}
//if(ch1!='y')
//exit(0);
}
// for array display
void display(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d \t",arr[i]);
}
//getch();
}
// for max item in array
int max(int arr[],int n)
{
int i;
int temp;
temp=arr[0];
for(i=0;i<n;i++)
{
if(temp<arr[i])
temp=arr[i];
}
return temp;
}
// for min item in array
int min(int arr[],int n)
{
int i;
int temp;
temp=arr[0];
for(i=0;i<n;i++)
{
if(temp>arr[i])
temp=arr[i];
}
return temp;
}
// for reversing array
void rev(int arr[],int x)
{ // int no=x;
int i,j;
i=0;
j=x-1;
while(j>i)
{
swap(&arr[i],&arr[j]);
i++;
j--;
}
display(arr,x);
}
// insertion of 1 element
void insert1(int arr[],int n)
{
printf("enter location and value\n");
int loc,val,i;
scanf("%d%d",&loc,&val);
for(int i=n-1;i>=loc-1;i--)
{
arr[i+1]=arr[i];
}
arr[loc-1]=val;
printf("after insertion\n");
for(i=0;i<=n;i++)
{
printf("%d\n",arr[i]);
}
}
// for inserting a array in anthor array
void insert2(int a[],int m)
{
printf("enter location \n");
int loc;
scanf("%d",&loc);
int b[100];int n;
printf("enter the number of element in 2nd array\n");
scanf("%d",&n);
printf("enter 2 array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("you have entered array 2\n");
for(int i=0;i<n;i++)
{
printf("%d\n",b[i]);
}
for(int i=m-1;i>=loc-1;i--)
{
a[i+n]=a[i];
}
for(int j=0;j<n;j++)
{
a[loc-1]=b[j];
loc++;
}
printf("after insertion\n");
for(int i=0;i<=m;i++)
{
printf("%d\n",a[i]);
}
}
// sorting of 1 d array
void sort(int arr1[],int n)
{
char ch1='y';
while(ch1=='y')
{
printf("menu\n");
printf("1 for selection sort\n");
printf("2 for display\n");
printf("3 for bubble sort\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for for insertion sort\n");
printf("7 for exit\n");
int ch;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: sort1(arr1,n);
break;
case 2: display(arr1,n);
break;
case 3: sort2(arr1,n);
break;
case 4: rev(arr1,n);
break;
case 5: main();
break;
case 6: sort3(arr1,n);
break;
case 7: exit(0);
break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in sorting section please enter y otherwise n\n");
scanf(" %c",&ch1);
}
}
// selection sort
void sort1(int arr[],int n)
{
int temp;
for(int i=n-1;i>0;i--) // for travesing in aaray
{
int max_index=0;
for(int j=0;j<i;j++)// for finding max index
{
if(arr[max_index]<arr[j])
{
max_index=j;
}
}
if(arr[max_index]>arr[i])
{
temp=arr[max_index];
arr[max_index]=arr[i];
arr[i]=temp;
}
}
display(arr,n);
}
// sorting by bubble sort
void sort2(int arr[],int n)
{
int temp;
for(int i=n-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
display(arr,n);
}
// sorting by insertion
void sort3(int arr[],int n)
{
int temp;
for(int i=1;i<n;i++)
{
int key=arr[i];
int j=i-1;
while(j>=0&&arr[j]>key)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
display(arr,n);
}
// for finding duplicate element in array
void dupli(int a[],int n)
{
int count=0,num;
for(int i=n-1;i>0;i--)
{
for(int j=i;j>0;j--)
{
if(a[i]==a[j])
count++;
}
if(count>1)
num=a[i];
}
if(count==1)
printf("no dublicate element\n");
else
printf("%d has %d number of dublicate element\n",num,count);
}
// for 2 d array only
void array2()
{
int arr1[10][10],r,c;
char ch1='y';
//clrscr();
printf("enter the no of rows and colomns\n");
scanf("%d%d",&r,&c);
printf("enter the values for matrix\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&arr1[i][j]);
}
}
while(ch1=='y')
{
printf("menu\n");
printf("1 for max\n");
printf("2 for display\n");
printf("3 for min\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for exit\n");
printf(" 7 for sum of all elements\n");
//printf("7 for inserting a element in a specified location\n");
//printf("8 for inserting a array in a specified location\n");
int ch,a;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: a=max2(arr1,r,c);
printf("max value in array is %d",a);
break;
case 2: display2(arr1,r,c);
break;
case 3: a=min2(arr1,r,c);
printf("min value in array is %d",a);
break;
case 4:// rev(arr1,r,c);
break;
case 5: main();
break;
case 6: exit(0);
break;
// case 7: insert1(arr1,n);
// break;
// case 8: insert2(arr1,n);
// break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in array section please enter y otherwise n\n");
scanf(" %c",&ch1);
}
//if(ch1!='y')
//exit(0);
}
// display 2 d array
void display2(int arr[10][10],int r,int c)
{
printf("you have entered\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
}
// max in 2 d array
int max2(int arr[10][10],int r,int c)
{
int large=arr[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(large<arr[i][j])
large=arr[i][j];
// if(small>arr[i][j])
// small=arr[i][j];
}
}
// printf("large%d\n",large);
// printf("small%d\n",small);
return large;
}
// min in 2 d array
int min2(int arr[10][10],int r,int c)
{
// int large=arr[0][0];
int small=arr[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
// if(large<arr[i][j])
// large=arr[i][j];
if(small>arr[i][j])
small=arr[i][j];
}
}
// printf("large%d\n",large);
// printf("small%d\n",small);
return small;
}
// swapping by reference
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
// swapping for call by value
void swap2(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping a=%d and b=%d\n",a,b);
}
// structure
void structure()
{
int no;int per[5];
int a1,a2,a3,a4,a5;
printf("\nhere you can store information of 5 student and can get per of student\n");
struct st a[5];
printf("\nfirstly enter the info\n");
for(int i=0;i<1;i++)
{
printf("enter info for %d student",i+1);
printf("\nenter the name\n");
scanf(" %s",&a[i].name);
// gets(a[0].name);
printf("enter age\n");
scanf("%d",&a[i].age);
printf("\nenter roll\n");
scanf("%d",&a[i].roll);
printf("enter the marks for 5 sub out of 100\n");
printf("marks for phy\n");
scanf("%d",&a[i].marks[0]);
printf("%d",a[i].marks[0]);
a1=a[i].marks[0];
printf("%d",a1);
printf("marks for math\n");
scanf("%d",&a[i].marks[1]);
a2=a[i].marks[1];
printf("marks for pc\n");
scanf("%d",&a[i].marks[2]);
a3=a[i].marks[2];
printf("marks for pee\n");
scanf("%d",&a[i].marks[3]);
a4=a[i].marks[3];
printf("marks for pic\n");
scanf("%d",&a[i].marks[4]);
a5=a[i].marks[4];
per[i]=(a1+a2+a3+a4+a5);
}
//int sum;
// sum=a1+a2+a3+a4+a5;
//printf("1st element%d",sum);
display(per,5);
dispstr(a,per);
}
void dispstr(struct st *a,int per[])
{
printf("you have entered \n");
for(int k=0;k<1;k++)
{
printf("The name of %d student is %s \n",k+1,a[k].name);
printf("The age is %d\n",a[k].age);
printf("The roll is %d \n",a[k].roll);
display(per,5);
float p;
p=per[k]*0.01;
printf("The percentage of student %d is %f\n\n\n\n",k+1,p);
}
}
// for string input
void string()
{
char str[25]; int len;
printf("enter the string \n");
//gets(str);//ye work nahi kr rha hai
scanf(" %s",str);
//puts(str); // ye kaam kar ra hai
char ch1='y';int ch;
while(ch1=='y')
{
printf("\t\t\t\tMENU");
printf("\n 1 length counting\n");
printf("\n 2 reverse the string\n");
printf("\n 3 for copying the string in anthor variable\n");
printf("\n 4 for concatenation \n");
printf("\n 5 for going to main function \n");
printf("\n 6 for exit");
printf("\n 7 for display");
printf("\n 8 for converting lower case to uppercase");
printf("\nenter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: len=lenstr(str);
printf("length of string is %d\n",len);
break;
case 2: revstr(str);
break;
case 3: char q[200];
copystr(str,q);
printf("%s",q);
break;
case 4: constr();
// strcat(a,b);
//printf("%s",a);
break;
case 5: main();
break;
case 6: exit(1);
break;
case 7: dispstr(str);
break;
case 8: upperstr(str);
break;
default: printf("wrong choice\n");
break;
}
printf("\ndo want to enter more choice in string section y or n\n");
scanf(" %c",&ch1);
}
}
// length of string
int lenstr(char str[])
{
int len=0;
while(str[len]!='\0')
{
len++;
}
return len;
}
// display string
void dispstr(char str[])
{
puts(str);
}
// string reverse
void revstr(char str[])
{
int len;
len=lenstr(str);//printf("%d",len);
int i=0;
int j=len-1;
while(i>j)// ye loop nahi chal ra hai
{
swap2(str[i],str[j]);
}
puts(str);
}
// copy a string using pointer
int copystr(char *p,char *q)
{
while(*p!='\0')
{
*q=*p;
p++;
q++;
}
*q='\0';
return 0;
}
//lower to upper
void upperstr(char str[])
{
int i=0;
while(str[i]!='\0')
{
if((str[i]>='a')&&(str[i]<='z'))
{
str[i]=str[i]-32;
}
i++;
}
puts(str);
}
// concatination
void constr()
{
char a[10],b[10];
printf("enter two strings\n");
scanf(" %s%s",a,b);
int l1,l2;
l1=strlen(a);
l2=strlen(b);
for(int i=0;i<=l2;i++)
{
a[l1+i]=b[i];
}
puts(a);
}
/*void addstr(char *p,char *q)
{
int len1=0;
int len2=0;
while(*p!='\0')
{
len1++;
p++;
}
printf("len of 1 is %d\n",len1);
while(*q!='\0')
{
len2++;
q++;
}
printf("len of 2 is %d\n",len2);
for(int i=0;i<len2;i++)
{
printf("%c",*p);
// *p=q[i];
// p++;
}
*(p+1)='\0';
printf("%s",p);
}*/
No comments:
Post a Comment