My professor told me that I made my function to complicated and it could be easier. Here is what she said...
"The real problem is in howmanynegative(). You've made it way more complicated than it needs to be. It uses a lot of features which you have not yet been taught, and I told you that none of the assignments require that. Worse, it repeatedly performs tests of each value, so that it takes about 5 times as long as the simplest version can be. And worse still, there are so many combinations that it is possible to omit one. Re-think this: you can do it in three test. Hint: count."
Here is the function:
//Function that determines how many of the numbers are negative.
//Receives int first, second and third and returns "negative" which is the total
//amount of negative numbers out of the three received.
int howmanynegative(int a,int b,int c){
int negative;
//Cosimo Vilardo Assignment 3
//This program will take sets of numbers(in 3's) and call
//2 functions, one which will print out how many numbers
//are negative, and one that will find the sum and product
//of the 3 numbers. After it will count how many
//sets have been processed.
#include <iostream>
#include <fstream>
using namespace std;
void introduction();
int howmanynegative(int a,int b,int c);
int findsumandprod(int x, int y);
ofstream myfile;
int main()
{
int first, second, third, neg, num_of_sets=0;
char yes_or_no;
myfile.open("numbersOutput.txt");
introduction();//calls the introduction function
cout<<"Please enter any integer then press enter."<<endl;
do{
cin>>first;
cin>>second;
cin>>third;
myfile<<endl<<"The three original integers are "<<first<<" "
<<second<<" "<<third<<"."<<endl;
neg=howmanynegative(first, second, third);//calls the howmanynegative function
myfile<<neg<<" of these numbers are negative."<<endl;
findsumandprod(first,second);//calls the findsumandprod function
findsumandprod(second,third);//calls the findsumandprod function
findsumandprod(third,first);//calls the findsumandprod function
num_of_sets++;
cout<<"Would you like to continue?(y/n)"<<endl;
cin>>yes_or_no;
}while(yes_or_no!='n');
myfile<<endl<<num_of_sets<<" sets have been processed";
myfile.close();
system("pause");
return 0;
}
//Simple function to just print an introduction at the top of the .txt file.
//Does not receive any parameters
void introduction(){
myfile<<"Cosimo Vilardo"<<endl;
myfile<<"This program will take sets of numbers(in 3's) and call "<<endl;
myfile<<"2 functions, one which will print out how many numbers"<<endl;
myfile<<"are negative, and one that will find the sum and product"<<endl;
myfile<<"of the 3 numbers. After it will count how many "<<endl;
myfile<<"sets have been processed."<<endl<<endl;
return;
}
//Function that determines how many of the numbers are negative.
//Receives int first, second and third and returns "negative" which is the total
//amount of negative numbers out of the three received.
int howmanynegative(int a,int b,int c){
int negative;
//Function will calculate the sum and the product of the two values it receives.
//The main program calls this function 3 times and sends 2 values.
//First call- Receives: "first" and "second".
//Second call- Receives: "second" and "third".
//Third call- Receives: "third" and "first".
//This prints the sum and product from the function its self.
int findsumandprod(int x, int y){
int sum, prod;
sum=x+y;
prod=x*y;
myfile<<x<<" and "<<y<<":"<<" Sum="<<sum<<" "<<"Product="<<prod<<endl;
return;
}
You simply need to compare each integer separately as follows:
if a < 0
increment number of negatives by one
if b < 0
increment number of negatives by one
if c < 0
increment number of negatives by one
As you can see, we are using three comparisons which is what your professor preferred and is much easier to comprehend.
Try to solve the problem as you normally would if it had nothing to do with programming. Answers can come much easier if done that way.