i'm having trouble with my factors function. I need to find the factors of all positive numbers in an array and then find their sums. I'm pretty confused at this point. The last thing that I tried was to take each number in array one at a time and find the factors. I'm not getting the correct ouput. Any assistance would be greatly appreciated.

``````#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

const int MAX=30;

void get_data(ifstream&,double[],int&);
double smallest(double[],int);
double largest(double[],int);
double average(double[],int);
double variance(double[],double,int);
double deviation(double);
int factors(int,int);
int numbers(double[],int);
void print_results(double,double,double,double,double,int,double[],int);

int main()
{
double list[MAX];
int n;
int factor,num;
ifstream in;
string fname;
double small,large,ave,var,stddev;
cout << "Please enter input file name" << endl;
cin >> fname;
in.open(fname.c_str());
get_data(in,list,n);
in.close();
small = smallest(list,n);
large = largest(list,n);
ave = average(list,n);
var = variance(list,ave,n);
stddev = deviation(var);
factor = factors(num,n);
num = numbers(list,n);
print_results(small,large,ave,var,stddev,factor,list,n);

return 0;
}

void get_data(ifstream& in,double list[],int& n)
{
n=0;
in >> list[n];
while (!in.eof())
{
n++;
in >> list[n];
}
}

double smallest(double data[],int n)
{
double little = data[0];
for (int j=1; j<n; j++)
if (little > data[j])
little = data[j];
return little;
}

double largest(double data[], int n)
{
double large = data[0];
for (int j=1; j<n; j++)
{
if (data[j] > large)
large = data[j];
}
return large;
}

double average(double data[], int n)
{
double sum=0.0;
for (int j=0; j<n; j++)
sum = sum + data[j];
return sum/n;
}

double variance(double data[],double ave,int n)
{
double var=0.0;
double temp=0.0;
for (int j=0; j<n; j++)
{
temp = ave - data[j];
var = pow(temp,2.0);
}
return var/n;
}

double deviation(double var)
{
double stddev=0.0;
stddev = sqrt(var);
return stddev;
}

int numbers(double data[],int n)
{
int num;
for (int j=0; j<n; j++)
num = static_cast<int>(data[j]);
return num;
}

int factors(int num,int n)
{
int factor=2;
for (int j=2; j<n; j++)
{
if (num%j == 0)
factor++;
}
return factor;
}

void print_results(double small,double large,double ave,double var,double stddev,int factor,double list[],int n)
{
string filename;
ofstream out;
cout << "Please enter name of outuput file" << endl;
cin >> filename;
out.open(filename.c_str());
out << fixed << showpoint << setprecision(2);
out << "Smallest: " << small << endl;
out << "Largest: " << large << endl;
out << fixed << showpoint << setprecision(4);
out << "Average: " << ave << endl;
out << "Variance: " << var << endl;
out << "Standard Deviation: " << stddev << endl;
out << "Factors: " << factor << endl;
}``````
2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by caut_baia

Hi.Hope it helps ..

``````#include <iostream>
#include <vector>

using namespace std;

void factor (int);

int main (int argc,char* argv[])
{
argc=1;
factor(atoi(argv[argc]));
}

void factor (int number)
{
int div=2;
vector<int> factors;

while (div++ && div<=number)
{
if ((number%div)==0)
factors.push_back(div);
}
copy (factors.begin(),factors.end(),ostream_iterator<int>(cout,"\n"));

}
``````

Edited by Reverend Jim: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.