Thread: How to get rid of repeated if statements and make a loop.

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    8

    How to get rid of repeated if statements and make a loop.

    Hi I have written a small program in which I am trying to assign values to variables a and b (1000-5000 with increment 1000 and 100-500 with increment of 100) dependent to the counter interval of 20. So that on each counter interval all possible combinations are assigned to these two variable (a and b). I apologize for my English and hope that my description makes some sense to the reader.
    The program works as expected! But I want to make it look simpler and get rid of if repetitions. I have tried a while and for loop but due to lack of experience I could not simplify it. I hope some people can show me the way how to do it. And get my coding level further.
    Thank you

    Here is the code:


    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int xinterval = 20;
        int y=5;
        int x=5;
    
    
        int count;
        int states = x*y;
    
    
        int a=0;
        int b=0;
    
    
    
    
    
    
        for(count=0;count<(xinterval*states);count++)
        {
            printf("%i\t%i\t%i\n", count,a,b);
            if(count>0){a=1000;b=100;}
            if(count>20){a=1000;b=200;}
            if(count>40){a=1000;b=300;}
            if(count>60){a=1000;b=400;}
            if(count>80){a=1000;b=500;}
    
    
            if(count>100){a=2000;b=100;}
            if(count>120){a=2000;b=200;}
            if(count>140){a=2000;b=300;}
            if(count>160){a=2000;b=400;}
            if(count>180){a=2000;b=500;}
    
    
            if(count>200){a=3000;b=100;}
            if(count>220){a=3000;b=200;}
            if(count>240){a=3000;b=300;}
            if(count>260){a=3000;b=400;}
            if(count>280){a=3000;b=500;}
    
    
            if(count>300){a=4000;b=100;}
            if(count>320){a=4000;b=200;}
            if(count>340){a=4000;b=300;}
            if(count>360){a=4000;b=400;}
            if(count>380){a=4000;b=500;}
    
    
            if(count>400){a=5000;b=100;}
            if(count>420){a=5000;b=200;}
            if(count>440){a=5000;b=300;}
            if(count>460){a=5000;b=400;}
            if(count>480){a=5000;b=500;}
        }
    
    
    
    
        return 0;
    }
    Last edited by hekk_tech; 02-17-2022 at 07:19 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,334
    Well
    a = 1000 + (count/100) * 1000;
    b = ((count % 100) / 20 + 1) * 100;

    Play around with ideas like this, there is clearly an arithmetic relationship between the values.
    Figure out what it is.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    8
    Quote Originally Posted by Salem View Post
    Well
    a = 1000 + (count/100) * 1000;
    b = ((count % 100) / 20 + 1) * 100;

    Play around with ideas like this, there is clearly an arithmetic relationship between the values.
    Figure out what it is.
    Thank you very much your answer put me definitely in the right direction I think I know how to do it now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop statements
    By viira in forum C Programming
    Replies: 4
    Last Post: 10-22-2014, 03:03 PM
  2. For Loop Skipping Statements.
    By rswest in forum C Programming
    Replies: 3
    Last Post: 01-27-2013, 07:12 AM
  3. HW Help - Switch and Loop statements
    By Pl2lNCE in forum C Programming
    Replies: 1
    Last Post: 01-22-2013, 04:16 PM
  4. How to make this function work with cout statements?
    By cplusplusnoob in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2012, 02:23 PM
  5. Loop and Case statements
    By Nexcompac in forum C Programming
    Replies: 6
    Last Post: 06-26-2008, 04:11 AM

Tags for this Thread