Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstdlib>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int size =5;
  9. double t, array[size]={5,0,3,6,1};
  10. const int sizZ=10;
  11. double a[sizZ];
  12.  
  13. cout<<"\nSo your problem is, that you have the \n array[5] \n and a[10]"<<endl;
  14. cout<<"\n array[5] has cell index 0-4 an you did define values \n and a[10] has cell index 0-9 (values not defined yet)"<<endl;
  15.  
  16. cout<<"\nLets print it - you would see it is undefined"<<endl;
  17. cout<<"\na[i]\t"<<"array[i]"<<endl;
  18. for (int i=0;i<=sizZ;i++) {
  19. cout<<a[i]<<"\t";
  20. cout<<array[i]<<"\n";
  21. }
  22.  
  23. for (int i=0;i<size;i++){
  24. for (int k=0;k<size-1;k++){
  25. if(array[k]<array[k+1]){
  26. t=array[k+1];
  27. array[k+1]=array[k];
  28. array[k]=t;
  29. }
  30. }
  31. }
  32.  
  33. cout<<"\nPart A"<<endl;
  34. for (int i=0;i<5;i++) {
  35. a[i]=array[i];
  36. cout<<a[i]<<" ";
  37. }
  38.  
  39. cout<<"\nPart B "<<endl;
  40. for (int i=0;i<size;i++){
  41. for (int k=0;k<size-1;k++){
  42. if(array[k]>array[k+1]){
  43. t=array[k+1];
  44. array[k+1]=array[k];
  45. array[k]=t;
  46. }
  47. }
  48.  
  49. }
  50.  
  51. cout<<"\nPart C"<<endl;
  52. cout<<endl;
  53.  
  54. cout<<"\n You assign to a[9]=array[9] a[8]=array[8] a[7]=array[7] a[6]=array[6] a[5]=array[5] "<<endl;
  55. cout<<"\n "<<endl;
  56. for (int i=9;i>=5;i--) {
  57. a[i]=array[i];
  58. cout<<i<<"\t"<<a[i]<<"\n";
  59. }
  60.  
  61. cout<<"\nThe conclusion is that you are accesing a non-defined table incex, and you would get random results"<<endl;
  62. cout<<"\nyou should not read outside of table";
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement