madzik4113

sztangret tablice tworzy trzecia

Jan 31st, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /*Napisz program, który mając dwie tablice liczb całkowitych o dowolnych długościach utworzy trzecią
  2. zawierającą tylko te liczby, które występują w obu tablicach.*
  3.  
  4. #include "stdafx.h"
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <cstdio>
  8. #include <iostream>
  9. #include <ctime>
  10. #include <time.h>
  11. using namespace std;
  12.  
  13.  
  14.  
  15.  
  16.  
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. srand(time(NULL));
  20. int *tab1;
  21. int *tab2;
  22. int *tab3;
  23. int size1,size2;
  24. cout<<"podaj rozmiar 1 tablicy : ";
  25. cin>>size1;
  26. tab1=new int [size1];
  27. cout<<"Podaj rozmiar 2 tablicy : ";
  28. cin>>size2;
  29. tab2=new int [size2];
  30. for(int i=0;i<size1;i++)
  31. {
  32. tab1[i]=rand()%5+1;
  33. cout<<tab1[i]<<" ";
  34. }
  35. cout<<endl;
  36. for(int j=0;j<size2;j++)
  37. {
  38. tab2[j]=rand()%5+1;
  39. cout<<tab2[j]<<" ";
  40. }
  41. int licznik=0; //ile takich samych
  42. for(int i=0;i<size1;i++)
  43. {
  44. for(int j=0;j<size2;j++)
  45. {
  46. if(tab1[i]==tab2[j])
  47. {
  48. licznik++;
  49. }
  50. }
  51. }
  52. cout<<endl<<"LICZNIK WYNOSI !!! "<<licznik<<" !!!!!"<<endl<<endl;
  53. int l=0;
  54. tab3=new int [licznik];
  55. for(int i=0;i<size1;i++)
  56. {
  57. for(int j=0;j<size2;j++)
  58. {
  59. if(tab1[i]==tab2[j])
  60. {
  61. tab3[l]=tab1[i];
  62. l++;
  63. }
  64. }
  65. }
  66. for(int i=0;i<licznik;i++)
  67. {
  68. cout<<tab3[i]<<" ";
  69. }
  70.  
  71.  
  72. system("PAUSE");
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment