|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
(Edit) Recursive bubble sort help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Nom nom nom..
![]()
Join Date: Jul 2007
Location: California, USA
Posts: 2,979
Rep: 254
![]() ![]() ![]() Unique Rep: 209
Trader Rating: 2
|
Edit: Look at post #9
I'm trying to write a Fibbonacci function and this is what I have? I'm trying to set argv to equal to a variable but I don't know how. Anyone? The program will compute the Fibbonaci number supplied by argv[3]. Code:
void Fibonacci(int number, char **argv)
{
int n = argv[3];
int sum;
if (n == 0 || n == 1)
{
return 1;
}
else
{
sum = Fibonacci(n-1) + Fibonacci(n-2);
return sum;
}
}
__________________
Blizzie Κατεψυγμένα Στερεά Hold on to the calm before the storm comes
Last edited by Blizzie : 05-18-09 at 04:21 AM |
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Nom nom nom..
![]()
Join Date: Jul 2007
Location: California, USA
Posts: 2,979
Rep: 254
![]() ![]() ![]() Unique Rep: 209
Trader Rating: 2
|
Ah I see, thanks. Now I have:
Code:
int Fibonacci(int number, char **argv)
{
int n = atoi(argv[3]);
int sum;
if (n == 0 || n == 1)
{
return 1;
}
else
{
sum = Fibonacci(n-1) + (Fibonacci(n-2);
return sum;
}
}
Code:
sum = Fibonacci(n-1) + (Fibonacci(n-2);
__________________
Blizzie Κατεψυγμένα Στερεά Hold on to the calm before the storm comes
|
|||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Linux Lobbyist
![]() |
edit: oops! nvm
__________________
Quote:
(417x9) (425x9)![]()
Last edited by FearMeansControl : 05-17-09 at 06:51 PM |
||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
Nom nom nom..
![]()
Join Date: Jul 2007
Location: California, USA
Posts: 2,979
Rep: 254
![]() ![]() ![]() Unique Rep: 209
Trader Rating: 2
|
Well I have this now:
Code:
int Fibonacci(int n)
{
int sum;
if (n == 0 || n == 1)
{
return 1;
}
else
{
sum = (Fibonacci(n-1) + (Fibonacci(n-2));
return sum;
}
}
Code:
int main(int argc, char **argv)
{
int number = atoi(argv[3]);
int Fib;
....
if (argv[1][1] == 'f')
{
Fib = Fibbonacci(number);
printf("Fibonacci number: %d", Fib);
....
error: syntax error before ';' token In reference to: sum = (Fibonacci(n-1) + (Fibonacci(n-2));
__________________
Blizzie Κατεψυγμένα Στερεά Hold on to the calm before the storm comes
Last edited by Blizzie : 05-17-09 at 07:03 PM |
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Nom nom nom..
![]()
Join Date: Jul 2007
Location: California, USA
Posts: 2,979
Rep: 254
![]() ![]() ![]() Unique Rep: 209
Trader Rating: 2
|
Quote:
__________________
Blizzie Κατεψυγμένα Στερεά Hold on to the calm before the storm comes
|
||||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Quick example, *didnt test it* but should work fine.
Code:
unsigned long int Fib(unsigned long int x, unsigned long int y)
{
unsigned long int z = x + y;
printf("%d + %d is %d\n", x, y, z);
Fib(y, z);
}
void main(int argc, char *argv[])
{
if(argc != 3) {
printf("%s <num1> <num2>\n", argv[0]);
return; }
Fib(atoi(argv[1]), atoi(argv[2]));
return;
}
Edit-I remember doing this with Hobo back when he was teaching me C++. Good times.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||||||
|
Nom nom nom..
![]()
Join Date: Jul 2007
Location: California, USA
Posts: 2,979
Rep: 254
![]() ![]() ![]() Unique Rep: 209
Trader Rating: 2
|
Alright. I got everything working but now I need to implement a bubble sort that's recursive. I looked up how to do a bubble sort on Google but I'm confused on how to make it recursive.
Can someone point me in the right direction or explain the algorithm a bit? Code:
void bubbleSort(int *array, int length)
{
int i, j, temp;
int test; /*use this only if unsure whether the list is already sorted or not*/
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array[j] > array[j+1]) /* compare neighboring elements */
{
temp = array[j]; /* swap array[j] and array[j+1] */
array[j] = array[j+1];
array[j+1] = temp;
test=1;
}
} /*end for j*/
if(test==0) break; /*will exit if the list is sorted!*/
} /*end for i*/
}/*end bubbleSort*/
I have a .txt file that contains lines of numbers. Do I have to create an array and feed in the numbers using fscanf in a while loop? Code:
int sortarray[100];
int arrayin;
int i = 0;
while (fscanf(fp2, "%d", &arrayin) != EOF)
{
sortarray[i] = &arrayin;
i++;
}
warning: assignment makes integer from pointer without a cast Code:
sortarray[i] = &arrayin;
__________________
Blizzie Κατεψυγμένα Στερεά Hold on to the calm before the storm comes
|
|||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Whyre you passing the address of arrayin? Thats of course where your warning(s) is.
You said you have a file containing the data, then in your example show a variable being used. Well, which is it? Read from a file, seperating by newlines or however you so choose, store this data somewhere for manipulation. The post of yours above spots about the ';', a quick look shows you left our a ')'. Lastly, if you look above I posted an example of a recursive fib function. Use the idea as needed.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|