473,503 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple C++ problem - C++ 101

Hi all,

I am tutoring introductory computer science in the Solomon Islands and
we are using C++ as the language of choice (nice). Please note that I
am no way a C++ programmer, but have but I have programmed in most
mainstream languages over the paste 20 years. We are learning HOW to
code, not just C++. The dudes in the class have their final assignment
in a couple of days and we have a simple problem to overcome. The
problem is this:

The program reads in an array of integers which instructs a robot which
direction to go in, in a 20 x 20 grid. The first two integers (read in
from a file) are the starting position of the robot, and have to be
validated in a function as a valid position within the 20 x 20 grid.
The starting postions are global variables in the file. The boolean
'valid' is declared as false also as a global. The code to validate is
as follows:

void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}

The problem is that if the initial start_x and start_y positions ARE
valid, the 'if' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we've been through in the last few weeks).

Nick

May 18 '06 #1
11 4637

ni****************@hotmail.com wrote:
Hi all,

I am tutoring introductory computer science in the Solomon Islands and
we are using C++ as the language of choice (nice). Please note that I
am no way a C++ programmer, but have but I have programmed in most
mainstream languages over the paste 20 years. We are learning HOW to
code, not just C++. The dudes in the class have their final assignment
in a couple of days and we have a simple problem to overcome. The
problem is this:

The program reads in an array of integers which instructs a robot which
direction to go in, in a 20 x 20 grid. The first two integers (read in
from a file) are the starting position of the robot, and have to be
validated in a function as a valid position within the 20 x 20 grid.
The starting postions are global variables in the file. The boolean
'valid' is declared as false also as a global. The code to validate is
as follows:

void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}

The problem is that if the initial start_x and start_y positions ARE
valid, the 'if' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we've been through in the last few weeks).

Nick


i run your code like this

//main.cpp
#include <iostream>

using namespace std;

bool valid = false;
int start_x = 2;
int start_y = 3;

int main()
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20))
{
valid = true;
cout<<"hello"<<endl;
}
return 0;
}

//output
hello

