d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Program Problem
12Next
Add Reply New Topic New Poll
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Jan 30 2016 11:59pm
Hi ^_^

http://pastebin.com/4sjQ99KN

There is a pastebin of my code.

What I am trying to do is have my print_grid that I have created print in my main function. Right now, it only prints out the number 1 and thats it. Same with my find_path function.

I'm definitely stuck with what I should do next, but I am trying to take a starting coordinate and ending coordinate from the user, and have it print out a path (Up down left right only) in a 4x4 grid.

Any tips? I am completely new to C++, this is my 3rd week :)
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 31 2016 12:07am
Quote (Rws @ Jan 31 2016 12:59am)
Hi ^_^

http://pastebin.com/4sjQ99KN

There is a pastebin of my code.

What I am trying to do is have my print_grid that I have created print in my main function. Right now, it only prints out the number 1 and thats it. Same with my find_path function.

I'm definitely stuck with what I should do next, but I am trying to take a starting coordinate and ending coordinate from the user, and have it print out a path (Up down left right only) in a 4x4 grid.

Any tips? I am completely new to C++, this is my 3rd week :)


IIRC C++, like C or C# or many other languages, requires the parenthesis after a function call so that the compiler actually knows you are trying to invoke a function.


When you are doing cout << print_grid << endl; at the end of main, I wonder if maybe you are passing it the function pointer or some shit. I don't know enough about C++ to s ay whether or not it will work as intended.

either way, for that specific problem, try to call the function with parenthesis, see if that helps at all
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Jan 31 2016 12:38am
Quote (Eep @ Jan 30 2016 10:07pm)
IIRC C++, like C or C# or many other languages, requires the parenthesis after a function call so that the compiler actually knows you are trying to invoke a function.


When you are doing cout << print_grid << endl; at the end of main, I wonder if maybe you are passing it the function pointer or some shit. I don't know enough about C++ to s ay whether or not it will work as intended.

either way, for that specific problem, try to call the function with parenthesis, see if that helps at all


Tried this suggestion, same result. Thanks for your time, any other input?

http://classes.engr.oregonstate.edu/eecs/winter2016/cs161-001/assign/assign3.pdf

That is the link to this assignment requirements... just got it a few days ago, and its due tomorrow night.

If I'm just not on the right track, please tell me... thanks to any who can help!

2nd edit... the lectures for this week were mid-term review, and very little on void function and loops.. so thats why this is really hard for me, because I do not know where to make this program work with the assignments criteria.

This post was edited by Rws on Jan 31 2016 12:51am
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Jan 31 2016 03:55am
If there is any way that someone can help me finish this assignment, I would be grateful. Thank you :hug:
Member
Posts: 2,012
Joined: Sep 23 2007
Gold: 60.00
Jan 31 2016 07:02am
What you're doing wrong, is that you're passing the whole function to cout as an argument. Instead of

Code
cout << print_grid << endl;

you should simply call the function, like this

Code
print_grid();

The void function call is simply executing the code that's included in it, without returning anything. Similarly, if you had a function like

Code
int foo() {
cout << "test" << endl;
return 0;
}

The function would not only return 0, but also write "test" to the output stream.

This post was edited by Redo on Jan 31 2016 07:04am
Member
Posts: 2,012
Joined: Sep 23 2007
Gold: 60.00
Jan 31 2016 07:08am
I could help you with this (since I have my shit to do but procrastination much rofl), but I'd like you to be online first. Contact me once you're on.

This post was edited by Redo on Jan 31 2016 07:10am
Member
Posts: 31,523
Joined: Mar 31 2004
Gold: 6.57
Jan 31 2016 01:58pm
You are going 100% in wrong direction if you can have time to sit down with me on skype and we can do Some pointers u will get it in no time

you need to use Array's I believe cause you need to know if it goes out of bounds or not... and with your current set up you don't have it running in a loop till it reaches its destination .



Edit:: You have Terrible Code layout I can help u just need hour or so of your time.

This post was edited by Katelyn on Jan 31 2016 02:01pm
Member
Posts: 31,523
Joined: Mar 31 2004
Gold: 6.57
Jan 31 2016 02:36pm


If you enter cordinates (0,0) than 3,3 does it auto move from start to finish or do you control it each turn? I have it 99% done.. Just looking to clean it up so I can help u out lemme know


if you use google hangout pm me ur emial


This post was edited by Katelyn on Jan 31 2016 02:36pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 1 2016 02:53am
Code
Output:
Found all possible paths
| 0 || 1 || 2 || 3 |
| 1 || 2 || 3 || 4 |
| 2 || 3 || 4 || 5 |
| 3 || 4 || 5 || 6 |


Found choses shortest path
| 1 || 1 || 1 || 1 |
| 0 || 0 || 0 || 1 |
| 0 || 0 || 0 || 1 |
| 0 || 0 || 0 || 0 |


Number of steps to get from coords 0, 0 to coords 2,3: 6

------------------------------------------

#include <stdio.h>
#include <stdlib.h>

#define element(width, x, y) (x)*width+(y)
#define xLength 4
#define yLength 4

void populatePaths(int *mappedPath, int level, int xStart, int yStart);
void findPath(int *mappedPath, int *chosenPath, int level, int xStart, int yStart, int xEnd, int yEnd);
void printGrid(int *mappedPath);
void initializeGrid(int *mappedPath);

int main() {
int mappedPath[xLength*yLength] = {0};
int chosenPath[xLength*yLength] = {0};
int xStart = 0;
int yStart = 0;
int xEnd = 2;
int yEnd = 3;

initializeGrid(mappedPath);

mappedPath[element(xLength, xStart, yStart)] = 0;
populatePaths(mappedPath, 0, xStart, yStart);
printf("Found all possible paths\n");
printGrid(mappedPath);

findPath(mappedPath, chosenPath, 0, xStart, yStart, xEnd, yEnd);
printf("Found choses shortest path\n");
printGrid(chosenPath);

printf("Number of steps to get from coords %d, %d to coords %d,%d: %d\n", xStart, yStart, xEnd, yEnd, mappedPath[element(xLength, xEnd, yEnd)]+1);

return 0;
}

void initializeGrid(int *mappedPath) {
int i = 0;

for(i = 0; i < (xLength * yLength); i++) {
mappedPath[i] = xLength * yLength;
}
}

void printGrid(int *mappedPath) {
int i = 0;
int j = 0;

for(i = 0; i < yLength; i++) {
for(j = 0; j < xLength; j++) {
printf("| %d |", mappedPath[element(xLength, i, j)]);
}

printf("\n");
}

printf("\n\n");
}

void findPath(int *mappedPath, int *chosenPath, int level, int xStart, int yStart, int xEnd, int yEnd) {
level = mappedPath[element(xLength, xEnd, yEnd)];
chosenPath[element(xLength, xEnd, yEnd)] = 1;


if(xEnd == xStart && yEnd == yStart) {
return;
}

if(xEnd+1 < xLength) {
if(mappedPath[element(xLength, xEnd+1, yEnd)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd+1, yEnd);
}
}

if(xEnd-1 >= 0) {
if(mappedPath[element(xLength, xEnd-1, yEnd)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd-1, yEnd);
}
}

if(yEnd+1 < yLength) {
if(mappedPath[element(xLength, xEnd, yEnd+1)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd, yEnd+1);
}
}

if(yEnd-1 >= 0) {
if(mappedPath[element(xLength, xEnd, yEnd-1)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd, yEnd-1);
}
}

return;
}

void populatePaths(int *mappedPath, int level, int xStart, int yStart) {
mappedPath[element(xLength, xStart, yStart)] = level;

if(xStart+1 < xLength) {
if(mappedPath[element(xLength, xStart+1, yStart)] > level+1) {
populatePaths(mappedPath, level+1, xStart+1, yStart);
}
}

if(xStart-1 >= 0) {
if(mappedPath[element(xLength, xStart-1, yStart)] > level+1) {
populatePaths(mappedPath, level+1, xStart-1, yStart);
}
}

if(yStart+1 < yLength) {
if(mappedPath[element(xLength, xStart, yStart+1)] > level+1) {
populatePaths(mappedPath, level+1, xStart, yStart+1);
}
}

if(yStart-1 >= 0) {
if(mappedPath[element(xLength, xStart, yStart-1)] > level+1) {
populatePaths(mappedPath, level+1, xStart, yStart-1);
}
}
}


ducks is leets

This post was edited by j0ltk0la on Feb 1 2016 02:55am
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Feb 1 2016 03:25am
Thanks for all the help, I've got this figured out now :)
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll