473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unpredictable deterministic behaviour again

a
After taking advice from previous replies, 1D and 2D arrays are allocated
by:
#define SIZE 12

smark = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
smark[i] = calloc(SIZE,sizeof(struct tagged_array));
}
omark = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
omark[i] = calloc(SIZE,sizeof(struct tagged_array));
}
sdist = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
sdist[i] = calloc(SIZE,sizeof(struct tagged_array));
}
odist = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
odist[i] = calloc(SIZE,sizeof(struct tagged_array));
}
assg_under_chair = calloc(SIZE,sizeof(int *));
for(i=0;i<SIZE;i++) {
assg_under_chair[i] = calloc(SIZE,sizeof(int));
occ[i]=0;
}

The values of the array are perfectly correct. But in the following loop,
for (k=0; k<SIZE; k++) { //for each chairman, id only, no
location implication
for (kk=0; kk<SIZE; kk++) { //moving to diff location
cost = 0;

for (j=0; j<SIZE; j++) { //objecting other chairmen in
other rows
for (jj=0; jj<SIZE; jj++) { //objecting other chairmen
in other rows
printf(" a: %d ", assg_under_chair[j][jj] ) ;
if ( assg_under_chair[j][jj] == k ) {
cost = cost + ( (odist[kk][jj].value) * (occ[k]) );
printf("3: %lf %lf %lf \n",cost ,
odist[kk][jj].value , occ[k] );
break;
}
}
}

}
}

the three %lf are all zeros. But once out of this loop, the values PRINTED
OUT immediately become normal again. Would anybody know what happens?
Nov 29 '07 #1
2 1246
a
printf(" a: %d ", assg_under_chair[j][jj] ) ;
This printing is correct
if ( assg_under_chair[j][jj] == k ) {
cost = cost + ( (odist[kk][jj].value) * (occ[k]) );
printf("3: %lf %lf %lf \n",cost ,
odist[kk][jj].value , occ[k] );
break;
}
This is not just a print out problem because the result obtained here will
be used to find out the final answer. And the final answer shows that the
values stored in these 3 variables at this instance are really all zeros.

I doubt it is again incorrect access of memory makes them temporarily
behaviour strangely but just don't know what can happen in a
nested-4-for-loop.
Nov 29 '07 #2
a
Sorry if the codes posted here are thought be dumped because several years
ago i was warned to give only necessary codes.

data:
12

0 90 10 23 43 0 0 0 0 0 0 0
90 0 0 0 0 88 0 0 0 0 0 0
10 0 0 0 0 0 26 16 0 0 0 0
23 0 0 0 0 0 0 0 0 0 0 0
43 0 0 0 0 0 0 0 0 0 0 0
0 88 0 0 0 0 0 0 1 0 0 0
0 0 26 0 0 0 0 0 0 0 0 0
0 0 16 0 0 0 0 0 0 96 0 0
0 0 0 0 0 1 0 0 0 0 29 0
0 0 0 0 0 0 0 96 0 0 0 37
0 0 0 0 0 0 0 0 29 0 0 0
0 0 0 0 0 0 0 0 0 37 0 0

0 36 54 26 59 72 9 34 79 17 46 95
36 0 73 35 90 58 30 78 35 44 79 36
54 73 0 21 10 97 58 66 69 61 54 63
26 35 21 0 93 12 46 40 37 48 68 85
59 90 10 93 0 64 5 29 76 16 5 76
72 58 97 12 64 0 96 55 38 54 0 34
9 30 58 46 5 96 0 83 35 11 56 37
34 78 66 40 29 55 83 0 44 12 15 80
79 35 69 37 76 38 35 44 0 64 39 33
17 44 61 48 16 54 11 12 64 0 70 86
46 79 54 68 5 0 56 15 39 70 0 18
95 36 63 85 76 34 37 80 33 86 18 0

program:

common.h:

struct tagged_array {
int tag;
double value;
};
#define SIZE 12
#define JUDGE 5

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <sys/param.h>

#include "common.h"

