Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // U2A14
- //
- // Created by Kenny Yu on 2017-03-24.
- // Copyright © 2017 Kenny Yu. All rights reserved.
- //
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- #define ROWS 1
- #define COLUMNS 10
- int main(int argc, const char * argv[]) {
- /*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.
- Includes search and sort algorithms.
- 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.)*/
- //Initalize Arrays
- int *xPtr;
- xPtr = (int*) malloc(sizeof(int));
- xPtr[0] = 2;
- xPtr[1] = 4;
- xPtr[2] = 6;
- xPtr[3] = 8;
- xPtr[4] = 10;
- xPtr[5] = 12;
- xPtr[6] = 14;
- xPtr[7] = 16;
- cout << xPtr[0] << endl;
- int *valptr, word, l, value, size;
- size= 10;
- cout << "Type in the number of the operation you want to perform" << endl;
- cout <<"1. Search"<<endl;
- cout <<"2. Sort"<<endl;
- cout <<"3. Exit"<<endl;
- cout <<"Please Enter Option Here:" <<endl;
- cin>>word;
- switch(word){
- //List
- case 1:
- cout<< "Type in the position of the number you are looking for:";
- cin>>value;
- for ( int i = 0; i < size; i++ )
- if ( value == x[ i ] )
- return i;
- cout<<"The number coresponding to the position is:";
- return -1; // search_key not found
- cout<<endl;
- free (xPtr);
- break;
- //Search
- case 2:
- cout<<" Type in the position of the number you would like to search for:";
- int linear_search( TYPE search_key, TYPE list[], int size ) {
- for ( int i = 0; i < size; i++ )
- if ( search_key == list[ i ] )
- return i;
- return -1; // search_key not found
- }
- cout<<endl;
- free (xPtr);
- break;
- //Sort
- case 3:
- void bubble_sort(int list[], int size ) {
- int temp;
- for (int pass = 0; pass < size - 1; pass++ )
- for ( int i = 0; i < size - 1; i++ )
- if ( list[ i ] > list[ i + 1 ] ) {
- temp = list[ i ];
- list[ i ] = list[ i + 1 ];
- list[ i + 1 ] = temp;
- }
- for(int i = 0; i < 5; i++){
- cout << list[i] << endl;
- }
- }
- int main()
- {
- int number;
- int num = 5;
- number = (int) malloc(sizeof(int));
- number[0] = 1;
- number[1] = 5;
- number[2] = 3;
- number[3] = 2;
- number[4] = 4;
- for(int i = 0; i < 5; i++){
- cout << number[i] << endl;
- }
- bubble_sort(number, num);
- free (number);
- return 0;
- }
- cout<<endl;
- free (xPtr);
- exit(0);
- //Exit code
- case 4:
- int *array, newval; // declare a pointer of type int.
- cin>>newval;
- xPtr = new int[newval];
- default:
- cout <<"You have typed in a Invalid Command, Please Try again."<<endl;
- cout<<endl;
- break;
- // Otherwise code
- return 0;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement