Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // U2A14
  4. //
  5. // Created by Kenny Yu on 2017-03-24.
  6. // Copyright © 2017 Kenny Yu. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11. using namespace std;
  12.  
  13. #define ROWS 1
  14. #define COLUMNS 10
  15.  
  16. int main(int argc, const char * argv[]) {
  17. /*Uses malloc() and free() OR new and delete to manage an array in memory. The array can be increased or decreased in size as the program is running, and is free of memory leaks and other common pointer programming errors.
  18.  
  19. Includes search and sort algorithms.
  20.  
  21. Includes proper use of the const qualifier, and at least one of the more advanced pointer programming techniques (pointer arithmetic, pointers to pointers, pointers to functions, arrays of pointers, etc.)*/
  22.  
  23. //Initalize Arrays
  24.  
  25. int *xPtr;
  26.  
  27. xPtr = (int*) malloc(sizeof(int));
  28.  
  29. xPtr[0] = 2;
  30. xPtr[1] = 4;
  31. xPtr[2] = 6;
  32. xPtr[3] = 8;
  33. xPtr[4] = 10;
  34. xPtr[5] = 12;
  35. xPtr[6] = 14;
  36. xPtr[7] = 16;
  37. cout << xPtr[0] << endl;
  38.  
  39.  
  40.  
  41.  
  42. int *valptr, word, l, value, size;
  43. size= 10;
  44. cout << "Type in the number of the operation you want to perform" << endl;
  45. cout <<"1. Search"<<endl;
  46. cout <<"2. Sort"<<endl;
  47. cout <<"3. Exit"<<endl;
  48. cout <<"Please Enter Option Here:" <<endl;
  49. cin>>word;
  50. switch(word){
  51. //List
  52. case 1:
  53. cout<< "Type in the position of the number you are looking for:";
  54. cin>>value;
  55. for ( int i = 0; i < size; i++ )
  56. if ( value == x[ i ] )
  57. return i;
  58. cout<<"The number coresponding to the position is:";
  59.  
  60. return -1; // search_key not found
  61.  
  62. cout<<endl;
  63. free (xPtr);
  64. break;
  65. //Search
  66. case 2:
  67. cout<<" Type in the position of the number you would like to search for:";
  68. int linear_search( TYPE search_key, TYPE list[], int size ) {
  69.  
  70. for ( int i = 0; i < size; i++ )
  71. if ( search_key == list[ i ] )
  72. return i;
  73.  
  74. return -1; // search_key not found
  75.  
  76. }
  77. cout<<endl;
  78. free (xPtr);
  79. break;
  80. //Sort
  81. case 3:
  82.  
  83. void bubble_sort(int list[], int size ) {
  84.  
  85. int temp;
  86.  
  87. for (int pass = 0; pass < size - 1; pass++ )
  88.  
  89. for ( int i = 0; i < size - 1; i++ )
  90.  
  91. if ( list[ i ] > list[ i + 1 ] ) {
  92.  
  93. temp = list[ i ];
  94. list[ i ] = list[ i + 1 ];
  95. list[ i + 1 ] = temp;
  96.  
  97. }
  98.  
  99. for(int i = 0; i < 5; i++){
  100. cout << list[i] << endl;
  101. }
  102. }
  103. int main()
  104. {
  105. int number;
  106. int num = 5;
  107. number = (int) malloc(sizeof(int));
  108.  
  109. number[0] = 1;
  110. number[1] = 5;
  111. number[2] = 3;
  112. number[3] = 2;
  113. number[4] = 4;
  114.  
  115. for(int i = 0; i < 5; i++){
  116. cout << number[i] << endl;
  117. }
  118.  
  119. bubble_sort(number, num);
  120.  
  121.  
  122. free (number);
  123.  
  124. return 0;
  125. }
  126. cout<<endl;
  127. free (xPtr);
  128. exit(0);
  129. //Exit code
  130. case 4:
  131. int *array, newval; // declare a pointer of type int.
  132. cin>>newval;
  133.  
  134. xPtr = new int[newval];
  135. default:
  136. cout <<"You have typed in a Invalid Command, Please Try again."<<endl;
  137. cout<<endl;
  138. break;
  139. // Otherwise code
  140. return 0;
  141. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement