Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post Posted: September 26th, 2017, 10:01 pm 
 

Joined: October 16th, 2016, 5:08 pm
Posts: 16
All you need to know is that this program is meant to ask if the coordinates x, y have to be visited.
then it is supposed to print out all of the coordinates that need to be visited. input r to skip to print.

Mainly what happens is when it prints it only prints coordinates where the y is 0.
it does some other weird stuff, when you don't finish it prints x (0-max) and y (Huge number)

Have fun

Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main()
{
    bool city[5][6] = {0};
    char buffer;
    int Cities = 0;
    unsigned int i = 0;
    unsigned int r = 0;

    char Comformation_Buffer;
    cout << "Welcome to the traveling salesman algorithm. It is to figure out the most efficient path between all \n of the diffrent places  that the salesman must visit \n" << endl;
    for(r = 0; r <= 5; r++){ if(buffer == 'r'){continue;}
        for(i = 0; i <= 6; i++)
        {
                cout << "do the coordinates " << r << ", " << i << " need to be visited by the salesman. (Y)es/(N)o \n";
                cin >> buffer;

                if(buffer == 'r'){break;} //escape route

                if(buffer == 'y' || buffer == 'Y'){city[r][i] = true; Cities++;}
                    else{city[r][i] = false;}

                if(city[r][i] == true){cout << "the coordinates " << r << ", " << i << " need to be visited by the salesman. \n";}
                    else{cout << "the coordinates " << r << ", " << i << " do not need to be visited by the salesman. \n";}

                cout << "is this correct? (Y)es/(N)o "<< endl;
                cin >> Comformation_Buffer;

                if(Comformation_Buffer == 'n' || Comformation_Buffer == 'N'){
                    if(i == 0){--r; i=6;}else{i--;}}

        }
                }

        int coor[Cities][1] = {0};
        int_fast16_t z = 0;
        cout << "The coordanates of each city are \n";

        for(r = 0; r <= 5; r++){
            for(i = 0; i <= 6; i++){
                    if(city[r][i]==true)
                        {coor[z][0]=r;
                        coor[z][1]=i;
                        z++;
                        cout << coor[z][0] << ", " << coor[z][1] << endl;}
                        }

        } system("pause");
    return 0;
}

_________________
You can't be stabbed in the back if you don't have any friends.
Why live? Cause why not.


Top 
 Profile  
Reply with quote  
 Post Posted: September 26th, 2017, 11:47 pm 
User avatar

Joined: January 19th, 2017, 10:28 pm
Posts: 509
Location: Idiot from northern Europe
This doesn't even build for me.

int coor[Cities][1] = { 0 };

cities needs to be a constant value

_________________
I just like to thumb my nose at millionaires.
If I was rolled I'd bust them heads up.


Top 
 Profile  
Reply with quote  
 Post Posted: September 27th, 2017, 12:34 am 
 

Joined: October 16th, 2016, 5:08 pm
Posts: 16
#define CITIES Cities
int coor[CITIES][1] = {0};

_________________
You can't be stabbed in the back if you don't have any friends.
Why live? Cause why not.


Top 
 Profile  
Reply with quote  
 Post Posted: September 27th, 2017, 12:59 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2908
Location: Oklahoma, United States
#define CITIES Cities
int coor[CITIES][1] = {0};

This doesn't mean anything other than replace CITIES with Cities and if Cities isn't a constant value like
#define Cities 10 or
constexpr int Cities = 10;
then the compiler will still complain that CITIES is not a constant value.

Another issue is if this declaration was to be instantiated, it would be an array of 1 element
int coor[ Cities ][ 1 ] = { 0 };

So the second line would be out of bounds
coor[ z ][ 0 ] = r;
coor[ z ][ 1 ] = i;

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: September 27th, 2017, 1:30 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2908
Location: Oklahoma, United States
Another issue would be the fact you set the city array to bool city[ 5 ][ 6 ] = { 0 };
While in your for loops you do a <= comparison which means you are indexing past the end of the arrays. An array of 5 elements goes from 0-4 {0,1,2,3,4} so it should be just
for( unsigned int r = 0; r < 5; r++ )
for( unsigned int i = 0; i < 6; i++ )

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: September 27th, 2017, 1:45 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2908
Location: Oklahoma, United States
if(buffer == 'r'){continue;}
This line seems odd to me. This is between the outer and inner for loops before you get any input from user. When you do get input from user, you immediately check for == 'r' and break out of the inner loop and when execution reaches this line you continue to the next outer loop which go back to this line and since the buffer hasn't been cleared, it will keep skipping the inner loop. Why not just break out of the outer loop as well instead of continuing if you are never going to process the inner loop again after someone presses 'r'?

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 6 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron