GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Oct-2005, 00:47
rina rina is offline
New Member
 
Join Date: Sep 2005
Posts: 5
rina is on a distinguished road

Problem with looping.


Hi all.
I am having a problem with my loop.
I need anyone here explain to me whats going on with the code below.

Input euc_total[cluster][i]:
1.0000 0.0000 2.8284 4.2426
5.0000 4.2426 1.4142 0.0000

CPP / C++ / C Code:
void Get_Min_Distance()
{
	int clust = 0, doc = 0, doc_cluster = 0, doc_count = 0;
	double min = 0.0;

	for (int i = 0; i < ROW; i ++ )
	{
		min = euc_total[0][i];
		for (int cluster = 0; cluster < cluster_num; cluster++)
		{
			if (euc_total[cluster][i] < min)
			{	
				min = euc_total[cluster][i];
                                clust = cluster;
				printf("\nmin= %.4f cluster=%d",min,cluster);//1
                         }					
		}
        printf("\n%.4f -> Document [%d] in cluster [%d]", min, i, clust);//2
        }
}

In the function above, I have 2 printf.
The first printf will display the "min" value and "cluster" value.
The problem is, it will display cluster 1 only, and I am wondering why it doesnt display starting with cluster 0.

Output:
min= 1.4142 cluster=1
min= 0.0000 cluster=1

The second printf has no such problem. It displays starting from cluster 0.

Output:
1.0000 -> Document [0] in cluster [0]
0.0000 -> Document [1] in cluster [0]
1.4142 -> Document [2] in cluster [1]
0.0000 -> Document [3] in cluster [1]

I am thinking why the first printf cant display cluster starting with 0 while the second printf can do that??

Anyone..pls explain.
And how to solve this problem.

Thanks in advance.
Last edited by LuciWiz : 04-Oct-2005 at 00:55. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 04-Oct-2005, 04:13
cybemulatorC++ cybemulatorC++ is offline
New Member
 
Join Date: Sep 2005
Location: Born in Usa but living in Nepal coolest place in whole world...
Posts: 24
cybemulatorC++ is on a distinguished road

Re: Problem with looping.


Is this a full code or part. Many things are undeclared like ROW,euc_total etc... And didn't understand a thing.
__________________
PLEASE JOIN MY NEW FORUM
http://singerboy.999.org
  #3  
Old 04-Oct-2005, 06:37
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Problem with looping.


hi...

you assigned min as:
Quote:
min = euc_total[0][i];
so if you check
Quote:
euc_total[cluster][i] < min
that will be always false...
can you understand?
so everytime, only when the cluster goes above 1, there is a chance for the condition inside the if statement to be true.
only if the condition in the if statement is true, the value of cluster will be assigned to clust...
else the value of clust will be zero..
so there is a chance that your second printf can print the cluster to be zero..

may be you can post your full code and tell what you need.. because i dont understand your program completely...

i compiled the following the program(same as yours)
CPP / C++ / C Code:
#include<stdio.h>
int main()
{
int clust = 0, doc = 0, doc_cluster = 0, doc_count = 0;

double min = 0.0;

int cluster_num = 5 , ROW = 5;                        //just to check all the values....

double euc_total[2][4]={  { 1.0000 , 0.0000 , 2.8284 , 4.2426 },
                                   {5.0000 , 4.2426 , 1.4142 , 0.0000 } };
for (int i = 0; i < ROW; i ++ )
 { 
    min = euc_total[0][i];
    for (int cluster = 0; cluster < cluster_num; cluster++)
    {
        if (euc_total[cluster][i] < min)
        {
            min = euc_total[cluster][i];
            clust = cluster;
            printf("\n min= %.4f  cluster=%d" , min , cluster);
        }
    }
    printf("\n %.4f -> Document [%d] in cluster [%d]" ,  min ,  i ,  clust );
 }
return 0;
}

but i got the result as:
Quote:
min=0.0000 cluster=2
0.0000 -> Document [0] in cluster [2]
0.0000 -> Document [1] in cluster [2]
min=1.4142 cluster=1
min=0.0000 cluster=2
0.0000 -> Document [2] in cluster [2]
min=0.0000 cluster=1
0.0000 -> Document [3] in cluster [1]
min=0.0000 cluster=1
0.0000 -> Document [4] in cluster [1]

did you see that? the value of clust is always the same of the cluster as you assigned clust=cluster....
else the value of clust will be the same as the one in your previous loop..
you can also check your program by giving different values of euc_total...


bye...
Paramesh
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
String problem vaha C Programming Language 3 24-May-2005 18:21
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.