SPIES BERDAFTAR

Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

03 November 2015

programming tips #1 : conditional expression (for c & c++)

ASSALAMMUALAIKUM W.B.T. peeps !

frankly, i'm inspired to share this on my blog bcoz on this evening, i got programming test and.... i'm stucked at this question. lol! never expect this one to be questioned on test... T.T k, my bad..


after finishing the test & went back to my room, i started to do some coding bout it to understand more. and now here i am, wanting to share to you all what i got.

so, first of all. what is conditional expression?  well, it is an expression that use conditional operators.

conditional operators are basically  ? and : symbols.

what are they used for? 
they are used to simplify or shorten if...else statement.

how?
here's the syntax >>
condition ? true case : false case

comparing it with an if..else statement;

if(condition)
   true case;
else
   false case;


got it? yes? congrats, fast-learner!

no? it's ok, let's move on with a simple example..

suppose there's a if...else statement;

if(marks>50)
   pass++; //increase number of student passed by 1
else
   fail++; //increase number of student failed by 1

converting this using conditional expression;
marks>50 ? pass++ : fail++ ;

condition before the ? will be tested. if true, execute pass++ (statement between ? and :). else, execute fail++ (statement after :) .


next example.. printing output.
from;

if(weight>65)
   cout<<"Fat!!"; 
else
   cout<<"Gorgeous!"; 

from what we had learnt before, converting this using conditional expression;
weight>65 ? cout<<"Fat!!" : cout<<"Gorgeous!" ;

but! there's a simpler way of doing this;
cout<<(weight>65 ? "Fat!!" : "Gorgeous!") ;

note that the second way we need to put brackets so the compiler will give priority to the conditional expression first.

same goes in c language;
from;

if(weight>65)
   printf("Fat!!"); 
else
   printf("Gorgeous!");

converting to conditional expression;
mark>50 ? printf("PASS") : printf("FAIL"); 

or putting the conditional expression inside printf;
printf(mark>50 ? "PASS" : "FAIL");

note that for the second way we did not need to put extra bracket (comparing to c++). bracket from the printf is enough.


all of examples above are the simple ones. of course, those that went out in my exam earlier is much more complicated. ~.~

such as (using c++):

if(cpa>3.0)
{
   if (cpa>3.5)
      cout<<"Dean's List";
   else
      cout<<"Second Class";
}
else
{
    if(cpa>2.0)
       cout<<"Passed";
    else
       cout<<"Failed!"; 
}

converting to conditional expression;
cout<<(cpa>3.0?cpa>3.5?"Dean's List":"Second Class":cpa>2.0?"Passed":"Failed");

if you compare both thoroughly, you can see that is just follow the flow as the if....else statement. line by line. and this is the example for nested if...else.


last example,
(using c language);

if(height>170)
{
   printf("So tall!") ; 
   tallpeople++ ; //increase number of tall people by 1
}
else
{
   printf("Hi shorty.") ;
   shortpeople++ ; //increase number of short people by 1
}

converting to conditional expression;
height>170 ? printf("So tall!") , tallpeople++ : printf("Hi shorty.") , shortpeople++ ;

or

printf(mark>50 ? "So tall!",tallpeople++ : "Hi shorty.",shortpeople++);

this example is actually for if...else with compound statements. we just need to put comma ',' between these statements.

final note, spaces between conditional operators are not necessary, but it'll make your coding easier to read.. as malay words, "sedap mata memandang" :3


that's all for tonight. hope this helps to enhance your knowledge. ^^


till we meet again!
regards,

02 February 2015

my group project assignment

ASSALAMMUALAIKUM W.B.T. peeps !

hi. just wanna tell you guys i'm doing sports score keeper (yep, with 's' coz more than 1 sport). for my Foundation in Computing II subject group project assignment. there'll will be basketball, futsal, netball & so on if Allah wills. Currently when i'm writing this i only finished Basketball Score Keeper & working on Futsal one. (stuck at penalty shootout part,duh.). Please pray for my group k. Adios!


regards,

28 January 2015

when..

ASSALAMMUALAIKUM W.B.T. peeps !

pabila lebih utamakan kerja yg due 2 MAC berbanding ESOK kerana minat..
watever. janji dah siap assignment. alhamdulillah. :3




korang dah siap assignment csc? 

***update---assignmentku direject, kena tunggu dah blajo chapter function dlu ghupanya...huhu


regards,

27 January 2015

c programming source code sharing #3 : Summon Rate

ASSALAMMUALAIKUM W.B.T. peeps !

so.. hi! sorry for not updating last week as i got no homework. :3 as repayment, today more source codes are coming! and as for the first 1 here is Summon Rate.

example of question :

(USE NESTED IF....ELSE)

Summon Rate
1.Summon within 1 month
2.Summon within 2 months and above

If user enter 1;
1.ask input for the value of summon
2.ask whether summon is within 15 days or not
   a)if yes, 50% discount will be given. display discounted value & the new summon value
   b)if no, 25% discount will be given. display discounted value & the new summon value

If user enter 2;
1.ask input for the value of summon
2.15% discount will be given. display discounted value & the new summon value

DISPLAY ERROR MESSAGE IF USER ENTER OTHER THAN 1 & 2.

so here is the snapshot of the source code.

CLICK THE IMAGE FOR LARGER VIEW


CLICK HERE FOR THE C FILE (can copy&paste and try to compile on your own compiler&edit)




regards,

15 January 2015

c programming source code sharing #2 : area of circle/disk/rim with hole calculator

ASSALAMMUALAIKUM W.B.T. peeps !
& good day

the title itself already describe the source code i'm gonna to share today. it is basically a a program which will calculate the area of circle/disk/rim with circular hole. the user need to input the outer diameter & inner diameter of the circle.

CLICK THE PICTURE TO ENLARGE

with a little bit of twist, you can change for the user to enter radius or perhaps both based on how the user wanted! this is just the simple version ;)

CLICK HERE FOR THE C-FILE (can copy&paste directly)
                                                                    regards,

c programming source code sharing #1 : change money calculator

ASSALAMMUALAIKUM W.B.T. peeps !
& good day.


this is my first entry on sharing my source code i've worked on. from now onwards i will basically share my source codes here which are actually my homework that i will be getting every weeks this semester.

do feel free to comments & ask anything. :)

example question:

              Write a program that takes as input any change expressed in cents. It should then compute the number of 50-ringgit, 10-ringgit, 5-ringgit, 1-ringgit, 50-cent, 20-cent, and 10-cent, in that order. For example, 3020 cents should be returned as one 20-ringgit, one 10-ringgit and one 20-cent.



CLICK THE IMAGE FOR LARGER VIEW


view the c-file here (can copy-paste)


                                                                    regards,