Hallo everyone,
I have written the below code but it doesn't work right.
The problem is that when I run it, and
the score_main array has a value of -3 or 3 and at the same time
the move_main is 9, I get as a "result" the number 3, which is wrong
according to this: - for (i=0; i<8; i++)
-
{
-
if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
-
{
-
result=3;
-
}
-
}
where is my mistake???@#%#@!!! :(
Here is the whole code: - #include <stdio.h>
-
#include <stdlib.h>
-
-
-
int main()
-
{
-
int score_main[8]={0,1,-2,-1,-3,1,0,1};
-
int move_main=9;
-
-
int i,result;
-
-
for (i=0; i<8; i++)
-
{
-
if (score_main[i]==3)
-
{
-
result=1;
-
}
-
}
-
-
for (i=0; i<8; i++)
-
{
-
if (score_main[i]==-3)
-
{
-
result=2;
-
}
-
-
}
-
-
for (i=0; i<8; i++)
-
{
-
if ((move_main<9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
-
{
-
result=0;
-
}
-
}
-
for (i=0; i<8; i++)
-
{
-
if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
-
{
-
result=3;
-
}
-
}
-
-
-
printf("%d\n", result);
-
-
-
-
system("PAUSE");
-
-
}
7 1499
if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
If score_main ==3,
score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main == -3
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==2
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==20
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==-60
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
That is, the expression (x != 3) || (x!= -3) is always TRUE.
If you are intending "score_main[i] not equal 3 or not equal -3", then you code:
(score_main[i]!=3) && (score_main[i]!=-3)
This expression will be TRUE only when score_main[i] is not 3 AND not -3.
If score_main ==3,
score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main == -3
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==2
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==20
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==-60
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
That is, the expression (x != 3) || (x!= -3) is always TRUE.
If you are intending "score_main[i] not equal 3 or not equal -3", then you code:
(score_main[i]!=3) && (score_main[i]!=-3)
This expression will be TRUE only when score_main[i] is not 3 AND not -3.
You are right about your thoughts!!!
Actually, this is the way I did it for the first time.
But, even if I tried it, your way (which is the right one) I still get wrong results.
What it happens, is that the result should be result=2
And that is what I get as I run the program, but unfortunatelly, it doesn't stop there.
It goes on to:
for (i=0; i<8; i++)
{
if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3))
{
result=3;
}
}
and returns result=3
WHY?????????
You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
All the four loops are used to assigned the value to variable result. This is the point.
Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
Hi again and thank you for your time!
First of all I would like to mention that I am the newbiest newbie in C programming, (I start reading about C, just 2 weeks ago) though I'm trying hard to figure everything out. Now, about my problem...
- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
I did try to do that, but it didn't work out (due to my mistaken code...of course)...therefore I wrote the above code.
Could I have a hint on how to do that? Pleaseeeeeeee....
- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
Also I would like to know how to do that too!
cube-
Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.
sicarie
cube-
Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.
sicarie
I'm sorry if I made you think that I'm looking for someone to solve my assignments, I didn't mean it.
I'm a married working woman with a child (and mature enough to respect the "laws" of universities) and I really really luck of free time.
I do my best and I want to learn as much as I can, though my whole schedual is too hard (to keep up with), I don't want to give up. I have worked to the bone with this one and I couldn't figure out what to do.
The help that I'm waiting for is sthg like "Try to use the "while" or the "else if" ect"
that's why I asked for a hint and not for the solution.
Am I asking too much???
You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
All the four loops are used to assigned the value to variable result. This is the point.
Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
You do have that. You then asked for more.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jay Moore |
last post by:
If any of you happened to read my earlier posts, I had a dilemma with
creating an efficient method of limiting access to data for my users and
subusers.
My heirarchy looks like this:
Admins...
|
by: Andrew Durdin |
last post by:
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.
I noticed that, although there are special methods for...
|
by: sam |
last post by:
Please look at the code below
#include <stdio.h>
int expr(char str, int i){
printf("%s \n",str);
return i;
}
|
by: Christopher Benson-Manica |
last post by:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one...
|
by: ThunderMusic |
last post by:
Hi,
I have a value that contains flags that I must get using a bitmask. I tryied
with the && operator, but the compiler outputs this error :
Operator '&&' cannot be applied to operands of type...
|
by: Mike |
last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8)
with X00 in it. Then I have define statements with X01 in it.
define('CERTACCESS_MEDALS', x01);
...
|
by: Protoman |
last post by:
I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's...
|
by: Jonathan |
last post by:
Hi all,
I'm having an argument with a co-worker about the difference between
the & and && operators when applied to boolean operands in C#. His
point of view is that the expression (false &...
|
by: marko |
last post by:
/* code start */
int a = 0;
/* expected evaluation and excution order with precedence in mind
/* False(3) , True(1), False(2) */
if ( (a=1) == 0 || 0 != 1 && (a =2) == 1)
putchar('T');...
|
by: Charles Law |
last post by:
This is a very basic question, but I can' turn up a statement on the
subject:
In C#, if I have
If (x == 1 && y == 2)
{
....
}
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |