Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Write a program to Implement a Linked List in C++

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





Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>

  2. #include<stdio.h>

  3. #include<alloc.h>

  4. #include<process.h>

  5. struct node

  6. { int info;

  7. struct node *next;

  8. };


  9. typedef struct node sn;


  10. void insertatbeg(sn **,int);

  11. void insertatend(sn **,int);

  12. void insertatpos(sn **,int,int);

  13. void traverse(sn *);

  14. int delfirst(sn *);

  15. int dellast(sn *);

  16. int delpos(sn *,int);

  17. int count(sn *);

  18. //Coding by: Snehil Khanor

  19. //http://WapCPP.blogspot.com

  20. void main()

  21. { sn *p;

  22. int n,i,l;

  23. char ch;

  24. clrscr();

  25. p=NULL;

  26. do

  27. { printf("\n-----select an operation-----");

  28. printf("\n1->To insert an item at begining<-");

  29. printf("\n2->To insert an item at end<-");

  30. printf("\n3->To insert an item at position<-");

  31. printf("\n4->To traverse the list<-");

  32. printf("\n5->To delete first item<-");

  33. printf("\n6->To delete last item<-");

  34. printf("\n7->To delete item from position<-");

  35. printf("\n8->To count no. of items in list<-");

  36. printf("\n9->Exit<-");

  37. printf("\n->Enter your choice:");

  38. scanf("%d",&n);

  39. switch(n)

  40. { case 1:printf("\n->Enter the item to insert");

  41. scanf("%d",&i);

  42. insertatbeg(&p,i);

  43. break;

  44. case 2:printf("\n->Enter the item to insert");

  45. scanf("%d",&i);

  46. insertatend(&p,i);

  47. break;

  48. case 3:printf("\n->Enter the item to insert");

  49. scanf("%d",&i);

  50. printf("\n->Enter the position");

  51. scanf("%d",&l);

  52. insertatpos(&p,i,l);

  53. break;

  54. case 4:traverse(p);

  55. break;

  56. case 5:i=delfirst(p);

  57. printf("\nItem deleted is %d.",i);

  58. break;

  59. case 6:i=dellast(p);

  60. printf("\nItem deleted is %d.",i);

  61. break;

  62. case 7:printf("\n->Enter the position");

  63. scanf("%d",&l);

  64. i=delpos(p,l);

  65. printf("\nItem deleted is %d.",i);

  66. break;

  67. case 8:i=count(p);

  68. printf("\nNumber of nodes in list are %d.",i);

  69. break;

  70. case 9:exit(0);

  71. break;

  72. default:printf("\n->You have entered wrong choice");

  73. }

  74. printf("\n->Do you want to continue/exit(y/n)");

  75. fflush(stdin);

  76. scanf("%c",&ch);

  77. } while((ch=='y')||(ch=='Y'));

  78. getch();

  79. }


  80. int count(sn *p)

  81. { sn *temp;

  82. int c=1;

  83. temp=p;

  84. while((temp->next)!=NULL)

  85. { c++;

  86. temp=temp->next;

  87. }

  88. return c;

  89. }


  90. void insertatbeg(sn **p,int item)

  91. { sn *ptr;

  92. ptr=(sn *)malloc(sizeof(sn));

  93. ptr->info=item;

  94. if(*p==NULL)

  95. ptr->next=NULL;

  96. else

  97. ptr->next=*p;

  98. *p=ptr;

  99. }


  100. void insertatend(sn **p,int item)

  101. { sn *ptr,*temp;

  102. ptr=(sn *)malloc(sizeof(sn));

  103. ptr->info=item;

  104. ptr->next=NULL;

  105. if(*p==NULL)

  106. *p=ptr;

  107. else

  108. { temp=*p;

  109. while((temp->next)!=NULL)

  110. temp=temp->next;

  111. temp->next=ptr;

  112. }

  113. }


  114. void insertatpos(sn **p,int item,int loc)

  115. { sn *ptr,*temp,*s;

  116. int i,c;

  117. temp=*p;

  118. ptr=(sn *)malloc(sizeof(sn));

  119. ptr->info=item;

  120. c=count(*p);

  121. if(loc>c)

  122. printf("\nList is short,Item can't inserted");

  123. else

  124. {

  125. for(i=0;i<loc-1;i++)

  126. { s=temp;

  127. temp=temp->next;

  128. }

  129. ptr->next=s->next;

  130. s->next=ptr;

  131. }

  132. }


  133. //Coding by: Snehil Khanor

  134. //http://WapCPP.blogspot.com

  135. void traverse(sn *p)

  136. { sn *temp;

  137. if(p==NULL)

  138. printf("\nList is empty.");

  139. else

  140. {

  141. temp=p;

  142. while((temp->next)!=NULL)

  143. { printf("\nno.=%d",temp->info);

  144. temp=temp->next;

  145. }

  146. printf("\nno.=%d",temp->info);

  147. }

  148. }


  149. int delfirst(sn *p)

  150. { int item;

  151. sn *temp;

  152. if(p==NULL)

  153. { printf("\nList is empty");

  154. return 0;

  155. }

  156. else

  157. { temp=p;

  158. p=p->next;

  159. item=temp->info;

  160. free(temp);

  161. return item;

  162. }

  163. }


  164. int dellast(sn *p)

  165. { int item;

  166. sn *temp,*s;

  167. if(p==NULL)

  168. { printf("\nList is empty");

  169. return 0;

  170. }

  171. else

  172. { temp=p;

  173. while((temp->next)!=NULL)

  174. { s=temp;

  175. temp=temp->next;

  176. }

  177. item=temp->info;

  178. s->next=NULL;

  179. free(temp);

  180. return item;

  181. }

  182. }


  183. int delpos(sn *p,int loc)

  184. { int item,i,c;

  185. sn *temp,*s;

  186. c=count(p);

  187. if(p==NULL)

  188. { printf("\nList is empty");

  189. return 0;

  190. }

  191. else

  192. { if(loc>c)

  193. { printf("\nItem is not in list.");

  194. return 0;

  195. }

  196. else

  197. { temp=p;

  198. for(i=0;i<loc-1;i++)

  199. { s=temp;

  200. temp=temp->next;

  201. }

  202. item=temp->info;

  203. s->next=temp->next;

  204. free(temp);

  205. return item;

  206. }

  207. }

  208. }


