3

Here to get help with this. I have to write a program for Djikstra's Algorithm. Don't have to take input from the user or anything, just hard coded in. This is my first time coding anything in C. Not really all that great with it. I got my code to how it logically would work in my head. My problem is, when I run it, I get nothing. Nothing is printed out. Someone could help me figure this out along with me, that would be great. I'll keep ya updated as I am trying to find the problem, but someone a bit smarter in C than me could prob figure it out easier.

#include <stdio.h>
void main (){
        int ab = 3;//path from a to b
        int ac = 7;//path from a to c
        int ad = 9;//path from a to d
        int bc = 2;//path from b to c
        int bd = 4;//path from b to d
        int cd = 1;//path from c to d
        int a = 10;//number values for position
        int b = 20;
        int c = 30;
        int d = 40;

        int position = 10;//starting position a
        int currenttravel = 0;
        //starting at a
        //if (position == 10){
        int checker = 40;//check for when at d
        do
        {
            //check for if at a
            if (position == 10){
                //if path a to b is shortest
                if (ab < ac && ab < ad){
                    position = b;//go to b
                    printf("%d", &position);
                    currenttravel+=ab;
                }
                //or if path a to c is shortest
                else if (ac < ad){
                    position = c;//go to c
                    printf("%d", &position);
                    currenttravel+=ac;
                }
                else{
                    position = d;
                    printf("%d", &position);
                    currenttravel+=ad;
                }

            }
            if (position == 20)//at b
            {
                if (bc < bd){
                    position = c;
                    printf("%d", &position);
                    currenttravel+=bc;
                }
                else{
                    position = d;
                    printf("%d", &position);
                    currenttravel+=bd;
                }
            }
            if (position == 30){
                position = d;
                printf("%d", &position);
                currenttravel+=cd;
            }
        }
        while(position != checker);
    //  }//end if start position is a
        printf("%d", currenttravel);
        return;  //leave function

     }

I've commented as best as I can, so hopefully can follow my logic. I am probably over complicating it, but this should be one possible way of doing this.

The fixed code that works!

#include <stdio.h>
int main (){
        int ab = 3;//path from a to b
        int ac = 7;//path from a to c
        int ad = 9;//path from a to d
        int bc = 2;//path from b to c
        int bd = 4;//path from b to d
        int cd = 1;//path from c to d
        int a = 10;//number values for position
        int b = 20;
        int c = 30;
        int d = 40;

        int position = 10;//starting position a
        int currenttravel = 0;
        //starting at a
        //if (position == 10){
        int checker = 40;//check for when at d
        do
        {
        printf("starting at a \n");
            //check for if at a
            if (position == a){
                //if path a to b is shortest
                if (ab < ac && ab < ad){
                    position = b;//go to b
                    printf("b \n");
                    currenttravel+=ab;
                }
                //or if path a to c is shortest
                else if (ac < ad){
                    position = c;//go to c
                    printf("c \n");
                    currenttravel+=ac;
                }
                else{
                    position = d;
                    printf("d \n");
                    currenttravel+=ad;
                }

            }
            if (position == b)//at b
            {
                if (bc < bd){
                    position = c;
                    printf("c \n");
                    currenttravel+=bc;
                }
                else{
                    position = d;
                    printf("d \n");
                    currenttravel+=bd;
                }
            }
            if (position == c){
                position = d;
                printf("d \n");
                currenttravel+=cd;
            }
        }
        while(position != checker);
    //  }//end if start position is a
        printf("%d", currenttravel);
    //  return;  //leave function

     }

Thank you all for your help. Now I just have to convert it to Prim's Algorithm (which will be super easy as I just don't add up everything). May also play around with if different starting positions, but this is probably enough for now.

Flint Coal
  • 87
  • 2
  • 13
  • If you put a `\n` in your printfs they will show up. And don't pass a pointer e.g. `printf("%d\n", position);` – bruceg Sep 10 '16 at 01:25
  • 1
    Got that part fixed, thank you. Yeah learning pointers right now and I when I read that I was like "Yup, I did a dumb there." haha – Flint Coal Sep 10 '16 at 01:57

1 Answers1

2

Here is the output I get when compiling with gcc:

main.cpp:2:12: error: ‘::main’ must return ‘int’
 void main (){
           ^

main.cpp: In function ‘int main()’:
main.cpp:64:2: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
  return;
  ^

main.cpp: In function ‘int main()’:
main.cpp:26:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                     printf("%d", &position);
                                           ^
main.cpp:32:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                     printf("%d", &position);
                                           ^
main.cpp:37:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                     printf("%d", &position);
                                           ^
main.cpp:46:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                     printf("%d", &position);
                                           ^
main.cpp:51:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                     printf("%d", &position);
                                           ^
main.cpp:57:39: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
                 printf("%d", &position);

So first of all, it should be int main and printf("%d", position) everywhere, also you should remove the return;. The function should return an int.

Then the code executes and does print something:

2030406

you probably want newlines in between, use printf("%d\n", position). Then:

20
30
40
6

I have not yet checked the correctness of the output though.

  • This worked. Thank you so much. I modified it so now it says the order that each point is visited. A lot of my problems was trying to get Eclipse to work and finally got that issue fixed. – Flint Coal Sep 10 '16 at 01:54