write a complete c++ program to do the following: the pgm will read in a set of
ID: 3568699 • Letter: W
Question
write a complete c++ program to do the following:
the pgm will read in a set of data(from a data file) in the three arrays,representing donations to three charities.the pgm will neatly print(to an output)the original data then it will store the sum of each persons donations and print that information.The pgm will find the smallest donation to each charity and its position aith in tha array.
1.Note:the data will be rad from a data file(this will be discussed separately).the main program will read in a prameter valuen.Then the main will call a function read3arrays(detalis below)to read data into three arrays which the programm will cal charity1,charity2,charity3.
2.(All the outputs will be sent to an output file) The main program will call a function print1array,sending it charity1 and k.Before the function prints,the main program will print a heading explaining what the function will print.The print1array funtion(details below)will print the first K elements of the charity1array.
The main program will also call the print1array function for arrascharity2 and charity1.
3.The main program will call a function makenewarray, sending it three arrays and k;there will also be another parameter,another array called sumdonations.The function will store values in the sumdonations array(detalis below).
When the function returns control to the main program,the main prgram will use print1array one or more time to print the values in the new subdonations array.
4.The main program will call a function findsmallestandpos, sending it one array and the size k,plus two references parameters.Using these two reference parametres, the function will find and "return" two things:the element in the array which is smallest and the position in the array of the smallest element.
the main programm will call this function four times,once for each of the array(charity1,charity2.charity3,and somedonations).Using the two actual parametres which match to the two reference parametres in the function,the main prgram will print the result of each call witha mesage,Like this (print in main,dont print in the functiom)
The sall element in array.....is..this value occurs in position...of the array.
Functional details
The function read3array will read a set of data into three arrays.The function will get four parametres,three arrays and an integer giving the size of k of these arrays.Each line of data will contain three values (for example .5,22,13)
The first data value(5) will be read into the first array;the second data value(22) will be read into the second array,and the similarly for the third data value(13).Then the function will read the second line of data containning three data values,and so on.Do this for each position in the array:0,1,1,....k-1.The function will also print the dta values as it reads them in.
The function print1array will rceieve one array,call it nums,and one integer,call it k.
It will print the elments of the nums -- -- -- -- -- --
array neatly,6 elements per line,as -- -- -- -- -- --
shown on the right(this showing K=17 -- -- -- -- --
elements in array).The numbers should align underneath each other in the columns.Each column should have several blank spaces between it and the next column.
The function makenewarray will have five parametres.Thre of them will be the three orginal arrays(the three charities),and one will ne k.The fifth parameter will be a new array called thesums.The function will store values in this new array,as follws:for each position in the new array(from 0 toK-1),the function will add the values in the other three arrays position,then store the sum in the new thesumarray.
For example ,for position 0,the function will find the sum of charity1[0],charity2[0],and charity3[0].If the position 0 of the three arrays are 10,3,and 1 ,then the sum of these three values is 14.The function will store this integer in the sums[0].It will do the same for each position in the thesums.
DATA:In data file,have 16,17 or 18 groups of 3 data values(sp the array size is atleast 16).Make sure the smallest value is not in the same position in two or more arrays;make sure that once the smallest is the first,and once it is in last.
output:Each part of the output in the output file should be clearly marked,with at lest 3-4 blank lines between parts.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int read3arrays(double c1[],double c2[],double c3[])
{
int k=0;
ifstream file;
file.open("charity.txt"); // opening file
while(!file.eof()) // reading contents from file to struct s
{
file>>c1[k]>>c2[k]>>c3[k];
k++;
}
return k;
}
void print1array(ofstream &f,double a[],int k)
{
for(int i=0;i<k;i++)
f<<a[i]<<" ";
}
void makenewarray(double c1[],double c2[],double c3[],double sum[],int k)
{
for(int i=0;i<k;i++)
{
sum[i]=c1[i]+c2[i]+c3[i];
}
}
void findsmallestandpos(double a[],int *pos,double *lowest,int k)
{
*lowest=a[0];
for(int i=1;i<k;i++)
{
if(a[i]<*lowest)
{
*lowest=a[i];
*pos=i;
}
}
}
int main()
{
ofstream f;
f.open("output.txt");
int k;
double charity1[100],charity2[100],charity3[100];
k=read3arrays(charity1,charity2,charity3);
f<<" printing details charity 1 ";
print1array(f,charity1,k);
f<<" printing details charity 2 ";
print1array(f,charity2,k);
f<<" printing details charity 3 ";
print1array(f,charity3,k);
double sumdonations[k];
makenewarray(charity1,charity2,charity3,sumdonations,k);
f<<" printing details sum donations ";
print1array(f,sumdonations,k);
int pos=0;
double lowest=0;
findsmallestandpos(charity1,&pos,&lowest,k);
f<<" lowest in charity1 is: "<<lowest<<" at postion "<<pos;
findsmallestandpos(charity2,&pos,&lowest,k);
f<<" lowest in charity1 is: "<<lowest<<" at postion "<<pos;
findsmallestandpos(charity2,&pos,&lowest,k);
f<<" lowest in charity1 is: "<<lowest<<" at postion "<<pos;
return 0;
}
charity.txt
1 4 5
3 4 5
5 6 3
1 3 5
3 4 6
1 4 6
3 5 5
4 4 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.