Write a program to Implement a Queue in C++

User is asked to Insert into.. Delete from or Display the entire queue..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #include<process.h> 

  4. int queue[5]; 

  5. long front,rear; 

  6. void initqueue(); 

  7. void display(); 

  8. void main() 


  9. int choice,info; 

  10. clrscr(); 

  11. while(1) 


  12. clrscr(); 

  13. printf(" MENU \n"); 

  14. printf("1.Insert an element in queue\n"); 

  15. printf("2.Delete an element from queue\n"); 

  16. printf("3.Display the queue\n"); 

  17. printf("4.Exit!\n"); 

  18. printf("Your choice: "); 

  19. scanf("%i",&choice); 

  20. //Coding by: Snehil Khanor  

  21. //http://WapCPP.blogspot.com  

  22. switch(choice) 


  23. case 1:if(rear<4) 


  24. printf("enter the number: "); 

  25. scanf("%d",&info); 

  26. if (front==-1) 


  27. front=0; 

  28. rear=0; 


  29. else 

  30. rear=rear+1; 

  31. queue[rear]=info; 


  32. else 

  33. printf("queue is full"); 

  34. getch(); 

  35. break; 

  36. case 2: int info; 

  37. if(front!=-1) 


  38. info=queue[front]; 

  39. if(front==rear) 


  40. front=-1; 

  41. rear=-1; 


  42. else 

  43. front=front+1; 

  44. printf("no deleted is = %d",info); 


  45. else 

  46. printf("queue is empty"); 

  47. getch(); 

  48. break; 

  49. case 3: display(); 

  50. getch(); 

  51. break; 

  52. case 4: exit(1); 

  53. break; 

  54. default:printf("You entered wrong choice!"); 

  55. getch(); 

  56. break; 




  57. void initqueue() 


  58. front=rear=-1; 


  59. void display() 


  60. int i; 

  61. for(i=front;i<=rear;i++) 

  62. printf("%i\n",queue[i]); 


Write a program to Implement a stack in C++

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

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #include<process.h> 

  4. #define MAXSIZE 10 

  5. void push(); 

  6. int pop(); 

  7. void traverse(); 

  8. int stack[MAXSIZE]; 

  9. int Top=-1; 

  10. void main() 


  11. int choice; 

  12. char ch; 

  13. do 


  14. clrscr(); 

  15. printf("\n1. PUSH "); 

  16. printf("\n2. POP "); 

  17. printf("\n3. TRAVERSE "); 

  18. printf("\nEnter your choice "); 

  19. scanf("%d",&choice); 

  20. switch(choice) 


  21. case 1: push(); 

  22. break; 

  23. case 2: printf("\nThe deleted element is %d ",pop()); 

  24. break; 

  25. case 3: traverse(); 

  26. break; 

  27. default: printf("\nYou Entered Wrong Choice"); 


  28. printf("\nDo You Wish To Continue (Y/N)"); 

  29. fflush(stdin); 

  30. scanf("%c",&ch); 


  31. while(ch=='Y' || ch=='y'); 


  32. //Coding by: Snehil Khanor  

  33. //http://WapCPP.blogspot.com  

  34. void push() 


  35. int item; 

  36. if(Top == MAXSIZE - 1) 


  37. printf("\nThe Stack Is Full"); 

  38. getch(); 

  39. exit(0); 


  40. else 


  41. printf("Enter the element to be inserted "); 

  42. scanf("%d",&item); 

  43. Top= Top+1; 

  44. stack[Top] = item; 



  45.  

  46. int pop() 


  47. int item; 

  48. if(Top == -1) 


  49. printf("The stack is Empty"); 

  50. getch(); 

  51. exit(0); 


  52. else 


  53. item = stack[Top]; 

  54. Top = Top-1; 


  55. return(item); 


  56.  

  57. void traverse() 


  58. int i; 

  59. if(Top == -1) 


  60. printf("The Stack is Empty"); 

  61. getch(); 

  62. exit(0); 


  63. else 


  64. for(i=Top;i>=0;i--) 


  65. printf("Traverse the element "); 

  66. printf("%d\n",stack[i]); 




Write a program to sort elements of array using Selection Sorting

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. void main() 


  4. int a[100],n,i,j,temp,loc,min; 

  5. clrscr(); 

  6. printf("\How many elements:\n"); 

  7. scanf("%d",&n); 

  8. printf("Enter the element of array\n"); 

  9. for(i=0;i<=n-1;i++) 


  10. scanf("%d",&a[i]); 


  11. min=a[0]; 

  12. //Coding by: Snehil Khanor  

  13. //http://WapCPP.blogspot.com  

  14. for(i=0;i<=n-1;i++) 


  15. min=a[i]; 

  16. loc=i; 

  17. for(j=i+1;j<=n-1;j++) 


  18. if(a[j]<min) 


  19. min=a[j]; 

  20. loc=j; 



  21. if(loc!=1) 


  22. temp=a[i]; 

  23. a[i]=a[loc]; 

  24. a[loc]=temp; 


  25.  


  26. printf("The number after selection sorting are:\n"); 

  27. for(i=0;i<=n-1;i++) 


  28. printf("%d\n",a[i]); 


  29. getch(); 



Write a program to sort elements of array using Quick Sort

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #define max 100 

  4. int a[max],n,i,l,h; 

  5. void main() 


  6. void input(void); 

  7. input(); 

  8. getch(); 


  9.  

  10. void input(void) 


  11. void output(int a[],int n); 

  12. void quick_sort(int a[],int l,int h); 

  13. printf("How many elements in the array : "); 

  14. scanf("%d",&n); 

  15. printf("\n"); 

  16. printf("Enter the elemennts : \n"); 

  17. for(i=0;i<=n-1;i++) 


  18. scanf("%d",&a[i]); 


  19. l=0; 

  20. h=n-1; 

  21. quick_sort(a,l,h); 

  22. printf("Sorted Array :\n "); 

  23. output(a,n); 


  24.  

  25. void quick_sort(int a[],int l, int h) 


  26. int temp,key,low,high; 

  27. low=l; 

  28. high=h; 

  29. key=a[(low+high)/2]; 

  30. do 


  31. while(key>a[low]) 


  32. low++; 


  33. while(key<a[high]) 


  34. high--; 


  35. if(low<=high) 


  36. temp=a[low]; 

  37. a[low++]=a[high]; 

  38. a[high--]=temp; 


  39. //Coding by: Snehil Khanor 

  40. //http://WapCPP.blogspot.com 

  41. } while(low<=high); 

  42. if(l<high) 

  43. quick_sort(a,l,high); 

  44. if(low<h) 

  45. quick_sort(a,low,h); 


  46. void output(int a[],int n) 


  47. for(i=0;i<=n-1;i++) 


  48. printf("%d\n",a[i]); 


  49. }

Write a program to sort elements of array using Insert Sorting

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. main() 


  3. int a[100],n,k,i,j,temp; 

  4. printf("How many elements: \n"); 

  5. scanf("%d",&n); 

  6. printf("Enter the element of array: \n"); 

  7. for(i=0;i<=n-1;i++) 

  8. scanf("%d",&a[i]); 

  9. for(k=1;k<=n-1;k++) 


  10. temp=a[k]; 

  11. j=k-1; 

  12. //Coding by: Snehil Khanor 

  13. //http://WapCPP.blogspot.com 

  14. while((temp<a[j])&&(j>=0)) 


  15. a[j+1]=a[j]; 

  16. j=j-1; 


  17. a[j+1]=temp; 


  18. printf("Elements of array after sorting: \n"); 

  19. for(i=0;i<=n-1;i++) 

  20. printf("%d\n",a[i]); 

  21. getch();


Memory Allocation example for Double Pointer of a class

The following program should be self explanatory:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to demonstrate memory allocation to double pointers
#include<iostream>

using namespace
std;

//Class named NewClass
class NewClass
{

public
:
int
a;
int
* b;
void
* c;
};


//List Structure containing multiple NewClass objects
struct NewClassList
{

int
numOfObjects;
NewClass** Objects;
};


int
main()
{

//Create NewClassList with 4 objects with following values:
//1, 10, 100; 2, 20; 200; 3, 30, 300; 4, 40, 400;
NewClassList someList;
someList.numOfObjects = 4;
//someList.Objects = new NewClass*; //1. Common mistake in allocating memory
someList.Objects = new NewClass*[someList.numOfObjects];

someList.Objects[0] = new NewClass;
someList.Objects[0]->a = 1;
someList.Objects[0]->b = new int(10);
someList.Objects[0]->c = (void*) new int(100);

someList.Objects[1] = new NewClass;
someList.Objects[1]->a = 2;
someList.Objects[1]->b = new int(20);
someList.Objects[1]->c = (void*) new int(200);

someList.Objects[2] = new NewClass;
someList.Objects[2]->a = 3;
someList.Objects[2]->b = new int(30);
someList.Objects[2]->c = (void*) new int(300);

someList.Objects[3] = new NewClass;
someList.Objects[3]->a = 4;
someList.Objects[3]->b = new int(40);
someList.Objects[3]->c = (void*) new int(400);

//Printing the outputs
for(int i = 0; i < someList.numOfObjects; i++)
{

cout<<"Iteration = "<<i;
cout<<": a = "<<someList.Objects[i]->a;
cout<<": b = "<<*(someList.Objects[i]->b);
cout<<": c = "<<*((int*)someList.Objects[i]->c)<<endl;
}


//Clear all the memory allocation
for(int i = 0; i < someList.numOfObjects; i++)
{

delete
someList.Objects[i]->b;
delete
someList.Objects[i]->c;
delete
someList.Objects[i];
}

delete
[] someList.Objects;
return
0;
}





