how to convert this code into function...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ballpen1019
    New Member
    • Mar 2010
    • 7

    #1

    how to convert this code into function...

    my problem is to convert this code into function....cou ld someone help me to solve these problem..???


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
        int n, N, year, choice = 0;
        float val, value, deprec, tag;
        char answer1 = 'Y', answer2 = 'Y';
    
        while(toupper(answer1) != 'N')
        {
            system("CLS");
    
            if(toupper(answer2) != 'N')
            {
                printf("\nOriginal value: ");
                scanf("%f", &value);
                printf("Number of years: ");
                scanf("%d", &N);
            }
            printf("\nMethod: (1-SL 2-DDB 3-SYD) ");
            scanf("%d", &choice);
    
            val = value;
            n = N;
    
            switch(choice)
            {
                case 1:
                    printf("\nStraight Line Method\n\n");
                    deprec = val/n;
                    for (year = 1; year <= n; ++year)
                    {
                        val -= deprec;
                        printf("End of year %2d ", year);
                        printf("Depreciation: %7.2f ", deprec);
                        printf("Current value:%8.2f\n", val);
                    }
                    break;
                case 2:
                    printf("\nDouble Declining Balance Method\n\n");
                    for (year = 1; year <= n; ++year)
                    {
                        deprec = 2*val/n;
                        val -= deprec;
                        printf("End of year %2d ", year);
                        printf("Depreciation: %7.2f ", deprec);
                        printf("Current value:%8.2f\n", val);
                    }
                    break;
                case 3:
                    printf("\nSum-of-the-Years\'-Digits method\n\n");
                    tag = val;
                    for (year = 1; year <= n; ++year)
                    {
                        deprec = (n-year+1)*tag/(n*(n+1)/2);
                        val -= deprec;
                        printf("End of year %2d ", year);
                        printf("Depreciation: %7.2f ", deprec);
                        printf("Current value:%8.2f\n", val);
                    }
                    break;
                default: printf("NOT A VALID CHOICE");
            }
            printf("\n\nAnother calculation? [Y/N] ");
            scanf("%1s", &answer1);
            if(toupper(answer1) != 'N')
            {
                printf("Enter a new set of data? [Y/N] ");
                scanf("%1s", &answer2);
            }
    
        }
        printf("\nNormal Termination... ");
    
        system("PAUSE");
    }



    please help me to solve this problem...,.tha nk you...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It's already in a function, main(). You need to be more precise about which bit of the code needs to be in a separate function.

    Comment

    Working...