UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-12-26 07:50 PM

eleanor
Member
Registered: 2007-11-11
Posts: 23

Re: recursion

Hey, can somebody help me with this problem:

#include <iostream.h>
#include <QList>
void dijkstra(int x);
int d = 0;
int names[] = {1,2,2,2,3,4,5,6,7,8,9};
QList<int> list;
int main() {

	dijkstra(2);
	printf("Value is: %d",d);
}

void dijkstra(int x) {
	if(!list.empty())
		list.removeLast();

	for(int i=0;i<11;i++) {
		if(x == names[i]) {
			list.append(x);
		}
	}
	for(int i=0;i<list.size();i++) {
		printf("List: %d",list.at(i));
		d=d+list.at(i);
                          if(list.at(i) == 9)
                                list.at(i) = 0;
		dijkstra(list.at(i)+1);
	}
}

I want the program to store every occurance of the argument and continue counting.

So when I pass 2 to a function, it shoud all 3 accurances to list and then use recursion to count on.

It should do this:
2+3+4+5+6+7+8+9
2+3+4+5+6+7+8+9
2+3+4+5+6+7+8+9
-- since there are triple 2s in the array.

Offline

#2 2007-12-29 10:41 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: recursion

I haven't answered because I'm not really certain what you are trying to do in your code...

Let me ask this... if you names array was set to the value(s) :

{ 1,2,3,2,4,2,5,2,6,2,7,2,8,2,9 }

instead of the values you gave... and you passed a 2 into your function - what would the desired output be...


???


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB