I need to read data from a file, write it to an array, and draw conclusions on that data. Here is what I have to do.
=============================================================================
The attached file name_deposit.txt contains monthly deposits of 5 accounts. Each line stores the deposits of one month for the corresponding accounts. Part of the file is shown below. The first line shows that John deposit is $42, David $68, Tom $35, Jane $1, Erin $70.
John David Tom Jane Erin
42 68 35 1 70
25 79 59 63 65
6 46 82 28 62
92 96 43 28 37
92 5 3 54 93
...
...
(The list is extremely long)
1. How many months of deposits does this file store?
2. Who has the largest account total?
3. How much is his total deposit?
============================================================================
I honestly am really stuck on this and could use some help.
I have some pseudocode but I don't know how to implement it.
=============================================================
string name[5]
int sum[5] = { 0, 0, 0, 0, 0 };
int numMonthlyDeposit = 0;
name[i] stores the name of the ith account.
sum[i] stores the total deposit for the ith account.
numMonthlyDeposit counts the number of monthly deposits
Open the file.
Read the first line in the file to get the 5 account names by
doing the following 5 times
Read the account name and save it in name[i].
Use a loop to read the lines until none is left in the file
{
Read a line of data by doing the following 5 times
{
Read a number.
If successful
add it to sum[i].
else
break out of this loop because there are no more data.
}
If the last read was successful,
add 1 to numMonthlyDeposit.
else
there are no more lines, break out of this loop.
}
string largestAccountName = name[0];
int largest = sum[0];
Use a loop to find the largest sum and its corresponding account name.
Display the following to get the answers to this problem
numMonthlyDeposit
largestAccountName
largest
ifstream read(name_deposit.txt);
int a[666][5],lines,;
void read_file(int a[][5]){
read>>lines;
for(int i=1;i<=lines;i++)
for(int j=1;j<=5;j++)
read>>a[i][j];
}
void who_is(int nr,int sum){
switch(nr){
case 1:cout<<"John "<<sum<<endl;break;
case 2:cout<<"David "<<sum<<endl;break;
case 3:cout....
//and so on till you show all the names
}
}
void max_acc(int a[][5]){
int max=0,sum=0,wh;
for(int j=1;j<=5;j++){
sum=0;
for(int i=1;i<=lines;i++)
sum+=a[i][j];
if(sum>max){
max=sum;
wh=j;
}
}
who(wh);
}
int main(){
read_file(a);
cout<<"The number of months is "<<lines<<endl;
max_acc(a);
return 0;
}
Sorry but I didn't have the patience to read your code so I made mine. I don't know if it's complicated or simple for you. If you don't understand my code then just tell me and I will make it easier. Feel free to ask anything you like. Happy coding : )