I am facing a logical error in the function filewrite() but i cant figure it out, it all seems right. The sum of the array elements is shown on the screen but when it is written in the file, it is not in the correct format.

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
void enter(double array[][5]);/*Getting input for array elements*/
void display(double array[][5]);/*Displaying Array Elements*/
void sum(double r[][5]);
void filewrite(double r[][5], double su[5]);
/*Main function starting here*/
main()
{
    double su[5];
    double r[2][5];
    enter(r);
    display(r);     
    sum(r);
    filewrite(r,su);            
    cout<<endl;
    system("pause");
}
/*Functrion for getting the input for array elements*/
void enter (double r[][5])
{
for(int i= 0;i < 1;i++)
{
 for(int j=0;j<5;j++)
    {
    cout<<endl<<"Please enter Numbers for position "<<"["<<i<<","<<j<<"] : ";
    cin>>r[i][j];
    }
}
 for (int i = 1;i<2;i++)
 {
    for(int j= 0;j<5;j++)
    {
        cout<<endl<<"Please enter numbers for position "<<"["<<i<<","<<j<<"] : ";
        cin>>r[i][j];
    }
 }
}
/*function for displaying the Array Elements*/
void display(double r[][5])
{
   for(int i = 0;i<2;i++)
    {
        for (int j=0; j<5;j++)
        {
        cout<<endl<<"The number saved in the position "<<"["<<i<<","<<j<<"] : "<<" is "<<r[i][j];

        }
    }
}
void sum(double r[][5])
{
 cout<<endl;
 double su[5] = {0}; 
 for (int i = 0; i<5;i++)
 {
        su[i] = r[0][i] + r[1][i];
        cout<<"The sum of element [0, "<<i<<"] and [1, "<<i<<"] is :"<<su[i]<<endl;
 }
}
/*Function to write the array elements in the file start here*/
void filewrite(double r[][5], double su[])
{
    ofstream re;
    re.open("2d array.txt");
    if(re.is_open())
    {
        cout<<"File Created"<<endl;
        re<<"\t\t"<<"1 Column"<<"\t"<<"2 Column"<<"\t"<<"3 Column"<<"\t"<<"4 Column"<<"\t"<<"5 Column";
        re<<"\n";
        re<<"First Row";
        for(int i = 0 ;i<5;i++)
        {
        re<<"\t"<<r[0][i]<<"\t";
        }
        re<<"\n";
        re<<"Second Row";
        for(int i=0;i<5;i++)
        {
         re<<"\t"<<r[1][i]<<"\t";
        }
        re<<"\n";
        re<<"Sum";
        for(int i=0;i<5;i++)
        {
         re<<"\t"<<su[i]<<"\t";
        }
        cout<<"First Row Written in the file"<<endl;
        cout<<"Second Row writen in the file"<<endl;
        cout<<"Sum Written in the file"<<endl;
    }
    re.close();
}

`

this program take inputs for an array having two rows and 5 columns and then add the corresponding elements in each row such as [0][1] + [1][1] and then save the result in another array with five elements. I am facing a logical error in the function filewrite() but i cant figure it out, it all seems right. The sum of the array elements is shown on the screen but when it is written in the file, it is not in the correct format. it means that the correct format is just simple integers but it shows scientific notation type numbers.

k. There are possibilities in 2 stages.

1) in calculation stage
2) in file writing satage

So first check in the calculation stage.

Now use a loop in the main function and try to print all the resultant array and put the output in your next comment.

The reason you are seeing "scientific notation type numbers" in your output is because your program is storing and manipulating double values. And double is a floating point type which can represent a greater range of values than float and uses scientific notation to represent values.

If you want your program to only use and display integer values you should change your arrays to use an integer type (unsigned int, int, long etc) and update the rest of your code accordingly (e.g. instead of taking doubles as parameters to functions, take the appropriate integer type).

Otherwise, if you want to store all of the values as doubles and output some of them as integers; when outputting the values you want displayed as ints, you can cast them to an integer type using the static_cast operator.

It's not that your program is displaying your data incorrectly, it seems more like you have chosen the incorrect type to represent your data!

Great post Jason !

In C++ when you call:

fout << value << std::endl;

the C++ compiler 'knows' what type of object 'value' is and will output what '<<' is supposed to do for that C++ type of object ... (if '<<' is defined for that object)

Note: you can overload << for your newly defined objects in C++

This article has been dead for over six months. Start a new discussion instead.