Thread: c++ code

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    c++ code

    Hi can some one tell me what's wrong here its not showing the results that should be
    Code:
    for(int s=0; s<n; s++){		 		cout<< "for the custmor " << s << "his neighbors in the increasing distance order are : " << endl;
    	for(int b=1; b<n;b++){
    		for (int w=0; w<n; w++)		
    			if(s!=w)	
    				for (int i=1; i<=b ;i++){
    					if(vect[s][i]!=w){ 
    					for(int m=w+1;m<n; m++)
    						if(s!=m){
    						for (int i=1; i<=b ;i++){
    						if (vect[s][i]!=m){ 
    							if(dis[s][w]<dis[s][m]){
    								if(dis[s][w]<distance){	
    									voi=w;
    									distance=dis[s][w]; }}
    						   else if(dis[s][w]>dis[s][m]){
    								if(dis[s][m]<distance){	
    									voi=m;
    									distance=dis[s][m]; }}}}
    						vect[s][b]=voi;}}}
    		 cout << vect[s][b] <<endl;}}
    c++ code-png
    Last edited by evanice; 03-19-2017 at 04:29 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,276
    What does this do and what's wrong with the results?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    2
    it display for each custmor his neighbors in the increasing order of distance , for example here the distance between the custmor 0 and 1 is 6.32 and the distance between 0 and 2 is 3.6 , so te neighbors of the custmor 0 in the increasing order of distance are 2 then 1
    but you see here it always show that the neighbors are 2 for all the custmors what should i do ? plz some one give me an idea

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,169
    I give you one suggestion learn how to indent your code!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,615
    It is unusual to have 11 levels of nested control structures, including 6 nested loops. Whenever your code gets that complicated you should consider that you might be overcomplicating things.

    If I understand your task correctly, the following algorithm might work:
    Code:
    for each row in the distance matrix:
       copy the distances and indices to a temp array
       sort the temp array by the distances
       print the saved indices of the temp array in order
           (except where the index equals the current row index
            or, equivalently, except where the distance is 0)
    Even if you write your own sort, this shouldn't take more than 3 levels of loops. If you use std::sort it will take just 2 levels.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,664
    Too much nesting!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread