Lesson 9: Two-sided and Multi-sided Selections


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



This lesson is examples only! Think you will specially love this one since it will bring you much knowledge in spite of small example blocks length. My recommendation is first to try and understand these executions, then try reading the task again and try solving it on your own… you will learn faster in this way. On the other hand, if you’re lazy but still want to learn, just read it twice or three times.



OPERATORS PRIORITY

What’s the value of variable d after executing following program block:


short int a=4, b=2, c=8, d;
d = a < 10 && 2 * b < c;

Result:

d = ( a < 10 ) && ( ( 2 * b ) < c )
d = 1 && (4<8)
d = 1 && 1
d = 1

What will be printed on your screen after executing this block?

int a = 5, b = -1, c = 0;
c = (a = c && b) ? a = b: ++c;
printf("a = %d, b = %d, c = %d: \n", a, b, c);

Result:

a = 0 , b = -1 , c = 1



TWOSIDED SELECTION


Example:

Write your own program that calculates and prints absolute value of given number:




#include<stdio.h>

void main(){

int number,abs;

printf("Input number: ");
scanf("%d",&number); /*can also be written: */
if (number < 0) { /*if (number < 0) */

abs = -number; /* abs = -number; */

}

else { /*else */

abs = number; /* abs = number; */

}

printf("Absolute value of given number %d is:
%d\n", number, abs);
}







Example:

Program calculates circle’s area with radius given from user:



#include <stdio.h>
#define PI 3.141592

void main(){

float radius, area;

printf("Input circle’s radius: ");
scanf("%f",&radius);
if (radius <= 0) {

printf("You have entered false radius!\n");
}
else {

area=radius*radius*PI;
printf("Circle’s area is: %f\n", area);
}
}




What will be printed if someone enters -10 in "Input circle’s radius:"


You have entered false radius!
Circle’s area is: x.xxxxx (depending on content of memory locations assigned to variable area)

  1. How would you modify upper program so that calculation-result message appears only if the program really calculated area of the circle?

  2. How would you avoid unexpected-unknown result?



1.
Execution printf("Circle’s area is: %f\n", area); would be written under else part:



if (radius <= 0) {

printf("You have entered false radius!\n");
}

else {

area=radius*radius*PI;
printf("Circle’s area is: %f\n", area);
}



2.
I would initialize variable area.

float radius, area = 0;



Example:

Write your own program which reads two given numbers, tests if the first number is dividable with second one (with no remain left); and prints suitable message. Second number must be different than 0 (why?).



#include <stdio.h>

void main(){

int a,b;

printf("Give a and b:");
scanf("%d %d", &a, &b);
if (b != 0) {

if (a % b == 0) {

printf("%d is dividable with %d\n", a, b);

}
else {

printf("%d isn’t dividable with %d\n", a, b);
}
}
}






Alternative approach:


if (b != 0) {

printf("%d %s dividable with %d\n",
a, a % b ? "isn’t" : "is", b);
}





MULTISIDED SELECTION


Example:

Oven is capable of producing working temperature in interval [50°C-80°C]. Write your own program that tests if the oven temperature given through keyboard is in previously mentioned interval; and prints suitable message.


#inclde <stdio.h>
#define lower 50.0
#define upper 80.0
void main(){

float temp;

printf("\n Give oven temperature: ");
scanf("%f",&temp);
if (temp < lower) {
printf("Temperature has fallen below interval!\n");
} else if (temp > upper) {
printf("Temperature has risen above interval!\n");
} else {
printf("Temperature of oven is OK\n");
}
}








10 Responses to “Lesson 9: Two-sided and Multi-sided Selections”

  1. Anonymous vurdlak 

    Someone who knows if bloogger suports code formating, please comment how to do that. *so that I make it more visible to an eye including tabs and spaces..*

  2. Anonymous vurdlak 

    managed to do it inserting PRE tags! :)

  3. Anonymous Broeisi 

    Nice tutorials..

    Are you going to teach us how to program too?..

    I mean from idea to working code?.. the steps that needs to be followed from idea to design to actual working code for a working program...

    Like a graphical cd player for linux... using only gtk+.

  4. Anonymous vurdlak 

    Yes, I will provide about 50 lessons and at the end you will be able to prorgram your own applications. The idea is first to understand the given examples and try solving them yourself. Then you will be given additional tasks to solve them on your own, offcourse the soultions will be also provided.

  5. Anonymous rjc 

    Hi.

    Just a quick one.

    Could you please comply with standards? I noticed that you're using

    void main()

    instead of

    int main()


    I don't mind you using void in your software, but if you're teaching someone it's always the best to comply with standards and let the people choose later if they want to follow them or not. Especially if the link to your tutorial is on OSNews.
    Please change it.

    P.S. I like the idea. Good job.

    Regards,
    rjc

  6. Anonymous broeisi 

    The latest C standard ISO 9998:1999 C99 specifies that you must use int main(void).

    But those are minor things.... Today no compiler fully supports C99 yet.

    The key is to teach someone to be a GOOD programmer here I think :) ...
    And he is doing his utmost I must say.

  7. Anonymous rjc 

    main() function should be int, not void type, the ANSI C standard (ISO standard ISO/IEC 9899:1990) covers it for 15 years now.

    I personally don't consider those as minor things, but that's only a matter of oppinion. It's the same with the web pages - they should conform to the standard. I find it quite supprising that some of the (X)HTML tutorials aren't even valid, or teach nonstandard extensions like the infamous "embed" tag.

    When it comes to teaching someone something. You cannot possibly teach someone how to be a good programmer - that needs years of practice. In my oppinion while teaching you should follow the standards and let the people choose whether to use them or not, because if you don't they will teach others what they've learned from you.
    What's the point of having standards then?


    P.S. You've made a typo there, the number of the standard is ISO 9899:1999.

    Regards,
    rjc

  8. Anonymous Pavel 

    I fully agree with broeisi. The idea is to see the forest behind the trees, but not to delve in the minor and irrelevant details.

  9. Anonymous Nazgob 

    All looks nice, code is not but(this main...). I wish you all best and I wait for next 40 lessons(I expect sth nice for me after 40).

  10. Anonymous Titie Alemu 

    hi,

    is there any one who know the site for c like java.sun.com. I need a site that has a sample codes on java on it. I need a site because I am a student and need more on sample code.Or is the any chance to get a good sample c codes.

    Any way I like this belog very much.I enjoyed the lesson on this site and the program example.It is very easy to understand c language. But a need other site because it will try to help me for my assignment.

    If you have another blog on C please write me on this belog or by email: titie_alemu@yahoo.com

    Regards,
    Titie

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.