Pointer


SUBMITTED BY: Guest

DATE: Dec. 14, 2013, 5:57 p.m.

FORMAT: C++

SIZE: 1.7 kB

HITS: 7596

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int *p = new int[10000];
  6. int *q;
  7. q = p;
  8. int n; //INPUT USER
  9. int ctr;
  10. ctr = 0; //penghitung jumlah data
  11. do
  12. {
  13. cout << "\nMenu:\n1. Tambah\n2. Lihat\n3. Ambil\n4. Keluar\n";
  14. cout << "Pilihan Anda: "; cin >> n;
  15. if (n == 1) //TAMBAH
  16. {
  17. if (p != NULL)
  18. {
  19. cout << "Masukkan bilangan yang mau ditambah: ";
  20. if (ctr == 0)
  21. {
  22. cin >> p[ctr];
  23. cout << p[ctr] << " berhasil ditambah\n";
  24. }
  25. else
  26. {
  27. int tmp = p[ctr - ctr];
  28. cin >> p[ctr - ctr];
  29. p[ctr] = tmp;
  30. cout << p[ctr - ctr] << " berhasil ditambah\n";
  31. }
  32. ctr++;
  33. }
  34. }
  35. else if (n == 2) cout << "Bilangan paling depan adalah: " << p[0] << "\n"; //LIHAT
  36. else if (n == 3) //AMBIL
  37. {
  38. if (p != NULL)
  39. {
  40. for (int i = 0; i < ctr; i++)
  41. {
  42. //SWAP
  43. int t = p[i];
  44. p[i] = p[i+1];
  45. p[i+1] = t;
  46. //
  47. }
  48. ctr -= 1;
  49. }
  50. }
  51. } while (n != 4);
  52. //delete memory
  53. delete [] p;
  54. p = NULL;
  55. q = NULL;
  56. return 0;
  57. }

comments powered by Disqus