# C Programming Homework

Page 1 of 1

## 14 Replies - 630 Views - Last Post: 22 March 2013 - 03:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316277&amp;s=53d0ac06d54134b1712b1d07a8de87bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

# C Programming Homework

Posted 21 March 2013 - 08:01 PM

Hello!

I am a beginner in C programming. Today, my teacher (who is not very good at teaching) assigned us with some homework that I attempted/tried to attempt.

1) The first problem wants me to write a C statement that will set "a" (a variable) to 1 if at least two of the three integer variables x, y, z are true (i.e. if any two or all three are true).

-I know this involves the use of an if statement, but I am not sure how to go about it.

Here is what I formulated, although I know it is not right:

```if(x == 1 && y == 1) a = 1;
if(y == 1 && z == 1) a = 1;
if(x == 1 && z == 1) a = 1;
if(x == 1 && y == 1 && z == 1) a = 1;

```

I want them to equal 1 because I set 1 to equal "true" and any number other than 1 to equal "not true". Am I on the right track or is this totally off?

Is This A Good Question/Topic? 0

## Replies To: C Programming Homework

### #2 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: C Programming Homework

Posted 21 March 2013 - 08:08 PM

ok if i understand correctly you have 3 conditions and if all three are met then the variable int is assigned to .

i am no a c programmer but in c++ i think you would want something like

```if ((conditionOne == 1) && (conditionTwo == 1) && (conditionThree == 1))
{
int a = 1;

}
```

This post has been edited by clickspiker23: 21 March 2013 - 08:10 PM

### #3 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 08:16 PM

I think there are four conditions (x and y are true, y and z are true, x and z and true, all three variables are true).

Can anyone help put that into C code?

### #4 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: C Programming Homework

Posted 21 March 2013 - 08:26 PM

i have never coded in c and and only a little familiar with c++ but i think for the basics they are pretty much the same. if someone with more experience knows otherwise please let me and the op know because i have only taken ONE introductory c++ class. but i think this should work for c and c++ try it and let me know

by the way i assume you declared the variables earlier in the program this is basically the answer if i understand you but i do not want to give a complete program/ do it f or you, so this should be a really good start if i understand the problem. post questions if you have any i'll try and help but not doing the whole thing for you

```    if(x == 1 && y == 1)
{
a = 1;

}
else if(y == 1 && z == 1)
{
a = 1;
}
else if(x == 1 && z == 1)
{
a = 1;
}

else if(x == 1 && y == 1 && z == 1)
{
a = 1;
}
```

### #5 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 08:32 PM

The question basically said to come up with a statement assuming everything beforehand was declared and assigned, so I think that's all I need to do.

Thanks once again and I'll be sure to ask if I have any more questions!

### #6 #define

• Duke of Err

Reputation: 1615
• Posts: 5,662
• Joined: 19-February 09

## Re: C Programming Homework

Posted 21 March 2013 - 08:43 PM

TParker, on 22 March 2013 - 03:01 AM, said:

I want them to equal 1 because I set 1 to equal "true" and any number other than 1 to equal "not true". Am I on the right track or is this totally off?

Usually in C, 0 is take as false and any other value is true.

### #7 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 08:48 PM

#define, on 21 March 2013 - 08:43 PM, said:

TParker, on 22 March 2013 - 03:01 AM, said:

I want them to equal 1 because I set 1 to equal "true" and any number other than 1 to equal "not true". Am I on the right track or is this totally off?

Usually in C, 0 is take as false and any other value is true.

So how would I express that in my statement?

### #8 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: C Programming Homework

Posted 21 March 2013 - 09:04 PM

TParker, on 21 March 2013 - 08:48 PM, said:

#define, on 21 March 2013 - 08:43 PM, said:

TParker, on 22 March 2013 - 03:01 AM, said:

I want them to equal 1 because I set 1 to equal "true" and any number other than 1 to equal "not true". Am I on the right track or is this totally off?

Usually in C, 0 is take as false and any other value is true.

So how would I express that in my statement?

it would be an

```if ()
{
//  these if, else ifs are already done
}

else
{
// here is where you handle the false part, assign the variable here
}

```

think about it. if else ifs are not correct there there is only one option. in english it ould be if something do something, otherwise do this

in c/c++ it would be
if (condition) {do this} else {do this}

This post has been edited by clickspiker23: 21 March 2013 - 09:05 PM

### #9 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 09:10 PM

Wait, I thought "else if" statements are part of the "if" statements. The conditions you put in the "else if" statements are correct.

Would I just attach an else statement after the "if else" statements.

### #10 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: C Programming Homework

Posted 21 March 2013 - 09:18 PM

TParker, on 21 March 2013 - 09:10 PM, said:

/> Wait, I thought "else if" statements are part of the "if" statements. The conditions you put in the "else if" statements are correct.

Would I just attach an else statement after the "if else" statements.

yep,

### #11 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 09:29 PM

```if(x == 1 && y == 1)
{
a = 1;
}

else if(y == 1 && z == 1)
{
a = 1;
}

else if(x == 1 && z == 1)
{
a = 1;
}

else if(x == 1 && y == 1 && z == 1)
{
a = 1;
}

else
{
a = 0
}

```

Is this good?

### #12 clickspiker23

Reputation: 5
• Posts: 200
• Joined: 29-October 10

## Re: C Programming Homework

Posted 21 March 2013 - 09:33 PM

TParker, on 21 March 2013 - 09:29 PM, said:

```if(x == 1 && y == 1)
{
a = 1;
}

else if(y == 1 && z == 1)
{
a = 1;
}

else if(x == 1 && z == 1)
{
a = 1;
}

else if(x == 1 && y == 1 && z == 1)
{
a = 1;
}

else
{
a = 0
}

```

Is this good?

almost dont forget the semicoln after 0; so it would be
```a = 0;
```

otherwise i think it is correct

### #13 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: C Programming Homework

Posted 21 March 2013 - 09:39 PM

Alright thanks.

### #14 baavgai

• Dreaming Coder

Reputation: 6333
• Posts: 13,529
• Joined: 16-October 07

## Re: C Programming Homework

Posted 22 March 2013 - 03:41 AM

TParker, on 21 March 2013 - 10:01 PM, said:

at least two of the three integer variables x, y, z are true

Just want to point out that "true" is not 1. Rather, in C/C++, true is actually !0. However, you needn't worry about that, just treat the values as boolean.

So:
```if (x) { /* you have one true value */
if (y || z) { a = 1; }
} else if (y && z) { /* you get here, you have eliminated x */
a = 1;
}

```

Interestingly, the result of a boolean test IS 1 or 0. So you can do this:
```if (x) {
a = (y || z);
} else {
a = (y && z);
}

```

And, if you want to get fancy:
```a = x ? (y || z) : (y && z);

```

Hope this helps.

This post has been edited by baavgai: 22 March 2013 - 03:42 AM
Reason for edit:: me grammar bad

### #15 jjl

• Engineer

Reputation: 1120
• Posts: 4,647
• Joined: 09-June 09

## Re: C Programming Homework

Posted 22 March 2013 - 03:42 PM

The condition is pretty clear within the text, you should be able logically solve it without too much branching.

a = 1 iif (x and y) or (x and z) or (y and z)

The and statements set the requirements for having at least two variables true, the or statements cover all possible cases.

```int a = (x && y) || (x && z) || (y && z);

```

This post has been edited by jjl: 22 March 2013 - 03:44 PM