The output is as follows:

The challenging 'Infinite loop' problem

Picked this one up from The C++ blog.

Consider the following program:



#include <iostream>

int
main()
{

int
i;
int
array[4];
for
(i=0; i<=8; i++)
{

array[i]=0;
}


return
0;
}


Most people including myself expect this program to crash. This is not the case. The reason being the way everything is stored on stack. In simple representation, the storage is something like this:



So when i = 6, value of i is reset to 0 and the loop continues forever.

After digging some information on stack, heap, etc. I found some useful info in C++ in 24 hours:

Programmers generally deal with five areas of memory:
  • Global name space
  • The free store (a.k.a. Heap)
  • Registers
  • Code space
  • The stack

Local variables are on the stack, along with function parameters. Code is in code space, of course, and global variables are in global name space. The registers are used for internal housekeeping functions, such as keeping track of the top of the stack and the instruction pointer. Just about all remaining memory is given over to the free store, which is sometimes referred to as the heap.

The problem with local variables is that they don't persist. When the function returns, the local variables are thrown away. Global variables solve that problem at the cost of unrestricted access throughout the program, which leads to the creation of code that is difficult to understand and maintain. Putting data in the free store solves both of these problems.

You can think of the free store as a massive section of memory in which thousands of sequentially numbered cubbyholes lie waiting for your data. You can't label these cubbyholes, though, as you can with the stack. You must ask for the address of the cubbyhole that you reserve and then stash that address away in a pointer.

The stack is cleaned automatically when a function returns. All the local variables go out of scope, and they are removed from the stack. The free store is not cleaned until your program ends, and it is your responsibility to free any memory that you've reserved when you are done with it.

The advantage to the free store is that the memory you reserve remains available until you explicitly free it. If you reserve memory on the free store while in a function, the memory is still available when the function returns.

The advantage of accessing memory in this way, rather than using global variables, is that only functions with access to the pointer have access to the data. This provides a tightly controlled interface to that data, and it eliminates the problem of one function changing that data in unexpected and unanticipated ways.

For this to work, you must be able to create a pointer to an area on the free store and to pass that pointer among functions. The pointer is created using new and once you are done with it you can free the memory using delete.

Write a program to store Name, Age and address of student

Name age and address of students are stored in a string..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. void main()
  5. {
  6. clrscr();
  7. char name[100],age[100],address[100];
  8. cout<<"Please enter a name: ";
  9. gets(name);
  10. cout<<"Please enter age: ";
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. gets(age);
  14. cout<<"Please eneter address: ";
  15. gets(address);
  16. cout<<endl<<endl<<"Name is: "<<name<<endl<<"Age is: "<<age<<endl<<"Address is:
  17. "<<address;
  18. getch();
  19. }

Write a program to count the number of vowels in a word

A word is taken from the user and number of vowels is determined..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. char str[10];
  7. int v=0;
  8. cout<<"Please enter a string: ";
  9. cin>>str;
  10. //Coding by: Snehil Khanor
  11. //http://WapCPP.blogspot.com
  12. for(int i=0;str[i]!='\0';i++)
  13. {
  14. 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')
  15. v++;
  16. }
  17. cout<<v<<" Number of Vowels are there.";
  18. getch();
  19. }

Write a program to find transpose of a matrix

A matrix is taken from user.. and its Transpose is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. if(m!=n)
  14. {
  15. cout<<"Matrix not square so Transpose not possible :(";
  16. }
  17. else
  18. {
  19. cout<<endl<<"Enter elements of matrix: "<<endl;
  20. for(i=0;i<m;i++)
  21. {
  22. for(j=0;j<n;j++)
  23. {
  24. cout<<"Enter element a"<<i+1<<j+1<<": ";
  25. cin>>a[i][j];
  26. }
  27. }
  28. cout<<endl<<"Displaying Matrix: "<<endl<<endl;
  29. for(i=0;i<m;i++)
  30. {
  31. for(j=0;j<n;j++)
  32. {
  33. cout<<a[i][j]<<" ";
  34. }
  35. cout<<endl<<endl;
  36. }
  37. cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
  38. for(i=0;i<m;i++)
  39. {
  40. for(j=0;j<n;j++)
  41. {
  42. cout<<a[j][i]<<" ";
  43. }
  44. cout<<endl<<endl;
  45. }
  46. }
  47. getch();
  48. }

Write a program for multiplication of two matrices

Two matrices are taken from user..multiplication matrix is displayed as output.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j;
  7. cout<<"Enter number of rows of A: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns of A: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Enter number of rows of B: ";
  23. cin>>o;
  24. cout<<"Enter number of coloumns of B: ";
  25. cin>>p;
  26. cout<<endl<<"Enter elements of matrix B: "<<endl;
  27. for(i=0;i<o;i++)
  28. {
  29. for(j=0;j<p;j++)
  30. {
  31. cout<<"Enter element b"<<i+1<<j+1<<": ";
  32. cin>>b[i][j];
  33. }
  34. }

  35. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  36. for(i=0;i<m;i++)
  37. {
  38. for(j=0;j<n;j++)
  39. {
  40. cout<<a[i][j]<<" ";
  41. }
  42. cout<<endl<<endl;
  43. }
  44. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  45. for(i=0;i<o;i++)
  46. {
  47. for(j=0;j<p;j++)
  48. {
  49. cout<<b[i][j]<<" ";
  50. }
  51. cout<<endl<<endl;
  52. }
  53. if(n==o)
  54. {
  55. for(i=0;i<m;i++)
  56. {
  57. for(j=0;j<p;j++)
  58. {
  59. c[i][j]=0;
  60. for(int k=0;k<n;k++)
  61. {
  62. c[i][j]=c[i][j]+a[i][k]*b[k][j];
  63. }
  64. }
  65. }
  66. cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl;
  67. for(i=0;i<m;i++)
  68. {
  69. for(j=0;j<p;j++)
  70. {
  71. cout<<c[i][j]<<" ";
  72. }
  73. cout<<endl<<endl;
  74. }
  75. }
  76. else
  77. cout<<"Multiplication not possible :(";
  78. getch();
  79. }

Write a program to find Difference of two matrix

Two matrix are taken from user.. Substraction matrix is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Enter elements of matrix B: "<<endl;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<"Enter element b"<<i+1<<j+1<<": ";
  28. cin>>b[i][j];
  29. }
  30. }
  31. //Coding by: Snehil Khanor
  32. //http://WapCPP.blogspot.com
  33. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  34. for(i=0;i<m;i++)
  35. {
  36. for(j=0;j<n;j++)
  37. {
  38. cout<<a[i][j]<<" ";
  39. }
  40. cout<<endl<<endl;
  41. }
  42. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  43. for(i=0;i<m;i++)
  44. {
  45. for(j=0;j<n;j++)
  46. {
  47. cout<<b[i][j]<<" ";
  48. }
  49. cout<<endl<<endl;
  50. }
  51. cout<<endl<<"Matrix A - Matrix B = Matrix C: "<<endl<<endl;
  52. for(i=0;i<m;i++)
  53. {
  54. for(j=0;j<n;j++)
  55. {
  56. cout<<a[i][j]-b[i][j]<<" ";
  57. }
  58. cout<<endl<<endl;
  59. }
  60. getch();
  61. }

Write a program to find sum matrix of two matrix

Two matrix are taken from user.. Sum matrix is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. for(i=0;i<m;i++)
  13. {
  14. for(j=0;j<n;j++)
  15. {
  16. cout<<"Enter element a"<<i+1<<j+1<<": ";
  17. cin>>a[i][j];
  18. }
  19. }
  20. cout<<endl<<"Enter elements of matrix B: "<<endl;
  21. //Coding by: Snehil Khanor
  22. //http://WapCPP.blogspot.com
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<"Enter element b"<<i+1<<j+1<<": ";
  28. cin>>b[i][j];
  29. }
  30. }

  31. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  32. for(i=0;i<m;i++)
  33. {
  34. for(j=0;j<n;j++)
  35. {
  36. cout<<a[i][j]<<" ";
  37. }
  38. cout<<endl<<endl;
  39. }
  40. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  41. for(i=0;i<m;i++)
  42. {
  43. for(j=0;j<n;j++)
  44. {
  45. cout<<b[i][j]<<" ";
  46. }
  47. cout<<endl<<endl;
  48. }
  49. cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl;
  50. for(i=0;i<m;i++)
  51. {
  52. for(j=0;j<n;j++)
  53. {
  54. cout<<a[i][j]+b[i][j]<<" ";
  55. }
  56. cout<<endl<<endl;
  57. }

  58. getch();
  59. }

Write a program to store elements in m x n 2D array (Matrix)

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.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Displaying Matrix: "<<endl<<endl;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<a[i][j]<<" ";
  28. }
  29. cout<<endl<<endl;
  30. }
  31. getch();
  32. }

Write a program to Insert and Delete elemnt of an array

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..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<conio.h>
  2. #include<iostream.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[100],i,pos,num,item,size;
  7. cout<<"How many element: ";
  8. cin>>size;
  9. cout<<"Enter the elements of array: "<<endl;
  10. for(i=0;i<size;i++)
  11. cin>>a[i];
  12. cout<<endl;
  13. cout<<"Now Array is: "<<endl;
  14. for(i=0;i<size;i++)
  15. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  16. cout<<endl<<"Press any key to add another element"<<endl<<endl;
  17. getch();
  18. //Coding by: Snehil Khanor
  19. //http://WapCPP.blogspot.com
  20. cout<<"Enter another element: ";
  21. cin>>num;
  22. cout<<"Enter position number: ";
  23. cin>>pos;
  24. cout<<endl;
  25. --pos;
  26. for(i=size;i>=pos;i--)
  27. a[i+1]=a[i];
  28. a[pos]=num;
  29. size++;
  30. cout<<"New Array: "<<endl;
  31. for(i=0;i<size;i++)
  32. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  33. cout<<endl<<"Press any key to delete any element"<<endl<<endl;
  34. getch();

  35. //Coding by: Snehil Khanor
  36. //http://WapCPP.blogspot.com
  37. cout<<"Enter position number to delete: ";
  38. cin>>pos;
  39. --pos;
  40. item=a[pos];
  41. for(i=pos;i<size;i++)
  42. a[i]=a[i+1];
  43. size--;
  44. cout<<"Deleted element: "<<item<<endl;
  45. cout<<endl<<"New array: "<<endl;
  46. for(i=0;i<size;i++)
  47. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  48. getch();
  49. }

Write a program to sort elements of array (bubble Sorting)

Size of array is taken from user.. Elemnts are entered by user.. Elements are sorted in ascending order..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>

  2. #include<iostream.h>

  3. void main()

  4. {

  5. clrscr();

  6. int a[100],i,n,j,temp;

  7. cout<<"How many element: ";

  8. cin>>n;

  9. cout<<"Enter the elements of array: "<<endl;

  10. for(i=0;i<n;i++)

  11. cin>>a[i];

  12. cout<<"The elements of array Before Sorting: "<<endl;

  13. //Coding by: Snehil Khanor

  14. //http://WapCPP.blogspot.com

  15. for(i=0;i<n;i++)

  16. cout<<a[i]<<" ";

  17. cout<<endl;

  18. for(i=0;i<n;i++)

  19. {

  20. for(j=0;j<n-1-i;j++)

  21. {

  22. if(a[j]>a[j+1])

  23. {

  24. temp=a[j];

  25. a[j]=a[j+1];

  26. a[j+1]=temp;

  27. }

  28. }

  29. }

  30. cout<<"Elements of array After Sorting: "<<endl;

  31. for(i=0;i<n;i++)

  32. cout<<a[i]<<" ";

  33. getch();

  34. }

Write a program to search an element in an array (Binary searching)

Size of array is taken from user.. Elements are entered by user in order (ascending or descending).. user is asked for the element to be searched... Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>

  2. #include<iostream.h>

  3. void main()

  4. {

  5. clrscr();

  6. int a[100],i,loc,mid,beg,end,n,flag=0,item;

  7. cout<<"How many element: ";

  8. cin>>n;

  9. cout<<"Enter (IN ORDER) the elements of array: "<<endl;

  10. //Coding by: Snehil Khanor

  11. //http://WapCPP.blogspot.com

  12. for(i=0;i<n;i++)

  13. cin>>a[i];

  14. cout<<"Enter the element to be searched: ";

  15. cin>>item;

  16. loc=0;

  17. beg=0;

  18. end=n-1;

  19. while((beg<=end)&&(item!=a[mid]))

  20. {

  21. mid=(beg+end)/2;

  22. if(item==a[mid])

  23. {

  24. cout<<"Search successful :)";

  25. loc=mid;

  26. cout<<"Position of the Item: "<<loc+1;

  27. flag=flag+1;

  28. }

  29. else if(item<a[mid])

  30. end=mid-1;

  31. else

  32. beg=mid+1;

  33. }

  34. if(flag==0)

  35. {

  36. cout<<"Search NOT sucessfull :( ";

  37. }

  38. getch();

  39. }

Write a program to search an element in an array (Linear searching)

Size of array is taken from user.. Elements are entered.. Then user is asked to enter element to be searched.. Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>

  2. #include<conio.h>

  3. void main()

  4. {

  5. clrscr();

  6. int ar[10],n,num,no;

  7. cout<<"Enter size of the array: ";

  8. cin>>n;

  9. cout<<"Enter array element: "<<endl;

  10. for(int i=0;i<n;i++)

  11. {

  12. cout<<"Enter element "<<i<<": ";

  13. cin>>ar[i];

  14. }

  15. //Coding by: Snehil Khanor

  16. //http://WapCPP.blogspot.com

  17. cout<<"Elements of array: "<<endl;

  18. for(i=0;i<n;i++)

  19. cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl;

  20. //////To search////

  21. cout<<"Enter number to search: ";

  22. cin>>num;

  23. for(i=0;i<n;i++)

  24. {

  25. if(num==ar[i])

  26. {

  27. cout<<"Found at index number: "<<i;

  28. no=0;

  29. break;

  30. }

  31. else

  32. {

  33. no=1;

  34. continue;

  35. }

  36. }

  37. if(no==1)

  38. cout<<"Sorry your search returned NO results. :(";

  39. getch();

  40. }

Write a program to find minimum in array

Size of array is taken from user..Elements are entered by user..Minimum of the elements is displayed..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int ar[10],n,min;
  7. cout<<"Enter size of the array: ";
  8. cin>>n;
  9. cout<<"Enter array element: "<<endl;
  10. for(int i=0;i<n;i++)
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. {
  14. cout<<"Enter element "<<i<<": ";
  15. cin>>ar[i];
  16. }
  17. cout<<"Elements of array: "<<endl;
  18. for(i=0;i<n;i++)
  19. cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl;
  20. ////To find MIN////
  21. min=ar[0];
  22. for(i=0;i<n;i++)
  23. {
  24. if(min>ar[i])
  25. min=ar[i];
  26. }
  27. cout<<"Minimum in the array is "<<min;
  28. getch();
  29. }

Check out this stream