what`s wrong!

May 18 '06 #2

ni****************@hotmail.com wrote:

void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}

The problem is that if the initial start_x and start_y positions ARE
valid, the 'if' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we've been through in the last few weeks).


On the contrary, valid is set to true whatever the value of start_x and
start_y because any number is "bigger than 0 *or* less than 20", you
probably meant:
if ((start_x >= 0 && start_x <= 20) && (start_y >= 0 && start_y <=
20))
valid = true;

This would set valid to true if both start_x and start_y are in the
range [0,20]

Abdo Haji-Ali
Programmer
In|Framez

May 18 '06 #3

Abdo Haji-Ali wrote:
ni****************@hotmail.com wrote:

void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}

The problem is that if the initial start_x and start_y positions ARE
valid, the 'if' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we've been through in the last few weeks).


On the contrary, valid is set to true whatever the value of start_x and
start_y because any number is "bigger than 0 *or* less than 20", you
probably meant:
if ((start_x >= 0 && start_x <= 20) && (start_y >= 0 && start_y <=
20))
valid = true;

This would set valid to true if both start_x and start_y are in the
range [0,20]

Forgot to mention that you probably need a range of [0,19] if the size
of the grid is 20...

Abdo Haji-Ali
Programmer
In|Framez

May 18 '06 #4
Good point.

But then you could write a one-liner like

void check_valid( )
{
valid = (start_x >= 0) && (start_x < 20) && (start_y >= 0) &&
(start_y < 20);
}

Looks nicer... :o)

Henryk

May 18 '06 #5
## Abdo Haji-Ali wrote:
## > ni****************@hotmail.com wrote:
## > >
## > > void check_valid( )
## > > {
## > > if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 ||
start_y <=
## > > 20)) // you know what I mean
## > > {
## > > valid = true;
## > > }
## > > }
## > >
## >
## > On the contrary, valid is set to true whatever the value of
start_x and
## > start_y because any number is "bigger than 0 *or* less than
20", you
## > probably meant:
## > if ((start_x >= 0 && start_x <= 20) && (start_y >= 0 && start_y
<=
## > 20))
## > valid = true;
## >
## Forgot to mention that you probably need a range of [0,19] if the
size
## of the grid is 20...
##

On 18 May 2006 03:38:09 -0700, "Henryk" <he************@gmx.de> wrote:
Good point.

But then you could write a one-liner like

void check_valid( )
{
valid = (start_x >= 0) && (start_x < 20) && (start_y >= 0) &&
(start_y < 20);
}


I thnik it could be nicer if:
- you quote previous messages
- you use unsigned int instead of signed int for the values start_x
and start_y, so that the test shrinks to:

void check_valid( )
{
valid = (start_x < 20) && (start_y < 20);
}
Regards,

Zara
May 18 '06 #6
In article <g4********************************@4ax.com>,
yo****@terra.es says...

[ ... ]
- you use unsigned int instead of signed int for the values start_x
and start_y, so that the test shrinks to:

void check_valid( )
{
valid = (start_x < 20) && (start_y < 20);
}


As long as we're at it, why not use the return value:

bool isValidPos(unsigned x, unsigned y) {
return x < 20 && y < 20;
}

Of course, that has the minor problem that it spreads
magic numbers all over the place. For real code, I'd
prefer something like:

class SizeChecker() {
unsigned max_x, max_y;
public:
isValid(unsigned x, unsigned y)
: max_x(x), max_y(y)
{}

bool operator()(unsigned x, unsigned y) {
return x<max_x && y < max_y;
}
};

This makes life a lot easier when you decide to allow
their robot to roam in 200x200 grid instead of 20x20. You
create the isValid object specifying the maximum
dimensions, then use it as a function object to check the
size:

const unsigned max_x = 20, max_y = 20;

SizeChecker isValid(max_x, max_y);

if (isValid(their_x, their_y))
std::cout << "Valid starting point\n";
else
std::cout << "Invalid starting point\n";

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 18 '06 #7
Zara wrote:
[snip]
I thnik it could be nicer if:
- you quote previous messages
- you use unsigned int instead of signed int for the values start_x
and start_y, so that the test shrinks to:

void check_valid( )
{
valid = (start_x < 20) && (start_y < 20);
}


Sure. But the context of the OP does not provide enough
information to know if the values can ever meaningfully
be less than 0. For example, is the start grid simply the
"legal" start position in a larger grid? Or is that the entire
range allowed and negative numbers are meaningless?

Maybe when they are outside the valid range the robot
has to "hunt" for its home instead of doing the normal
thing it does at home. Just as an example.
Socks

May 18 '06 #8

Puppet_Sock wrote:
Zara wrote:
[snip]
I thnik it could be nicer if:
- you quote previous messages
- you use unsigned int instead of signed int for the values start_x
and start_y, so that the test shrinks to:

void check_valid( )
{
valid = (start_x < 20) && (start_y < 20);
}


Sure. But the context of the OP does not provide enough
information to know if the values can ever meaningfully
be less than 0. For example, is the start grid simply the
"legal" start position in a larger grid? Or is that the entire
range allowed and negative numbers are meaningless?

Maybe when they are outside the valid range the robot
has to "hunt" for its home instead of doing the normal
thing it does at home. Just as an example.
Socks


so, can we discuss algorithms here? I have many problems to expose...

May 19 '06 #9
Diego Martins wrote:
[snip]
so, can we discuss algorithms here? I have many problems to expose...


If the question is how to do it in standard C++ language, probably it
is on topic.

If the question is what is the algorithm for particular tasks, probably
it is off topic and likely there are better places to ask.

Check out groups.google.com and see if any group discusses the
issues you have in mind. Maybe the answer to your questions are
already posted.
Socks

May 19 '06 #10

Puppet_Sock wrote:
Diego Martins wrote:
[snip]
so, can we discuss algorithms here? I have many problems to expose...


If the question is how to do it in standard C++ language, probably it
is on topic.

If the question is what is the algorithm for particular tasks, probably
it is off topic and likely there are better places to ask.

Check out groups.google.com and see if any group discusses the
issues you have in mind. Maybe the answer to your questions are
already posted.
Socks


people who spend too much time in the Internet lose their capabilities
about irony detection

:-(

May 22 '06 #11
Diego Martins wrote:
[snip]
people who spend too much time in the Internet lose their capabilities
about irony detection


Except that this is usenet.
Socks

May 24 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6065
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at...
3
3659
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
8
6477
by: Dan | last post by:
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward?
6
2048
by: Manuel Collado | last post by:
I would like to write simple, yet well structured documents with a really simple XML DTD (or schema). Either Docbook or SDocbook are overkill for this simple case. XHTML is simpler, but...
11
2676
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
4
2084
by: Steven Blair | last post by:
I have the following number: 64521234567890 and need to apply some sort of simple encryption. Does c# have any classes for doing this. I cant use 3DES or anything as complex as. The size...
7
2265
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
24
6285
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
14
2960
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
10
2105
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.