Skip to content

Instantly share code, notes, and snippets.

@talbenari1
Created April 12, 2015 17:34
Show Gist options
  • Save talbenari1/9d4d7745ac33dbc0e351 to your computer and use it in GitHub Desktop.
Save talbenari1/9d4d7745ac33dbc0e351 to your computer and use it in GitHub Desktop.
C++ Programs

Programs

For all of these programs, never use using namespace std;. Declare each function explicitly as necessary.

  1. Write a function named insertionSort that takes a pointer to an integer and an integer as arguments. The function should sort the array passed to it using the insertion sort algorithm and print out each step of the process. Demonstrate the function in a program. Do the same for selection sort, or any other sorting algorithm that interests you.

  2. Write a function template named sum that takes two different types as arguments. The function returns the sum of the two values passed to it, using the type given by the first value. Demonstrate the template in a program with combinations of int and double.

  3. Write two functions that each take a pointer to an integer, an integer, and a string as arguments. One function, arrayToFile, should push the contents of the given array to the given filename. The other function, fileToArray, pushes the contents of the file to the array. Demonstrate the functions by copying the contents of one array to another array using the file data.txt. Make sure to check for errors.

  4. Write a function named recursivePower that takes a double and an integer as arguments. The function should recursively take the double to the power of the integer. Write another function named iterativePower that does the same thing, but iteratively. Demonstrate the functions in a program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment