hello, i have an integer array and i want to know wich is the bigger number in that array
example:
| 1 | 2 | 6 | 3 | 5 | 7 | -> the bigger number on the array is 7.
how do i make this??
thanks
Printable View
hello, i have an integer array and i want to know wich is the bigger number in that array
example:
| 1 | 2 | 6 | 3 | 5 | 7 | -> the bigger number on the array is 7.
how do i make this??
thanks
wow....... talk about lazy....
use a loop and a simple algorithm
>how do i make this??
How do you test two numbers and save the larger of them? Save the first number in the array as the largest, then walk across the rest of the array with the largest as one of the two numbers to test.
This is a very tough question and should be handled with much care as incorrect code could easily cause some sort of motherboard overheating.
Example:
Code:
struct array_number {
int number;
struct array_number *next;
};
int main(void) {
int array[12] = {0, 1, 2 ,3 ,4 5, 7, 8, 9, 10, 11}, i, size;
struct number_array *base, *p;
p = base = malloc(sizeof(struct number_array);
if(base == NULL)
return EXIT_FAILURE;
base->next = NULL;
for(i = 0, size = sizeof(array)/sizeof(int); i < size; i++) {
p->number = array[i];
p->next = malloc(sizeof(struct number_array));
if(p->next == NULL)
if(base->next)
for(p = base->next; p != NULL; base = p, p = p->next)
free(base);
else
free(base);
p = p->next;
}
for(p = base; p != NULL; p = p->next) {
printf("%d ", p->number);
}
if(base->next)
for(p = base->next; p != NULL; base = p, p = p->next)
free(base);
else
free(base);
puts("\nwhich number is largest?");
scanf("%d", &i);
printf("\n\nafter lengthy computation, this program found %d to be the largest number in "
"the array", i);
return EXIT_SUCCESS;
}
Was that supposed to be an example of incorrect code or just complete overkill?Quote:
Originally Posted by master5001
>Was that supposed to be an example of incorrect code or just complete overkill?
Incorrect code, of course. It's only overkill if you can get far enough to run it. ;)
Its a little of both. I figured if you played with the code, by the time you get it running you will have realized "hmm maybe I should have tried doing my simple code by myself."