i need to creat a function that will read information from a data file, and store the bank accounts in two seperate parrarell arrays. array “account”, and store the balance on the accounts in array “balance”.
here is an example of what i have to do
<data file>
231 4000.52
345 10000.00
541 500.00
231 100.00
541 200.00
541 1400.00

the data file should be added to the arrays which should give me the data
“account” “balance”
231 5500.52
345 10000.00
541 2100.00

Here is basic idea of what i have been trying to get to work(ps. the data file contains 3000 lines of data but their is only 100 account numbers)

``````int TemporaryID;
int TemporaryBalance;
int index;
while(indata>>temporaryID>>temporaryBalance)
{
for(int i=0 ; i < array_size ; i++)
{
indata>>temporaryID>>temporaryBalance;

index=linearSearch(account, array_size, temporaryID)
if(index<0)
{
account[i]=temporaryID;
balance[i]=temporaryBalance;
}
else
balance[index]+=temporaryBalance;
}
}//end of main

//the function linear search
int linearSearch(const int array[], size, findnumber)
{
for (int i = 0; i < size; i++)
if (array[i] == findnumber)
return i;

return -1;
}``````

I CANT GET THE PROPER INPUT FOR MY ARRAYS AND I DONT KNOW WHY... PLZ HELP

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ancient Dragon

TemporaryBalance needs to be declared as either type float or double because integers do not contain decimal values.

line 22: syntax error -- here is correction: `int linearSearch(const int array[],int size, int findnumber)` lines 11-16: If the account is not already in the array then you have to add one. You need another linear search algorithm to find the next available slot in the array, which is probably not the same as the i counter in the loop that starts on line 6. So if you initialzed the array to all 0s when the arry was declared all you have to do is find the first slot that contains a 0 value. And you don't need the loop at line 6 at all -- just use

``````while( indata>>temporaryID>>temporaryBalance )
{
index=linearSearch(account, array_size, temporaryID);
if( index < 0)
{
// set index to the next available slot in the array not shown here.
...
account[index] = temporaryID;
}
balance[index]+=temporaryBalance;
}``````

ok i made some changes and I see no reason why it shouldnt work but it still continues to store incorrect and weird numbers in my arrays.

``````[LIST=1]
[*]int LinearSearch(int a[], int array_size, int NumberToFind);

[*]int main ()
[*]{

[*]int index;
[*]int const array_size = 100;
[*]int temporaryID;
[*]float temporaryBalance;
[*]int account[100];
[*]float balance[100];
[*]Const int numbertofind=0;

[*]ifstream indata;
[*]indata.open("boxes.dat");

[*]for(int i=0; i<array_size; i++)
[*]account[i]=0;

[*]while( indata>>temporaryID>>temporaryBalance)
[*]{

[*]	cout << temporaryID<<"   "<<temporaryBalance<<endl;
[*]
[*]	index=LinearSearch(account[], array_size, temporaryID);
[*]	cout <<index<<endl;
[*]	if( index < 0)
[*]	{
[*]
[*]		index=LinearSearch(account, array_size, numbertofind);
[*]		account[index] = temporaryID;
[*]		balance[index] = temporaryBalance;
[*]	}
[*]	balance[index]+=temporaryBalance;
[*]}

[*]return 0;
[*]}

[*]int LinearSearch(const int a[], int array_size, int NumbertoFind)
[*]{
[*]for (int i=0; i<array_size; i++)
[*]	{
[*]	if (a[i] == NumbertoFind)
[*]	return i;
[*]	}
[*]
[*]	return -1;
[*]}
[/LIST]``````

lines 8 and 9: initialize the array with 0s. `int account[100] = {0};` `float balance[100] = {0.0F};` lines 13 and 14: delete (see above)

line 10: Const is misspelled should be const

delete line 24 because it will be calculated on line 26.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.