Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum and Count using array, vector???

Status
Not open for further replies.

meracle

Programmer
Oct 6, 2003
8
MY
here's my condition:

int count =0;

if (percent >100){
count = 1;
value = 50;
}
else if (percent >=90 || percent <100){
count = 1;
value = 40;
}
.
.
.


I want to get the counter in the particular ramge of percentage and sum the value up...for example if i get the counter of 4 in the percentage range > 100, the result will return the count = 4, the sum value =(4*50) = 200.
The question is, how to store the value and sum up of them as well as to get the counter?? Becoz the data will loop inthe database...Using vector, or linklest, or array???
Who can show some sample codes for me??

 
// you should tell us more details about your problem
// check the following code if this suits you
class sum
{
public static void main(String args[])
{
final int VALUE50 = 0;
final int VALUE40 = 1;
final int VALUE30 = 2;
final int VALUE20 = 3;
final int VALUE10 = 4;
int dataFromDb[] = new int[1000];
int count[] = new int [5], sum[] = new int [5];

// select percent from myTable into dataFromDb
int dataCount = 6;
dataFromDb[0] = 101;
dataFromDb[1] = 92;
dataFromDb[2] = 83;
dataFromDb[3] = 70;
dataFromDb[4] = 60;
dataFromDb[5] = 55;
for (int i=0; i<dataCount; i++)
{
if (dataFromDb>=100)
{
count[VALUE50]++;
sum[VALUE50]+= 50*dataFromDb;
}
else if (dataFromDb>=90 && dataFromDb<100)
{
count[VALUE40]++;
sum[VALUE40]+= 40*dataFromDb;
}
else if (dataFromDb>=80 && dataFromDb<90)
{
count[VALUE30]++;
sum[VALUE30]+= 30*dataFromDb;
}
else if (dataFromDb>=70 && dataFromDb<80)
{
count[VALUE20]++;
sum[VALUE20]+= 20*dataFromDb;
}
else if (dataFromDb>=60 && dataFromDb<70)
{
count[VALUE10]++;
sum[VALUE10]+= 10*dataFromDb;
}
}
for (int i=0; i<sum.length; i++)
{
System.out.println(&quot;Sum&quot;+i+&quot; =&quot;+sum+&quot;, &quot;+&quot;count&quot;+i+&quot; =&quot;+count);
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top