Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2005, 11:30 PM   #1
Mathsniper
Newbie
 
Join Date: May 2005
Posts: 1
Rep Power: 0 Mathsniper is on a distinguished road
dynamic union array doesn't work

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define w_max 50
#define c_max 200 

typedef union wcStruct {
	float d;
	int index;
}*wcType;

void iexchange(int *a, int *b)
{
	int t = *a;
	*a = *b;
	*b = t;
}

void fexchange(float *a, float *b)
{
	float t = *a;
	*a = *b;
	*b = t;
}

int partition(wcType *a, int p, int r)
{
	float x = a[r]->d;
	int i, j;
	j = p-1;
	for (i = p; i < r; i++) {
		if (a[i]->d <= x) {
			j++;
			fexchange(&a[i]->d, &a[j]->d);
			iexchange(&a[i]->index, &a[j]->index);
		}
	}
	j++;
	fexchange(&a[j]->d, &a[r]->d);
	iexchange(&a[j]->index, &a[r]->index);
	return j;	
}

void quick_sort(wcType *a, int p, int r)
{
	int q;
	if (p < r) {
		q = partition(a, p, r);
		quick_sort(a, p, q-1);
		quick_sort(a, p+1, r);
	}
}

void knapsack_01(int *get, int *w, int *c, int m, int n)
{
	int sum, i;
	wcType *r = (wcType*)malloc(n*sizeof(wcType));
	for (i = 0; i < n; i++)
		get[i] = 0;
	for (i = 0; i < n; i++) {
		r[i]->d = c[i]/w[i];
		r[i]->index = i;
		printf("r[i]->d = %f, r[i]->index = %d\n", r[i]->d, r[i]->index);
		sum += w[i];
	}
	if (sum >= m) {
		free(r);
		for (i = 0; i < n; i++)
			get[i] = 1;
		return;
	}
	else {
		sum = 0;
		quick_sort(r, 0, n-1);
		for (i = 0; i < n; i++) {
			sum += w[r[i]->index];
			if (sum > m) break;
			get[i] = 1;
		}
	}
	free(r);
}

void generation(FILE *pFile, int *w, int *c, int m, int n)
{
	int i;
	srand(time(NULL));
	fprintf(pFile, "%d\n%d\n", m, n);
	for (i = 0; i < n; i++) {
		w[i] = rand()%w_max+1;
		c[i] = rand()%c_max+1;
	}
	for (i = 0; i < n; i++) {
		if (i%10 == 0) 
			fprintf(pFile, "\n");
		fprintf(pFile, "%d ", w[i]);
	}
	fprintf(pFile, "\n");
	for (i = 0; i < n; i++) {
		if (i%10 == 0)
			fprintf(pFile, "\n");
		fprintf(pFile, "%d ", c[i]);
	}
}

int main()
{
	int *w, *c, *get;  
	// w = weight, c = cost, get = which items have been got(1 = got, 0 = no got)
	int n, m, i;
	char gen;
	FILE *pFile;
	pFile = fopen("knapsack.dat", "wt");
	printf("m = ");
	scanf("%d", &m);
	printf("n = ");
	scanf("%d", &n);
	
	if (pFile) {
		w = (int*)malloc(n*sizeof(int));
		c = (int*)malloc(n*sizeof(int));
		get = (int*)malloc(n*sizeof(int));
		generation(pFile, w, c, m, n);
		fclose(pFile);
	}
	else {
		printf("Can't open file.\n");
		return;
	}
	knapsack_01(w, c, get, m, n);
	for (i = 0; i < n; i++)
		printf("%d ", get[i]);
	/*
	pFile = fopen("knapsack.out", "wt");
	if (pFile) {
	knapsack_01(w, c, d, n, size);
	for (i = 0; i < size; i++)
		fprintf(pFile, "%d ", w[d[i]]);
	fclose(pFile);
	}
	*/
	return 0;
}
the function of knapsack_01, r[i]->d=c[i]/w[i] seems doesn't work. it is going to equal zero or infinite number, the program was stopped at knapsack_01 function. what's wrong?
Mathsniper is offline   Reply With Quote
Old Nov 20th, 2005, 10:53 AM   #2
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
hi. im trying to reinforce my learning of c by helping people on teh forums debug their programs. When trying to compile your program i got several errors and here is what i did to eliminate them.

On line 109 i changed the comment style to /* */ instead of //. (Was complaining about syntax before /)

on line 128 i changed the return; statement to return 1; (It appears that you wanted to return an error so you need to return something to show the program exited unsuccessful. Pretty sure that is 1 but someone more learned in C can correct me if im wrong)

on line 111 it was complaining that the variable gen was unused. So i just deleted it to get rid of the error message. So take a look at your code and see what you intended to use gen for. Im not sure.

Also starting on line 132 you have some lines commented out. Im not sure if you intended to keep them this way or was doing testing and forgot to uncomment them. So take a look at that.

Also i still get this warning.
[jeff@localhost ~]$ gcc -ansi -O2 -Wall -pedantic -o knapsack knapsacknew.c
knapsacknew.c: In function `knapsack_01':
knapsacknew.c:56: warning: 'sum' might be used uninitialized in this function

so take another look at that function. One thing i noticed is right after you declare sum you don't intialize it right away like i often see in other programs. I am not too sure what the intent of the program is but here is some output i got with my changes.

[jeff@localhost ~]$ ./knapsack
m = 1
n = 1
r[i]->d = 0.000000, r[i]->index = 0
0 [jeff@localhost ~]$

is m and n coordinates in a table? looks like you could use a new tab somewhere to format the output neater. Hope i was of some help.
linuxpimp20 is offline   Reply With Quote
Old Nov 20th, 2005, 11:11 AM   #3
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
did some interesting test to your program.

 [jeff@localhost ~]$ ./knapsack
m = -1
n = 100
Segmentation fault

[jeff@localhost ~]$ ./knapsack
m = -1
n = -1

[jeff@localhost ~]$ ./knapsack
m = .0
Segmentation fault

[jeff@localhost ~]$ ./knapsack
m = a
Segmentation fault

[jeff@localhost ~]$ ./knapsack
m = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
n = 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Segmentation fault

As you can see your program is very breakable. I don't mean to do this to discourage you. Just bored and thought it might give you and idea of what you could work on to make the code more robust. Now of course if this code is only for personal use then robustness might not matter to you because you know the intended input. Good luck.
linuxpimp20 is offline   Reply With Quote
Old Nov 20th, 2005, 7:06 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Regarding linux's comments. Shown below is where sum is being used with no known value to accumulate (unless you have a particular compiler and know its foibles).
int sum, i;
	wcType *r = (wcType*)malloc(n*sizeof(wcType));
	for (i = 0; i < n; i++)
		get[i] = 0;
	for (i = 0; i < n; i++) {
		r[i]->d = c[i]/w[i];
		r[i]->index = i;
		printf("r[i]->d = %f, r[i]->index = %d\n", r[i]->d, r[i]->index);
		sum += w[i];
I have no idea where line 109 is. If comment style is causing an error, one should resolve the problem without resorting to voodoo programming techniques.

Right on about returning the "1" (or something, anyway). If one defines a function to return a value, one should accomodate one's own wishes and return a value.

Many compilers will issue a warning if a variable (such as "gen") is declared, but not used. This is a GOOD THANG, as it's an attempt to aid the programmer. It's very good to have error and warning levels set as stringently as possible. It is then up to the coder to determine if the warning is superfluous, or whether he made a mistake.

Since integer values such as m, n are used to determine the amount of memory one wishes to borrow from the heap, one should impose constraints on their possible values.

The use of scanf is akin to function abuse. Fortunately, I have yet to hear of that being made into a hanging offense. It prolly oughtta be, before some coders kill someone with a faulty medical device. Test its return. If you don't know what its return signifies, back to the docs.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 20th, 2005, 7:29 PM   #5
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
// w = weight, c = cost, get = which items have been got(1 = got, 0 = no got)

that is line 109. Im sure that is alright to keep in there. I think i just got the warning cuz i was compiling with debugging with warning messages as verbose as i understand how to do it with gcc. changing to /* */ made the warning go away. Maybe cuz ansi doesn't like mixing c and c++ style comments?
linuxpimp20 is offline   Reply With Quote
Old Nov 20th, 2005, 7:37 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, you are 100% correct. According to my spies, strict ANSI C will barf on C++ style comments.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 20th, 2005, 8:03 PM   #7
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
I think // style comments are fine under C99, comeau compiler doesnt mind them.
Animatronic is offline   Reply With Quote
Old Nov 20th, 2005, 8:11 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Possibly I should shoot one of my spies. All these new-fangled standards are too much for me.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 20th, 2005, 8:18 PM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Heh, nobody uses C99 in my environment. I'd probably kick them if they tried though
I'd like to shoot them, but guns are prohibited here.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 21st, 2005, 3:11 AM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
If MathSniper (the OP) didn't get an answer within 6 months to his only post on this forum, I doubt he is still waiting patiently.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC