how to out put the array number; i want to print the array contain largest number?

2012-12-16 2:39 am
having a question,which request for a maximum number of cars sold(settle), the sum of car sold(settle), and the salesman that sales the most car(dunno how to do)

int ID(int sales)
{
int cars[10];
int max = cars[0];
for (int i = 1; i < 10 ; i++)
{
if (cars[i] > max)
max = cars[i];
// i want to print the i as the salesmen ID
}

return sales;
}

is it somehow to do with the jump statement?


thanks for whoever can lend a hand...if this question cannot settle, i'll just pass up this assignment with this criteria not fulfil...
更新1:

the question was having an array of numbers, which represents the number of car sold by individual salesman with the ID 0-9

更新2:

i am using c++ as coding, and the salesman ID is written in user defined function...

回答 (2)

2012-12-16 4:03 am
✔ 最佳答案
- maximum number of cars sold(settle)
- salesman that sales the most car(dunno how to do)
- sum of car sold(settle)

It looks like the code you provided is your attempt to cover some of the criteria, but there are certainly better ways to do this. I still tried to make an example as similar to your method as possible. If I could see the original question or your full code I might be able to assist you better, but here's what I got. (C++)

http://pastebin.com/embed_js.php?i=xWFkTnu5

Since you are using the indices of your array as the employee id numbers, all you have to do to "print the i as the salesmen ID" is record the index number in the same manner you recorded the "max." i.e.

if (cars[i] > max)
{
max = cars[i];
salesmanID = i;
}
2012-12-16 12:12 pm
include <stdio.h>

int main()
{
int i, max, sid, sum, cars[10];

printf("Enter the 10 numbers : ");
for(i = 0; i < 10; i++) { scanf(" %d", &cars[i]); }

max = cars[0]; sid = 0;
sum = cars[0];

for(i = 1; i < 10; i++)
{
if(cars[i] > max) { max = cars[i]; sid = i; }
sum = sum + cars[i];
}

printf("Max no. of cars sold : %d, by salesman ID : %d\n", max, sid);

printf("Total sales of car : %d\n", sum);

return 0;
}
參考: am a programmer


收錄日期: 2021-05-03 14:31:07
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20121215183913AAFYrtR

檢視 Wayback Machine 備份