0

For “YEAR” field, create a function to calculate the value (hint : use %)based on following categories:
Year 1 : Sem 1 – Sem 3
Year 2 : Sem 4 – Sem 6
Year 3 : Sem 7 – Sem 9

so, we must convert from given semester number to year..
pls.. help me!!

2
Contributors
2
Replies
11
Views
3 Years
Discussion Span
Last Post by Himalan
0

hint : use %

That's a dead end hint that actually makes the solution more difficult. Instead, consider dividing the semester number by 3 and then taking the ceiling of the result. This will produce the correct year:

#include <math.h>
#include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i < 10; i++)
    {
        printf("%d -> %d\n", i, (int)ceil(i / 3.0));
    }

    return 0;
}

Basic arithmetic, baby. ;)

Alternatively, since you're looking at a tiny set of numbers, a brute force solution may be faster and easier to understand:

#include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i < 10; i++)
    {
        int year = 3;

        if (i < 4)
        {
            year = 1;
        }
        else if (i < 7)
        {
            year = 2;
        }

        printf("%d -> %d\n", i, year);
    }

    return 0;
}

Note, of course, that range checking the semester number was excluded in these examples but should be included in your code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.