| View previous topic :: View next topic |
| Author |
Message |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun Oct 11, 2009 12:37 am Post subject: help on a question in C |
|
|
hi there
so i've got that question at school but i can't get it right
thought you guys might help me out
i got that function that retrieves the biggest number between 2 numbers
| Code: |
void Max(int a, int *b)
{
*b = a > *b ? a : *b;
}
|
as you noticed the function set the biggest number at the address of b instead of returning the number in the function's name
and my question is that
create a program that scan N numbers (lets say N = 5) and using the function above the program should return the second biggest number
for: 1,2,3,4,5 the output should be 4
i don't know if i need to read 2 numbers at start or just one, i'm so confused by that.. :S
i'm not asking for perfect solution, just a little bit understanding
thanks a lot :]
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Oct 11, 2009 12:51 am Post subject: |
|
|
are you not allowed to use the stuff included in <algorithm>?
edit: oh, C, durf. oops.
why are you passing a single int by reference anyway?
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun Oct 11, 2009 1:11 am Post subject: |
|
|
it's sort of pointers exercises
instead of getting the value from the function's name we use reference vars
but that function isn't the problem here i got the idea of it
the problem is how can i get the second biggest value :S
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Oct 11, 2009 3:40 am Post subject: |
|
|
Just pass a pointer to the array and the length of the array. Then do bubble sort, quick sort, whatever you prefer and you're done. So, you just kinda write a sorting function. Like this | Code: | #include <stdlib.h>
#include <time.h>
void qsort(int *arr, int len);
int main(int argc, char *argv[])
{
int *x = malloc( 100 );
srand( time(NULL) );
for (int i=0; i<100; ++i)
*x = rand();
sort(x, 100);
return 0;
}
void qsort(int *arr, int len)
{
// I don't remember how qsort goes.. You choose pivot, start comparing and use recursion or something.
} | Btw, there's a great video on YouTube about those algorithms.. Too lazy to find it, but if you're interested, I suggest taking a look at it. Also, C stdlib does already provide quick sort, but if you were to show the algorithm...
EDIT: wait.. You didn't want to sort the whole array. So, just find the biggest and then find the second biggest. | Code: | int secondbiggest(int *arr, int len)
{
// Note, only positive stuff, if you need negative too, change this.
int b = 0, b2 = 0;
for (int i=0; i<len; ++i) {
if (b < arr[i]) {
b2 = b;
b = arr[i];
} else if (b2 < arr[i])
b2 = arr[i];
}
return b2;
} | This could be optimized a bit, but that's your job :P
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun Oct 11, 2009 4:29 am Post subject: |
|
|
yea i've already thought about that guys
the point is that i can't / don't even need to use sorting and arrays
i have to use the above function that compares two numbers and retrieve the biggest
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Oct 11, 2009 5:11 am Post subject: |
|
|
| 1qaz wrote: | | i have to use the above function that compares two numbers and retrieve the biggest | So, now you only need the biggest? Just go whole thru the whole array using the comp and retrieve the biggest.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Tue Oct 13, 2009 11:52 am Post subject: |
|
|
well.. i've done it using array and searching for the biggest value using Max(a,*b)
right after that searching the second biggest value so thanks i guess :]
btw if you can help me with 1 more thing..
i'v been passing all over google trying to get a list of an assembly instructions that are being used in protected mode (not real mode) like: GDTR, LDTR and so on..
but i can't find anything related
i even tried searching for the intel's manual but i got messed up :S
so if one of u guys can help me that'd be great, thanks :]
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Oct 13, 2009 1:48 pm Post subject: |
|
|
| You can use 2 variables - max and max2, if the input number, x, is bigger than max than max2 = max and max = x. if x > max2 and x < max then max2 = x. else, do nothing.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Oct 14, 2009 12:41 am Post subject: |
|
|
| Deltron Z wrote: | | You can use 2 variables - max and max2, if the input number, x, is bigger than max than max2 = max and max = x. if x > max2 and x < max then max2 = x. else, do nothing. | So, you just read aloud the piece of code I wrote earlier? :P
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Oct 14, 2009 6:43 am Post subject: |
|
|
| Jani wrote: | | Deltron Z wrote: | | You can use 2 variables - max and max2, if the input number, x, is bigger than max than max2 = max and max = x. if x > max2 and x < max then max2 = x. else, do nothing. | So, you just read aloud the piece of code I wrote earlier?  |
Not really, I just saw both of you suggest sorting the array, I suggested another idea, I didn't even see your post, I just saw your code containing a sorting function.
|
|
| Back to top |
|
 |
|