Posted by
snehil
Saturday, January 29, 2011
The user is provided with the following options..
To insert an item at begining
To insert an item at end
To insert an item at position
To traverse the list
To delete first item
To delete last item
To delete item from position
To count no. of items in list
- #include<conio.h>
- #include<stdio.h>
- #include<alloc.h>
- #include<process.h>
- struct node
- { int info;
- struct node *next;
- };
- typedef struct node sn;
- void insertatbeg(sn **,int);
- void insertatend(sn **,int);
- void insertatpos(sn **,int,int);
- void traverse(sn *);
- int delfirst(sn *);
- int dellast(sn *);
- int delpos(sn *,int);
- int count(sn *);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void main()
- { sn *p;
- int n,i,l;
- char ch;
- clrscr();
- p=NULL;
- do
- { printf("\n-----select an operation-----");
- printf("\n1->To insert an item at begining<-");
- printf("\n2->To insert an item at end<-");
- printf("\n3->To insert an item at position<-");
- printf("\n4->To traverse the list<-");
- printf("\n5->To delete first item<-");
- printf("\n6->To delete last item<-");
- printf("\n7->To delete item from position<-");
- printf("\n8->To count no. of items in list<-");
- printf("\n9->Exit<-");
- printf("\n->Enter your choice:");
- scanf("%d",&n);
- switch(n)
- { case 1:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatbeg(&p,i);
- break;
- case 2:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatend(&p,i);
- break;
- case 3:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- printf("\n->Enter the position");
- scanf("%d",&l);
- insertatpos(&p,i,l);
- break;
- case 4:traverse(p);
- break;
- case 5:i=delfirst(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 6:i=dellast(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 7:printf("\n->Enter the position");
- scanf("%d",&l);
- i=delpos(p,l);
- printf("\nItem deleted is %d.",i);
- break;
- case 8:i=count(p);
- printf("\nNumber of nodes in list are %d.",i);
- break;
- case 9:exit(0);
- break;
- default:printf("\n->You have entered wrong choice");
- }
- printf("\n->Do you want to continue/exit(y/n)");
- fflush(stdin);
- scanf("%c",&ch);
- } while((ch=='y')||(ch=='Y'));
- getch();
- }
- int count(sn *p)
- { sn *temp;
- int c=1;
- temp=p;
- while((temp->next)!=NULL)
- { c++;
- temp=temp->next;
- }
- return c;
- }
- void insertatbeg(sn **p,int item)
- { sn *ptr;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- if(*p==NULL)
- ptr->next=NULL;
- else
- ptr->next=*p;
- *p=ptr;
- }
- void insertatend(sn **p,int item)
- { sn *ptr,*temp;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- ptr->next=NULL;
- if(*p==NULL)
- *p=ptr;
- else
- { temp=*p;
- while((temp->next)!=NULL)
- temp=temp->next;
- temp->next=ptr;
- }
- }
- void insertatpos(sn **p,int item,int loc)
- { sn *ptr,*temp,*s;
- int i,c;
- temp=*p;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- c=count(*p);
- if(loc>c)
- printf("\nList is short,Item can't inserted");
- else
- {
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- ptr->next=s->next;
- s->next=ptr;
- }
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void traverse(sn *p)
- { sn *temp;
- if(p==NULL)
- printf("\nList is empty.");
- else
- {
- temp=p;
- while((temp->next)!=NULL)
- { printf("\nno.=%d",temp->info);
- temp=temp->next;
- }
- printf("\nno.=%d",temp->info);
- }
- }
- int delfirst(sn *p)
- { int item;
- sn *temp;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- p=p->next;
- item=temp->info;
- free(temp);
- return item;
- }
- }
- int dellast(sn *p)
- { int item;
- sn *temp,*s;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- while((temp->next)!=NULL)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=NULL;
- free(temp);
- return item;
- }
- }
- int delpos(sn *p,int loc)
- { int item,i,c;
- sn *temp,*s;
- c=count(p);
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { if(loc>c)
- { printf("\nItem is not in list.");
- return 0;
- }
- else
- { temp=p;
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=temp->next;
- free(temp);
- return item;
- }
- }
- }
Posted by
snehil
User is asked to Insert into.. Delete from or Display the entire queue..
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- int queue[5];
- long front,rear;
- void initqueue();
- void display();
- void main()
- {
- int choice,info;
- clrscr();
- while(1)
- {
- clrscr();
- printf(" MENU \n");
- printf("1.Insert an element in queue\n");
- printf("2.Delete an element from queue\n");
- printf("3.Display the queue\n");
- printf("4.Exit!\n");
- printf("Your choice: ");
- scanf("%i",&choice);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- switch(choice)
- {
- case 1:if(rear<4)
- {
- printf("enter the number: ");
- scanf("%d",&info);
- if (front==-1)
- {
- front=0;
- rear=0;
- }
- else
- rear=rear+1;
- queue[rear]=info;
- }
- else
- printf("queue is full");
- getch();
- break;
- case 2: int info;
- if(front!=-1)
- {
- info=queue[front];
- if(front==rear)
- {
- front=-1;
- rear=-1;
- }
- else
- front=front+1;
- printf("no deleted is = %d",info);
- }
- else
- printf("queue is empty");
- getch();
- break;
- case 3: display();
- getch();
- break;
- case 4: exit(1);
- break;
- default:printf("You entered wrong choice!");
- getch();
- break;
- }
- }
- }
- void initqueue()
- {
- front=rear=-1;
- }
- void display()
- {
- int i;
- for(i=front;i<=rear;i++)
- printf("%i\n",queue[i]);
- }
Posted by
snehil
User is asked to PUSH, POP , or TRAVERSE.. PUSH is to add items to the Stack, POP is to remove item from the Stack, and TRAVERSE is to traverse the whole Stack
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- #define MAXSIZE 10
- void push();
- int pop();
- void traverse();
- int stack[MAXSIZE];
- int Top=-1;
- void main()
- {
- int choice;
- char ch;
- do
- {
- clrscr();
- printf("\n1. PUSH ");
- printf("\n2. POP ");
- printf("\n3. TRAVERSE ");
- printf("\nEnter your choice ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: push();
- break;
- case 2: printf("\nThe deleted element is %d ",pop());
- break;
- case 3: traverse();
- break;
- default: printf("\nYou Entered Wrong Choice");
- }
- printf("\nDo You Wish To Continue (Y/N)");
- fflush(stdin);
- scanf("%c",&ch);
- }
- while(ch=='Y' || ch=='y');
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void push()
- {
- int item;
- if(Top == MAXSIZE - 1)
- {
- printf("\nThe Stack Is Full");
- getch();
- exit(0);
- }
- else
- {
- printf("Enter the element to be inserted ");
- scanf("%d",&item);
- Top= Top+1;
- stack[Top] = item;
- }
- }
-
- int pop()
- {
- int item;
- if(Top == -1)
- {
- printf("The stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- item = stack[Top];
- Top = Top-1;
- }
- return(item);
- }
-
- void traverse()
- {
- int i;
- if(Top == -1)
- {
- printf("The Stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- for(i=Top;i>=0;i--)
- {
- printf("Traverse the element ");
- printf("%d\n",stack[i]);
- }
- }
- }
Posted by
snehil
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100],n,i,j,temp,loc,min;
- clrscr();
- printf("\How many elements:\n");
- scanf("%d",&n);
- printf("Enter the element of array\n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- min=a[0];
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<=n-1;i++)
- {
- min=a[i];
- loc=i;
- for(j=i+1;j<=n-1;j++)
- {
- if(a[j]<min)
- {
- min=a[j];
- loc=j;
- }
- }
- if(loc!=1)
- {
- temp=a[i];
- a[i]=a[loc];
- a[loc]=temp;
- }
-
- }
- printf("The number after selection sorting are:\n");
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- getch();
- }
Posted by
snehil
Friday, January 28, 2011
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- #define max 100
- int a[max],n,i,l,h;
- void main()
- {
- void input(void);
- input();
- getch();
- }
-
- void input(void)
- {
- void output(int a[],int n);
- void quick_sort(int a[],int l,int h);
- printf("How many elements in the array : ");
- scanf("%d",&n);
- printf("\n");
- printf("Enter the elemennts : \n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- l=0;
- h=n-1;
- quick_sort(a,l,h);
- printf("Sorted Array :\n ");
- output(a,n);
- }
-
- void quick_sort(int a[],int l, int h)
- {
- int temp,key,low,high;
- low=l;
- high=h;
- key=a[(low+high)/2];
- do
- {
- while(key>a[low])
- {
- low++;
- }
- while(key<a[high])
- {
- high--;
- }
- if(low<=high)
- {
- temp=a[low];
- a[low++]=a[high];
- a[high--]=temp;
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- } while(low<=high);
- if(l<high)
- quick_sort(a,l,high);
- if(low<h)
- quick_sort(a,low,h);
- }
- void output(int a[],int n)
- {
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- }
Posted by
snehil
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- main()
- {
- int a[100],n,k,i,j,temp;
- printf("How many elements: \n");
- scanf("%d",&n);
- printf("Enter the element of array: \n");
- for(i=0;i<=n-1;i++)
- scanf("%d",&a[i]);
- for(k=1;k<=n-1;k++)
- {
- temp=a[k];
- j=k-1;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- while((temp<a[j])&&(j>=0))
- {
- a[j+1]=a[j];
- j=j-1;
- }
- a[j+1]=temp;
- }
- printf("Elements of array after sorting: \n");
- for(i=0;i<=n-1;i++)
- printf("%d\n",a[i]);
- getch();
- }
Posted by
snehil
Friday, September 18, 2009
Posted by
snehil
Friday, September 4, 2009
The difference between a++ and ++a, is that when we use a++ in a program and then the effect would be on the next statement while the ++a, effect is that on the current line not on the next statement.
Output:
Example code:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a,b;
- a=1;
- cout<<a<<endl;
- cout<<a++<<endl;
- cout<<a<<endl<<endl;
- b=5;
- cout<<b<<endl;
- cout<<++b<<endl;
- cout<<b<<endl;
- getch();
- }
Posted by
snehil
Thursday, July 16, 2009
Name age and address of students are stored in a string..
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- clrscr();
- char name[100],age[100],address[100];
- cout<<"Please enter a name: ";
- gets(name);
- cout<<"Please enter age: ";
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- gets(age);
- cout<<"Please eneter address: ";
- gets(address);
- cout<<endl<<endl<<"Name is: "<<name<<endl<<"Age is: "<<age<<endl<<"Address is:
- "<<address;
- getch();
- }
Posted by
snehil
A line is taken from user and number of words is calculated..
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- clrscr();
- char str[100];
- int w=1;
- cout<<"Please enter a String: ";
- gets(str);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for (int i=0;str[i]!='\0';i++)
- {
- if(str[i]==' ')
- w++;
- }
- cout<<"There are "<<w<<" words.";
- getch();
- }
Posted by
snehil
A word is taken from the user and number of vowels is determined..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- char str[10];
- int v=0;
- cout<<"Please enter a string: ";
- cin>>str;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(int i=0;str[i]!='\0';i++)
- {
- if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
- v++;
- }
- cout<<v<<" Number of Vowels are there.";
- getch();
- }
Posted by
snehil
A character is taken from user and is checked if it is in UPPER CASE or lower case..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- char x;
- cout<<"Please enter a character: ";
- cin>>x;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- if(x>='A'&&x<='Z')
- cout<<x<<" is in UPPER CASE:)";
- else
- cout<<x<<" is in lower case :)";
- getch();
- }
Posted by
snehil
A character is taken from user.. and is checked if it is a vowel or a consonant..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- char x;
- cout<<"Please enter a character: ";
- cin>>x;
- if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='o'||x=='O'||x=='u'||x=='U')
- cout<<x<<" is a Vowel :)";
- else
- cout<<x<<" is a consonant :)";
- getch();
- }
Posted by
snehil
A matrix is taken from user.. and its Transpose is displayed as output..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a[10][10],m,n,i,j;
- cout<<"Enter number of rows: ";
- cin>>m;
- cout<<"Enter number of coloumns: ";
- cin>>n;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- if(m!=n)
- {
- cout<<"Matrix not square so Transpose not possible :(";
- }
- else
- {
- cout<<endl<<"Enter elements of matrix: "<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element a"<<i+1<<j+1<<": ";
- cin>>a[i][j];
- }
- }
- cout<<endl<<"Displaying Matrix: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[j][i]<<" ";
- }
- cout<<endl<<endl;
- }
- }
- getch();
- }
Posted by
snehil
Two matrices are taken from user..multiplication matrix is displayed as output.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j;
- cout<<"Enter number of rows of A: ";
- cin>>m;
- cout<<"Enter number of coloumns of A: ";
- cin>>n;
- cout<<endl<<"Enter elements of matrix A: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element a"<<i+1<<j+1<<": ";
- cin>>a[i][j];
- }
- }
- cout<<endl<<"Enter number of rows of B: ";
- cin>>o;
- cout<<"Enter number of coloumns of B: ";
- cin>>p;
- cout<<endl<<"Enter elements of matrix B: "<<endl;
- for(i=0;i<o;i++)
- {
- for(j=0;j<p;j++)
- {
- cout<<"Enter element b"<<i+1<<j+1<<": ";
- cin>>b[i][j];
- }
- }
-
- cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
- for(i=0;i<o;i++)
- {
- for(j=0;j<p;j++)
- {
- cout<<b[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- if(n==o)
- {
- for(i=0;i<m;i++)
- {
- for(j=0;j<p;j++)
- {
- c[i][j]=0;
- for(int k=0;k<n;k++)
- {
- c[i][j]=c[i][j]+a[i][k]*b[k][j];
- }
- }
- }
- cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<p;j++)
- {
- cout<<c[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- }
- else
- cout<<"Multiplication not possible :(";
- getch();
- }
Posted by
snehil
Two matrix are taken from user.. Substraction matrix is displayed as output..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a[10][10],b[10][10],c[10][10],m,n,i,j;
- cout<<"Enter number of rows: ";
- cin>>m;
- cout<<"Enter number of coloumns: ";
- cin>>n;
- cout<<endl<<"Enter elements of matrix A: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element a"<<i+1<<j+1<<": ";
- cin>>a[i][j];
- }
- }
- cout<<endl<<"Enter elements of matrix B: "<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element b"<<i+1<<j+1<<": ";
- cin>>b[i][j];
- }
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<b[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Matrix A - Matrix B = Matrix C: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]-b[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- getch();
- }
Posted by
snehil
Two matrix are taken from user.. Sum matrix is displayed as output..
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a[10][10],b[10][10],c[10][10],m,n,i,j;
- cout<<"Enter number of rows: ";
- cin>>m;
- cout<<"Enter number of coloumns: ";
- cin>>n;
- cout<<endl<<"Enter elements of matrix A: "<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element a"<<i+1<<j+1<<": ";
- cin>>a[i][j];
- }
- }
- cout<<endl<<"Enter elements of matrix B: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element b"<<i+1<<j+1<<": ";
- cin>>b[i][j];
- }
- }
-
- cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<b[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]+b[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
-
- getch();
- }
Posted by
snehil
m(row) and n(coloumns) are taken from user.. Elements are taken from user.. matrix is displayed in tabular form..
This should be your first program for Matrix.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a[10][10],m,n,i,j;
- cout<<"Enter number of rows: ";
- cin>>m;
- cout<<"Enter number of coloumns: ";
- cin>>n;
- cout<<endl<<"Enter elements of matrix: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<"Enter element a"<<i+1<<j+1<<": ";
- cin>>a[i][j];
- }
- }
- cout<<endl<<"Displaying Matrix: "<<endl<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<a[i][j]<<" ";
- }
- cout<<endl<<endl;
- }
- getch();
- }
Posted by
snehil
Size of array is taken from user.. Elements are Entered.. Array is shown.. user is prompted for adding another element.. new element is added at desired position.. user is prompted to delete an element.. Element is deleted from entered position..
- #include<conio.h>
- #include<iostream.h>
- void main()
- {
- clrscr();
- int a[100],i,pos,num,item,size;
- cout<<"How many element: ";
- cin>>size;
- cout<<"Enter the elements of array: "<<endl;
- for(i=0;i<size;i++)
- cin>>a[i];
- cout<<endl;
- cout<<"Now Array is: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- cout<<endl<<"Press any key to add another element"<<endl<<endl;
- getch();
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<"Enter another element: ";
- cin>>num;
- cout<<"Enter position number: ";
- cin>>pos;
- cout<<endl;
- --pos;
- for(i=size;i>=pos;i--)
- a[i+1]=a[i];
- a[pos]=num;
- size++;
- cout<<"New Array: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- cout<<endl<<"Press any key to delete any element"<<endl<<endl;
- getch();
-
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<"Enter position number to delete: ";
- cin>>pos;
- --pos;
- item=a[pos];
- for(i=pos;i<size;i++)
- a[i]=a[i+1];
- size--;
- cout<<"Deleted element: "<<item<<endl;
- cout<<endl<<"New array: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- getch();
- }
Posted by
snehil
Size of array is taken from user.. Elemnts are entered by user.. Elements are sorted in ascending order..
- #include<conio.h>
- #include<iostream.h>
- void main()
- {
- clrscr();
- int a[100],i,n,j,temp;
- cout<<"How many element: ";
- cin>>n;
- cout<<"Enter the elements of array: "<<endl;
- for(i=0;i<n;i++)
- cin>>a[i];
- cout<<"The elements of array Before Sorting: "<<endl;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<n;i++)
- cout<<a[i]<<" ";
- cout<<endl;
- for(i=0;i<n;i++)
- {
- for(j=0;j<n-1-i;j++)
- {
- if(a[j]>a[j+1])
- {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- }
- cout<<"Elements of array After Sorting: "<<endl;
- for(i=0;i<n;i++)
- cout<<a[i]<<" ";
- getch();
- }