The program reads in an operation, and then numbers, and does stuff to them based on that.
Exercise number 1 at this linl
For example, ./this_file sum 1 2 3 would yield 6
I'm fairly certain I don't need all the #includes, but I had added them at some point to deal with a problem, some ideas I kept others I didn't.
I've also started reading Accelerated C++ and I realize that I should have probably tried to use things like Array<> if it's to really be C++.
/*
* Write a program that takes as its first argument one of the words ‘sum,’ ‘product,’ ‘mean,’ or ‘sqrt’ and for further arguments a series of numbers.
* The program applies the appropriate function to the series.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
// Prototypes
double sum(int argc,const char* argv[]);
double product(int argc, const char* argv[]);
double mean(int argc, const char* argv[]);
int main(int argc, const char* argv[])
{
double return_value;
if(argc == 3)
{
//This means we're in sqrt, as we don't support sqrt of > 1 number
//Nor sum, product, etc of less than one. 'cause that's dumb
double operand;
operand = strtod(argv[2], NULL );
return_value = sqrt(operand);
}
else
{
if(strcmp(argv[1],"sum") == 0)
{
return_value = sum(argc, argv);
}
if(strcmp(argv[1],"product") == 0)
{
return_value = product(argc, argv);
}
if(strcmp(argv[1],"mean") == 0)
{
return_value = mean(argc, argv);
}
}
cout << return_value << endl;
}
double sum(int argc,const char* argv[])
{
double return_value = 0.0;
for(int i = 2; i < argc; i++)
{
double x = strtod(argv[i], NULL);
return_value += x;
}
return return_value;
}
double product(int argc, const char* argv[])
{
double return_value = 1;
for(int i = 2; i < argc; i++)
{
return_value *= strtod(argv[i],NULL);
}
return return_value;
}
double mean(int argc,const char* argv[])
{
double summation = sum(argc, argv);
return summation/((double) argc-2);
}