|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: May 2005
Messaggi: 249
|
[c] ricorsione
Come trovare il minimo di un array 1D con metodo ricorsivo?
Ho provato varie strade ma credo di essere giunto a qualche cosa di troppo complesso! Deve esserci sicuramente un metodo MOLTO più facile! |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Codice:
int array_min( int* data, int length )
{
return rec_min( data[0], data+1, length -1);
}
int rec_min( int x, int* data, int length )
{
if (length == 0 )
return x;
x = min(x,data[0])
return rec_min( x , data+1, length-1);
}
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#4 | ||
|
Member
Iscritto dal: May 2005
Messaggi: 249
|
Quote:
Quote:
Pensa che in realtà si dovrebbe creare una ricerca di minimo con ricorsione e approccio divide et impera... assurdo da un punto di vista della logica della programmazione
|
||
|
|
|
|
|
#5 | |||
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
Quote:
Codice:
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define F FunzioneTotalmenteImbecille
int FunzioneTotalmenteImbecille(int *Array, int Start, int End) {
if (End == Start) {
return Array[Start];
}
return MIN(F(Array, Start, Start + (End - Start) / 2),
F(Array, Start + (End - Start) / 2 + 1, End));
}
Quote:
|
|||
|
|
|
|
|
#6 |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
ma perché non chiedi al tuo prof di farvi fare algoritmi che meritino realmente un approccio divide et impera? come ad esempio uno dei tanti algoritmi di ordinamento... -_-'
|
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
Ah, per il minimo di un array ci ho provato anche io. Ecco la "mia" soluzione: Codice:
int int_array_min (int *arr, int length)
{
int min = *arr;
if (length > 1)
min = int_array_min (arr+1, length-1);
return *arr < min ? *arr : min;
}
int main (void)
{
int a[4] = { 12, 5, 7, 20 };
int min;
min = int_array_min (a, 4);
return 0;
}
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Dec 2000
Città: bologna
Messaggi: 1309
|
Quote:
anche se sarebbe meglio impararla su un linguaccio funzionale(io ho imparato su schema, e anche io all'epoca lo odiavo...) |
|
|
|
|
|
|
#9 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
![]() L'approccio divide et impera qua non ti serve, l'array non è ordinato e devi *comunque* passare in rassegna tutti gli elementi . Se fai un paio di conti vedrai che all'incirca fai lo stesso numero di confronti. A voler essere pignoli, in questo modo accedi "a saltoni" alla memoria, col probabile risultato di rallentare l'afflusso dei dati alla cpu (soprattutto se l'array è piu' grande della cache del processore).
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
In questo caso pero' l'esempio non è dei migliori ([i]grazie[(i] anche alla scelta del C come linguaggio).
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#11 | |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
|
|
|
|
|
|
|
#12 |
|
Member
Iscritto dal: Oct 2004
Città: Bologna
Messaggi: 50
|
io risolverei così
Codice:
int
minimo (int *input, int p, int r)
{
int q; /* mediano attorno il quale dividiamo l'array */
if ((r - p) > 0)
{
q = floor ((r - p) / 2);
return MIN (minimo (input, p, p + q), minimo (input, p + q + 1, r));
}
else
return input[p];
}
Se r - p > 0 allora hai più di un elemento nell'array e hai bisogno di spezzare il problema: q è il mediano inferiore dell'array e le due parti vengono passate a due chiamate ricorsive della funzione. Se r - p == 0 allora l'array ha un solo elemento, che è banalmente il minimo dell'array. Quando le ricorsioni spezzettano i sottoproblemi fino ad arrivare ad array di un elemento, la ricorsione finisce e i numeri ritornati vengono confrontati dalla macro MIN Codice:
#define MIN(x,y) ((x) < (y) ? (x) : (y)) Questo è un semplice codice di test, se lo intitoli "min_ricorsivo.c" lo puoi compilare con "gcc -Wall -ansi -pedantic -lm -o min_ricorsivo min_ricorsivo.c" Codice:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int
minimo (int *input, int p, int r)
{
int q;
if ((r - p) > 0)
{
q = floor ((r - p) / 2);
return MIN (minimo (input, p, p + q), minimo (input, p + q + 1, r));
}
else
return input[p];
}
int
main (int argc, char **argv)
{
int i;
int *input;
input = (int *) malloc ((argc - 1) * sizeof (int));
if (input == NULL)
{
perror ("errore malloc");
exit (1);
}
if (argc == 1)
{
printf ("uso: min_ricorsivo <numeri interi>\n");
printf ("i numeri interi devono essere separati da spazi.\n");
exit (1);
}
for (i = 1; i < argc; i++)
{
input [i-1] = strtol(argv[i], (char **)NULL, 10);
printf ("%d ", input [i - 1]);
}
printf ("\n");
printf ("min = %d\n", minimo(input, 0, argc - 2));
return 0;
}
Spero vada bene, l'ora è quella che è
__________________
And the salad is frightful! I have an important message to deliver to all the cute people all over the world. If you're out there and you're cute, maybe you're beautiful. I just want to tell you something: there's more of us ugly mother-fuckers than you are, hey-y, so watch out. |
|
|
|
|
|
#13 | ||
|
Junior Member
Iscritto dal: May 2006
Messaggi: 5
|
Quote:
Quote:
(primo post: saluti a tutti!) |
||
|
|
|
|
|
#14 | |
|
Junior Member
Iscritto dal: May 2006
Messaggi: 5
|
Quote:
Codice:
(reduce #'min array) |
|
|
|
|
|
|
#15 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#16 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#17 | |
|
Junior Member
Iscritto dal: May 2006
Messaggi: 5
|
Quote:
|
|
|
|
|
|
|
#18 |
|
Member
Iscritto dal: Oct 2004
Città: Bologna
Messaggi: 50
|
A me non sembra tanto strano che la didattica insegni roba poco utile in pratica ma semplice concettualmente.
Capire un concetto nuovo come la ricorsione partendo da casi reali mi sembra controproducente. Una volta compreso il concetto grazie all'esempio banale e scolastico, si può procedere per gradi di difficoltà (come si fa sempre in ogni corso non solo informatico). Poi sarà compito dello studente digerire il concetto elementare ed applicarlo ai casi veri.
__________________
And the salad is frightful! I have an important message to deliver to all the cute people all over the world. If you're out there and you're cute, maybe you're beautiful. I just want to tell you something: there's more of us ugly mother-fuckers than you are, hey-y, so watch out. |
|
|
|
|
|
#19 | |
|
Junior Member
Iscritto dal: May 2006
Messaggi: 5
|
Quote:
|
|
|
|
|
|
|
#20 | |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:38.



















