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

Complete Programs and Functions Write complete programs or functions as asked. N

ID: 3820104 • Letter: C

Question

Complete Programs and Functions Write complete programs or functions as asked. No comments are needed. These programs and functions will be graded for syntax, semantics, and programming style. Put your answers on the back of this sheet if there is not enough space. Write a complete program, using proper standards, to write into a file called sum.txt. The program will ask the user for positive integer numbers, calculate the sum of the positive integer numbers, and write the list of positive numbers and their sum separated with comma into the file. The user enters -1 to terminate the program. If the user enters any negative number other than -1, display an error message.

Explanation / Answer

Hello,

Please find the code for the program above:-

CODE:-

#include <fstream>
#include <iostream>
using namespace std;

int main () {
  
int intarr[10];
int size = 10;
int sumo;
readArray(intarr, size);
sumo=calcSum(intarr,size);
writedata(sumo,intarr,size);

//This function will take positive numbers and store it in array
void readArray(int intarr[], int sizen)
{

for(int i = 0; i < sizen; i++)
{
cout << "Enter positive number" + i + ":";
cin >> intarr[i];
cout << endl;
}
}

//This function will calculate the sum of all elements of array
int calcSum(int intarr[], int sizen)
{

for(int j = 0; j < sizen; j++)
{
int sum=0;
sum=sum+intarr[j];

cout<<The sum of all positive integers is<<sum<<endl;
}
}

//This function will write array data to a file and then ask user to input the -1
void writeData(int s,int intarr[],int sizen)
{
int ext;
// open a file in write mode.
ofstream outfile;
outfile.open("outputwrite.dat");
for(int j = 0; j < sizen; j++)
{
outfile<<intarr[j]
}
outfile<<",";
outfile<<s<<endl
//closing the file to be in write mode
outfile.close();
cout<<"enter -1 to exit"<<endl;
cin>>ext;
if (ext==-1)
{
return 0;
}
else
{
cout<<"error !!! Enter only -1"<<endl;
}


}

}

================================================================================

OUTPUT OF THE PROGRAM:-

  Enter positive number 1 10

Enter positive number 2 20

Enter positive number 3 30

Enter positive number 4 40

Enter positive number 5 50

Enter positive number 6 60

Enter positive number 7 70

Enter positive number 8 80

Enter positive number 9 90

Enter positive number 10 100

The sum of all positive integers is 550

Enter -1 to exit

-1

===================================================================================

Please ket me know in case clarification is required.

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