Home > Blockchain >  Using While Loop To Iterate Through Two Values
Using While Loop To Iterate Through Two Values

Time:10-10

I am having trouble iterating through two values together. I have a program I'm writing in which I need to find a specific holiday for every year for the next 20 years from whatever year is entered and I am currently having issues being able to look through the specified days for the given years. I tried using a while loop within a while loop but it doesn't work for me.

Current Code

#include <stdio.h>

#define SIZE 1000

int main() {
    // update date opposite of year so go through all the days first then the year
    // while loop that checks if the date was on a monday and gets all the mondays from the month then checks the second last one 
    int d = 15, m = 5, y, future, day, month, year, i = 0;
    char dates[SIZE];

    printf("Enter a year: ");
    scanf("%d", &y);
    future = y   20;

    while (15 <= d && d < 25 && y <= future) {
        printf("\nDay: %d and Year: %d\n", d, y);
        d  ;
        y  ;
        // day = d;
        // printf("\nDay: %d and Year: %d\n",day,year);

        // while(y <= future){
            // printf("year: %d\n", y);
            //check dates in year from previous while loop 
            // y  ;
            // year = y;
            // printf("\nDay: %d and Year: %d\n",day,year);
        // }
    }
    return 0;
}

This code here is where both conditions are in one while loop I tried moving the year (y) condition to another while loop within the first while loop to no avail.

Current Input & Output

Enter a year: 2015

Day: 15 and Year: 2015

Day: 16 and Year: 2016

Day: 17 and Year: 2017

Day: 18 and Year: 2018

Day: 19 and Year: 2019

Day: 20 and Year: 2020

Day: 21 and Year: 2021

Day: 22 and Year: 2022

Day: 23 and Year: 2023

Day: 24 and Year: 2024

Desired Output

Day: 15 and Year: 2015

Day: 16 and Year: 2015

Day 17: and Year: 2015

Day...24 and Year...2035

So the program should count from day 15 to 24 for all years up to 20 years after the entered year but currently is only counting one day in the years up to year 2024.

CodePudding user response:

It should be nested loops. The outer loop iterates over years, the inner loop iterates over days.

It's simpler to write with for than while.

for (int year = y; year <= y 20; year  ) {
    for (int day = 15; day <= 24; day  ) {
        printf("\nDay: %d and Year: %d\n",day,year);
    }
}

But if you have to use while for academic reasons, just convert them:

int year = y;
while (year <= y   20) {
    int day = 15;
    while (day <= 24) {
        printf("\nDay: %d and Year: %d\n",day,year);
        day  ;
    }
    year  ;
}

CodePudding user response:

To build on the excellent answer by @Barmar, we can eliminate the inner loop and do this in a single loop by abstracting out the cycling of the days.

To do this we create a struct cycle that will hold all of the relevant information about how to cycle over a range:

  • The start of the range.
  • The end of the range.
  • The current value.
typedef struct cycle {
    int start;
    int end;
    int cur;
} cycle;

Now, we can create an advance function that takes a cycle and an int to increment by. If incrementing the cycle by that value will go past the end of the range, we set the current value to the start of the cycle. Otherwise we simply increment it.

void advance(cycle *c, int inc_by) {
    if (c->cur   inc_by > c->end) {
        c->cur = c->start;
    }
    else {
        c->cur  = inc_by;
    }
}

Now, in the main function we can create a range that represents the days you want to loop over, and the for loop's update can advance the cycle by one day.

So a simple version of your program with hard-coded values for the days and years looks like the following.

int main() {
    cycle days = {.start = 15, .end = 24, .cur = 15 };

    for (int year = 2000; year <= 2020; year  , advance(&days, 1)) {
        printf("\nDay: %d and Year: %d\n", days.cur, year);
    }
}
  • Related