/** * This file contains instructions and information * for lab 1 of CS588. * * Author: Travis Desell * Copyright: Travis Desell & The University of North Dakota, 2015 * * This lab introduces some looping structures, and does some * string and character manipulation, with the end goal of * being able to print out the sum of a list of numbers entered * on the command line. * * You can compile this with: * g++ -Wall 2015_lab1.cxx -o lab1 * or: * clang++ lab1.cxx -o lab1 * * There the '-o lab1' means output the executable generated by * the g++ or clang compiler as a file called 'lab1'. You can * then run the program with: * ./lab1 * From a linux/unix console. */ /** * We need to include iostream to be able * to use cout and endl. */ #include /** * The stdlib.h header file includes the 'atof' * function which we need to convert a character * array into a floating point number. */ #include /** * You need to implement this sort3 function. Make sure the * command line parameters are correct. When sort3 completes * the following should be true: * n1 >= n2 >= n3 * */ void sort3(int n1, int n2, int n3) { } /** * The main function typically takes as arguments an * integer (the number of command line arguments) and * an array of an array of characters (the actual * arguments). * * If you run this: * ./lab1 5 19 -3 * * The number of arguments will be 6, and each will be: * lab1 * 5 * 19 * -3 */ int main(int number_of_arguments, char** arguments) { /** * The purpose of lab 1 is to write a program * which sorts the three numbers passed to * the command line. * * Remember that since the first command * line argument is the program executable, it * should be skipped. */ for (int i = 0; i < number_of_arguments; i++) { //Print out each command line argument here std::cout << "arguments[" << i << "] = " << arguments[i] << std::endl; } /* * It's always good to check that the input to the * program is correct. We need to make sure that * there actually were 3 command line parameters * passed. */ if (number_of_arguments != 4) { std::cout << "ERROR: incorrect command line parameters, proper usage is: " << std::endl; std::cout << " " << arguments[0] << " " << std::endl; exit(1); //exit(1) exits the program with a return value of 1, for an error } /* * The atoi function converts a char* (an array of characters) to an * int, if it is possible. */ int n1 = atoi(arguments[1]); int n2 = atoi(arguments[2]); int n3 = atoi(arguments[3]); std::cout << "The three numbers are: " << n1 << ", " << n2 << ", " << n3 << std::endl; /* * You need to implement this sort3 function */ sort3(n1, n2, n3); std::cout << "The three sorted numbers are: " << n1 << ", " << n2 << ", " << n3 << std::endl; return 0; //return 0 for success }