int main(int argc, char **argv) {

FILE *file; int i=0;

int *Massg; double *Mcost; //double *Msdist;

double winner=0x7FFFFF;
int winner_id=0;
int u=0;

char buf[SIZE*30]="";
struct tagged_array **smark;
struct tagged_array **omark;
struct tagged_array **sdist;
struct tagged_array **odist;
int chair_assg[JUDGE+1][SIZE];
int **assg_under_chair;
double *r;
double *util_1d;
double *score;
double occ[SIZE];
int firstline = 0;
int f = 0;

int t=0,k,kk,j,jj,cost,mid,did;

smark = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
smark[i] = calloc(SIZE,sizeof(struct tagged_array));
}
omark = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
omark[i] = calloc(SIZE,sizeof(struct tagged_array));
}
sdist = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
sdist[i] = calloc(SIZE,sizeof(struct tagged_array));
}
odist = calloc(SIZE,sizeof(struct tagged_array*));
for(i=0;i<SIZE;i++) {
odist[i] = calloc(SIZE,sizeof(struct tagged_array));
}
assg_under_chair = calloc(SIZE,sizeof(int *));
for(i=0;i<SIZE;i++) {
assg_under_chair[i] = calloc(SIZE,sizeof(int));
occ[i]=0;
}

score = malloc(SIZE * sizeof(double));
r = malloc(SIZE * SIZE * sizeof(double));
util_1d = malloc(SIZE * SIZE * sizeof(double));
Massg = malloc(SIZE * SIZE * sizeof(int));
Mcost = malloc(SIZE * SIZE * sizeof(double));
if (argc == 0) puts("usage: fn input");

file = fopen(argv[1], "r");

if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
i=0;
fscanf(file, "%lf", &k);//size
while(!feof(file) ) {

fscanf(file, "%lf", &r[i]); //printf("i:%d r[i]:%lf\n",i, r[i]);
i++;
}
}

t=0;
//finish reading/generating omark chain matrix
for (k=0; k<SIZE; k++) {
for (kk=0; kk<SIZE; kk++) {
omark[k][kk].value = smark[k][kk].value = r[k*SIZE+kk];
odist[k][kk].value = sdist[k][kk].value = r[(k+SIZE)*SIZE+kk];
omark[k][kk].tag = smark[k][kk].tag = odist[k][kk].tag = sdist[k][kk].tag
= kk;
occ[k] += omark[k][kk].value;
}

score[k] = 0.0;
chair_assg[t][k] = k;
}

u = 0;
for (k=0; k<SIZE; k++) { //for each chairman, id only, no location
implication
for (kk=0; kk<SIZE; kk++) { //moving to diff location
cost = 0;

for (j=0; j<SIZE; j++) { //objecting other chairmen in other rows
for (jj=0; jj<SIZE; jj++) { //objecting other chairmen in other rows
printf(" a: %d ", assg_under_chair[j][jj] ) ;
if ( assg_under_chair[j][jj] == k ) {
cost = cost + ( (odist[kk][jj].value) * (occ[k]) );
printf("3: %lf %lf %lf \n",cost , odist[kk][jj].value , occ[k] );
break;
}
}
}

}
}
return 0;
}
Nov 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: siggy2 | last post by:
Hi, I'm using Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 I've noticed a strange (= not deterministic) behaviour of ftplib.py: sometimes (not always) it fails (after a variable number of...
48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
5
by: rox.scott | last post by:
The following article says the .NET SOM will not allow what it calls "non-deterministic" schemas...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
14
by: Rowland Shaw | last post by:
I've got a databound combo (databound to a System.Data.DataTable), but some rather unpredicatable behaviour -- even though I have 8 rows in the source table, only the first 6 are showing up in the...
2
by: alanb | last post by:
This is driving me NUTS. I have a listbox bound to a datasource and it all works fine until I start deleting items from the class collection. The frustrating thing is that there is no pattern to...
66
by: gyan | last post by:
Hi All, I am using sprintf and getting starnge output in following case char temp_rn; memset(temp_rn,'\0',12); sprintf(temp_rn,"0%s",temp_rn); the final value in temp_rn is 00 how it...
1
by: rdspicer | last post by:
Hi I have a probelm with a simulation I am running and I was hoping someone out there may be able to shed some light on the subject. Before I start, this is what I am using: MVSC++6.0 (SP6...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.