Thread: compare to pervious values

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    7

    compare to pervious values

    How can I compare a previous value with a current value to analysis the input of each day with a while loop?
    The input's times are defined by the user; as "printf("Enter the number in day %d: ", days); scanf("%d", &number)

    I want to set a current day = previous day outside the while loop, but how to compare in the loop again each time?

    This program is ok, except the twice value.

    Code:
    /*  Purpose of this program: Analytics of clicking on a web-based
    */ 
    
    
      #include <stdio.h>
        
    
    
        int main() {
     
         int days; /* with days++ in looping to count the input days */
        int daysPeriod; /* Input the days are needed to analytics */
        int highest; /* Highest value  */
        int lowest; /*Lowest value */
        int more500; /* Click more than 500 */
        int click; /* number of click each loop */
        int sumClick; /* Total click value */
        int twice; /* count the clicks that more than twice for previouse day */
        int currentday, hold;
        
        /*initialize vlaue*/
        click =0;
        highest=0;
        lowest=2.e2; /* let the lowest to be max value for easy to compare the input value. */
        twice = 0;
        hold = 0;
        currentday = 0;
        sumClick = 0;
        days = 1;
        
        /* Read how many days are need to analytis */
        printf("Enter the period of click analytics (number of days): ");
        scanf("%d", &daysPeriod);
        
        /* Using a while loop to count the input day for analytics, the input day at lease is 1 day. */
        while (days <= daysPeriod) {
    
    
        /* Read how many clicks per day within while loop for daysPeriod, as input's day. */
        printf("Enter the clicks in day %d: ", days);
        scanf("%d", &click);
        fflush(stdin);
    
    
         hold = click; /*unused*/
         
        /* Checking the value of click is value highest or lowest */
        if (click > highest)
        {
        highest = click;}
    
    
     if (click < lowest){
      lowest = click;}
      
         /* Checking the value of click, if over 500 times, the value of more500 will be counted. */
      if(click >= 500)
      {
      more500++;
      }
      
      /* Checking the value of currentday, if the click of pervious day is more then current day 2 times, 
        the value of twice will be counted */
      if(currentday/2 < click)
      {
      currentday = click;
      twice++;
        }
        
        
      /* Add the total click's value each loop, then put it into sum of click, as sumClick */
        sumClick += click;
        
        /* This days will be added each loop until all input's days are counted; as days will be equal to dayPeriod. */
        days++;
        }
    
        /* printout the click's results of average, highest, lowest, click more than 500 and clicks twice more than the previous day. */
    
        printf("Average click is: $%d\n", sumClick / daysPeriod);
        printf("Highest clicks is %d\n", highest);
        printf("Lowest clicks is %d\n", lowest);
        printf("Number of days with clicks more than 500 is %d\n", more500);
        printf("Number of days with clicks twice of previous day: %d\n", twice);
    
    
        return 0;
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    A better indentation style would help.
    Indentation style - Wikipedia

    Like so
    Code:
    /*  Purpose of this program: Analytics of clicking on a web-based
    */
    #include <stdio.h>
    
    int main()
    {
      int days;                     /* with days++ in looping to count the input days */
      int daysPeriod;               /* Input the days are needed to analytics */
      int highest;                  /* Highest value  */
      int lowest;                   /*Lowest value */
      int more500;                  /* Click more than 500 */
      int click;                    /* number of click each loop */
      int sumClick;                 /* Total click value */
      int twice;                    /* count the clicks that more than twice for previouse day */
      int currentday, hold;
    
      /*initialize vlaue */
      click = 0;
      highest = 0;
      lowest = 2.e2;                /* let the lowest to be max value for easy to compare the input value. */
      twice = 0;
      hold = 0;
      currentday = 0;
      sumClick = 0;
      days = 1;
    
      /* Read how many days are need to analytis */
      printf("Enter the period of click analytics (number of days): ");
      scanf("%d", &daysPeriod);
    
      /* Using a while loop to count the input day for analytics, the input day at lease is 1 day. */
      while (days <= daysPeriod) {
        /* Read how many clicks per day within while loop for daysPeriod, as input's day. */
        printf("Enter the clicks in day %d: ", days);
        scanf("%d", &click);
        fflush(stdin);  //!! BAD
    
        hold = click;               /*unused */
    
        /* Checking the value of click is value highest or lowest */
        if (click > highest) {
          highest = click;
        }
        if (click < lowest) {
          lowest = click;
        }
    
        /* Checking the value of click, if over 500 times, the value of more500 will be counted. */
        if (click >= 500) {
          more500++;
        }
    
        /* Checking the value of currentday, if the click of pervious day is more then current day 2 times,
           the value of twice will be counted */
        if (currentday / 2 < click) {
          currentday = click;
          twice++;
        }
    
        /* Add the total click's value each loop, then put it into sum of click, as sumClick */
        sumClick += click;
    
        /* This days will be added each loop until all input's days are counted; as days will be equal to dayPeriod. */
        days++;
      }
    
      /* printout the click's results of average, highest, lowest, click more than 500 and clicks twice more than the previous day. */
      printf("Average click is: $%d\n", sumClick / daysPeriod);
      printf("Highest clicks is %d\n", highest);
      printf("Lowest clicks is %d\n", lowest);
      printf("Number of days with clicks more than 500 is %d\n", more500);
      printf("Number of days with clicks twice of previous day: %d\n", twice);
    
      return 0;
    }

    Code:
        /* Checking the value of currentday, if the click of pervious day is more then current day 2 times,
           the value of twice will be counted */
        if (currentday / 2 < click) {
          currentday = click;
          twice++;
        }
    Better variable names would help.
    Code:
        if (yesterdayClicks / 2 < todaysClicks) {
          yesterdayClicks = todaysClicks;
          twice++;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compare values?
    By davidl33t in forum C Programming
    Replies: 1
    Last Post: 11-10-2016, 12:35 PM
  2. Replies: 5
    Last Post: 03-29-2015, 05:30 AM
  3. How to compare, without using compare (homework)
    By Mr.777 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2011, 02:55 AM
  4. compare with chars && compare with int
    By zcrself in forum C Programming
    Replies: 1
    Last Post: 04-22-2010, 03:19 AM
  5. DIFF doesn't compare numerical values?
    By patiobarbecue in forum C Programming
    Replies: 1
    Last Post: 02-11-2010, 11:44 PM

Tags for this Thread