14 Replies - 468 Views - Last Post: 22 March 2013 - 03:42 PM Rate Topic: -----

#1 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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).

Thanks for your help!

Can anyone help put that into C code?
Was This Post Helpful? 0
  • +
  • -

#4 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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;
    }

Was This Post Helpful? 0
  • +
  • -

#5 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1391
  • View blog
  • Posts: 4,852
  • Joined: 19-February 09

Re: C Programming Homework

Posted 21 March 2013 - 08:43 PM

View PostTParker, 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.
Was This Post Helpful? 0
  • +
  • -

#7 TParker  Icon User is offline

  • New D.I.C Head

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

Re: C Programming Homework

Posted 21 March 2013 - 08:48 PM

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

View PostTParker, 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?
Was This Post Helpful? 0
  • +
  • -

#8 clickspiker23  Icon User is offline

  • D.I.C Head

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

Re: C Programming Homework

Posted 21 March 2013 - 09:04 PM

View PostTParker, on 21 March 2013 - 08:48 PM, said:

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

View PostTParker, 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

Was This Post Helpful? 0
  • +
  • -

#9 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 clickspiker23  Icon User is offline

  • D.I.C Head

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

Re: C Programming Homework

Posted 21 March 2013 - 09:18 PM

View PostTParker, 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,
Was This Post Helpful? 0
  • +
  • -

#11 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#12 clickspiker23  Icon User is offline

  • D.I.C Head

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

Re: C Programming Homework

Posted 21 March 2013 - 09:33 PM

View PostTParker, 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
Was This Post Helpful? 0
  • +
  • -

#13 TParker  Icon User is offline

  • New D.I.C Head

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

Re: C Programming Homework

Posted 21 March 2013 - 09:39 PM

Alright thanks.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 12,803
  • Joined: 16-October 07

Re: C Programming Homework

Posted 22 March 2013 - 03:41 AM

View PostTParker, 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

Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1