Hello..
Can anybody help me to find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?
Hello..
Can anybody help me to find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?
First, Welcome to the forum, Ainon! :cool:
If I asked you to record say, 10 numbers, and then find the largest and the second largest number, and what their position was in the list you wrote down, could you do it?
Of course you could.
So try it. If you need a C tutorial, click on the tab for a good one, right at the top of the forum.
You have to start it -- that's how we roll here. If you get stuck on something, post up your code, and we'll try and help out - it's pretty quick, and pretty correct - if I do say so.
But you have to get this program started.
Hints:
if arr[x]>arr[y], then arr[x]=arr[y] /// use temporary variables // it derives smaller number, its opposite makes larger number program. you may use loop.
You could use a data structure which is automatically sorted. BSD tree.h provides some macros which do this for you, but they might be a bit tricky to use at first. For example, if you define a list called IntList il and a pointer to a node IntListNode *iln which contains `index' and `integer' members, then the following would print the largest N items
Code:iln = RB_MAX(IntList, &il);
for (int i = 1; i <= N; i++) {
if (iln == NULL) {
fatal("can't find an ith largest integer (i: %d)\n", i);
}
printf("[%d]: %d\n", iln->index, iln->integer);
iln = RB_PREV(IntList, &il, iln);
}