Search code examples
arraysctraversal

How to repeat traverse elements of an string array?


I want to count the months from Feb to Jan or Dec to July, how can I traverse repeatedly the array to get the counted months from Feb to Jan or Dec to July.


char * months[] = {"Jan", "Feb", "Mar", "April", "May", "Jun", 
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    char start_m[5];
    char end_m[5];
    float bill, rate;
    float total;

    
    scanf("%s %s %f %f",start_m, end_m,&bill, &rate);

    int i, start, end;
    for(i = 0; i < 12; i++){
        if(strcmp(months[i], start_m) == 0){
            start = i;
        }

        if(strcmp(months[i], end_m) == 0){
            end = i;
        }
    }

    if(end < start) {
        for(i = start; i < start - end; i++){
            total += (bill * rate);
        }
    } else {
        for(i = start; i < end; i++){
            total += (bill * rate);
        }
    }
    total += bill;
    printf("%.2f", total);

Solution

  • You can use the module operation with the expression pos % size. An example:

    int main()
    {
        char * months[] = {"Jan", "Feb", "Mar", "April", "May", "Jun", 
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        int size = 12;
        int start = 1;
        
        for(int i = start; i < start + size; i++)
              printf("%d %s\n",i % size, months[i % size]);
    
        return 0;
    }
    

    Output:

    1 Feb                                                                                                                                   
    2 Mar                                                                                                                                   
    3 April                                                                                                                                 
    4 May                                                                                                                                   
    5 Jun                                                                                                                                   
    6 Jul                                                                                                                                   
    7 Aug                                                                                                                                   
    8 Sep                                                                                                                                   
    9 Oct                                                                                                                                   
    10 Nov                                                                                                                                  
    11 Dec                                                                                                                                  
    0 Jan 
    

    This works because for 0 <= i < size, the expression i % size returns i. For i = size, return 0, for i = size + 1 returns 1 and so on.