Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this C++ program is to output results that looks like this: The prob

ID: 3623844 • Letter: T

Question

The goal of this C++ program is to output results that looks like this:

The problem I am having is that my results have an extra line at the bottom of array that repeats the "0.880" and "0.637" which also conflicts with my sum values.  I can not for the life of me get it to not show up without screwing up another part of the results.  All I need is what I am doing wrong with what I have, I have tried multiple solutions.

The requirements for the program are to:

Write a C++ program which consists of a main function and a void function called
void myfun2(double[ ], double[ ], double &, double &, int)

The main function:
a. Opens a file called data2.txt. The file is found by right clicking here.
b. Reads all the numbers from the data file, into a single array x, using a while loop that continues until failure (use the C++ function fail()).
c. Calls the void function myfun2 and sends the array of numbers to the function and the number of numbers read from the file as arguments.
d. The function stores the cosine of each of the numbers in a second array. And the sum of the cosines and the sum of the x values in variables.
e. The second array, and the two summation variables, are passed by reference back to the main function.
f. The main function prints out the values of x, cos(x), and the sums in a well formated table with appropriate labels and headings.

This is the data table:

0.4 0.43
0.62 0.95
0.99 1.22
0.25 0.86
0.91 0.34
1.25 0.88

And this is my program:

using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
void myfun2(double[],double[],double&,double&,int);
double x[12],cosx[12],sumx,sumcosx;
int i,n,;
int main()
{
    ifstream datain("data2.txt");
    int max;
    cout<<setprecision(3)<<fixed<<showpoint;
    cout<<right<<setw(5)<<"x"<<setw(12)<<"cosx ";
    while(!datain.fail())
    {
        datain>>x[i];
        max++;
        myfun2(x,cosx,sumx,sumcosx,max);
        cout<<setw(7)<<xx[i]<<setw(10)<<cosx[i]<<endl;
    }
    cout<<"The sum of the x values = "<
    cout<<" The sum of the cosx values = "<<
system("pause");
return 0;
}
void myfun2(double x[],double cosx[],double& sumx,double& sumcosx,int max)
{
    for(n=0;n
    {
         cosx[i]=cos(x[i]);
    }
    sumx+=x[i];
    sumcosx+=cosx[i];
}

Explanation / Answer

please rate - thanks

when the input fails you process the data, and then check if it failed, max was never initialized

using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
void myfun2(double[],double[],double&,double&,int);
double x[12],cosx[12],sumx,sumcosx;
int i,n,;
int main()
{
    ifstream datain("data2.txt");
    int max=0;
    cout<<setprecision(3)<<fixed<<showpoint;
    cout<<right<<setw(5)<<"x"<<setw(12)<<"cosx ";
    datain>>x[i];
    while(!datain.fail())
    {      
        max++;
        myfun2(x,cosx,sumx,sumcosx,max);
        cout<<setw(7)<<x[i]<<setw(10)<<cosx[i]<<endl;
        datain>>x[i];
    }
    cout<<"The sum of the x values = "<<sumx<<endl;
    cout<<"The sum of the cosx values = "<<sumcosx<<endl;
system("pause");
return 0;
}
void myfun2(double x[],double cosx[],double& sumx,double& sumcosx,int max)
{
    for(n=0;n<max;n++)
    {
         cosx[i]=cos(x[i]);
    }
    sumx+=x[i];
    sumcosx+=cosx[i];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote