1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Modular Programing Perspective

Discussion in 'C' started by KHANZZzzz, Jul 29, 2012.

  1. KHANZZzzz

    KHANZZzzz New Member

    Joined:
    Jul 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I need a Program In C or either Pascal, including 2 conditions of (if-else) + loops (fixed and non fixed both) in Modular Programing Perspective?

    what i tried as i dont know c very well but some how what i did is
    Code:
     #include <stdio.h>
    
     int salary,check1,check2,tax=250,medical=500;
    
     main( );
    
     {
    
     Information( );
    
     Check_Salary( );
    
     Results ( );
    
     }
    
     Information( );
    
     { printf("enter your salary:);
    
     scanf("%d",&salary);
    
     }
    
     Check_Salary( );
    
     {
    
     if(salary<1000)
    
     if(salary>1000)
    
    // from here i dont know how to proceed coz i have to use one fixed loop (for loop) and one non fixed loop ( do while )

    so please do it for me...
     
  2. KHANZZzzz

    KHANZZzzz New Member

    Joined:
    Jul 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Note: its not neccessary to have or create the same program i tried. you can create any program under modular programing perspective with 2 conditions and and both fixed and non fixed loops
     
  3. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    In C, the compiler needs to know about your functions before they're called. You can do this one of two ways

    way one

    Code:
    #includes ...
    
    void function1(void); // function prototype
    
    int main(...) {
       
       ... // other bits of code
       function1(); // function call
       ... // other bits of code
    }
    
    void function1(void) {  // function definition
       ...
    }
    or

    Code:
    #includes ...
    
    void function2(void) { // function definition
       ...
    }
    
    int main(...) {
     
       ...  // other bits of code
       function2();
       ...  // other bits of code
    }
    to your program, using global variables is risky because you can unintentionally change a value anywhere.

    Code:
    #include <stdio.h>
    
    void showValue(int val) {
      
       // this function doesn't return anything
     
       printf("The value is %d", val);
    }
    
    int retValue(int val) {
    
       // a copy of "val" is passed into the function
       // "val" can be changed within the function but
       // the changes are local - it will
       // not affect the variable passed by the caller   
       // function returns an integer
    
       int tmp = 0;
    
       if(val > 1000)
          tmp = 3;
       else if(val > 500)
          tmp = 2;
       else
          tmp = 1;
    
       return tmp;
    }
    
    void getValue(int *val) {
    
       // this function is pass by reference; changes to "val"
       // within the function affect the value passed by the caller
       // the function doesn't return a value
    
       printf("Enter your number: ");
       scanf("%d", tmp);
    }
    
    int main(void) {
    
       int myval; // scope is local to main
    
       getValue(&myval); // pass the address of myval so that the function
                         // getValue can alter it
    
        showValue(retValue(myval)); 
          // retValue returns an integer and showValue takes an integer
          // as a parameter.  The output *should* be either 3, 2, or 1
          // depending on the value entered
    
        while(getchar() != '\n'){} // pause output
    
         return 0;
    } 
    loops are fairly straight forward.

    Code:
    for(variable_initializer; test condition; increment or decrement variable_initializer) {
       // loop may or may not execute - depends on test condition
       ... // loop body
    }
    Code:
    variable_initializer
    
    while(test condition) {  
       // loop may or may not execute - depends on test condition
       ... // loop body
       increment / decrement variable_initializer   
    }
    Code:
    do {
       // loop executes at least once
       ... // loop body
       // manipulate test condition
    } while(test condition);
    Code:
    int myloop = 0; // initializer
    
    for(;;) { // "forever" loop
       if(myloop == 20) break; // test condition
       printf("%d\n", myloop); // print 0 - 19 
       ++myloop; // increment initializer
    }
    HTH; good luck
     

Share This Page