for loop: iterate between specified numbers

The loop must initialize on a specified number and iterate between the given intervals.
Eg: distributing chocolates to children.
input: n=10 c=15 q=6
output: last chocolate given to 10th child.
explanation: 6->7->8->9->10->1->2->3->4->5->6->7->8->9->10
loop should iterate in this fashion.

This code is just to show my idea of iterating through the loop, pardon me for any code errors.
->t stands for no of test cases...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<stdio.h>
#include <stdlib.h>

int main()
{
    int i,t,n,c,f;
    scanf("%d",&t);
lab:for(i=0;i<t;i++)
    {
        scanf("%d %d %d",&n,&c,&f);
        
        
        for(i=f;i<=n;i++)
        {
            c=c-1;
            if(c==0)
            {
                printf("%d ",i);
                goto lab;
                
            }
        }
        while(c!=0)
        {
        for(i=1;i<=n;i++)
        {
            c=c-1;
            if(c==0)
            {
                printf("%d ",i);
            }
        }
        }
            
    }
}

Needs ideas to solve this.
Last edited on
Hello sharbu,

Are you learning C or C++?

Line 6: You should give your variable names something that allows the reader what they do. I have no idea what "t" is or what value I should give to "t".

Although this is a small program the use of "goto" should be avoided. In this case "break;" will work just as well.

Line 10: Why would you want to enter these numbers each time the outer for loop returns to the top?

Line 23: With out testing I have the feeling that the while loop will never execute because the value of "c" will be zero by the time you reach this line.

Just my thoughts for now.

Hope that helps,

Andy
Hello sharbu,

I am not quite sure what you actually want to do, but this will print out what you want the output to look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
#include <stdlib.h>

int main()
{
	int i{ 0 }, t{ 0 }, n{ 0 }, c{ 0 }, f{ 0 };
	
	printf("\nEnter a value for t: ");
	scanf_s("%d", &t);

	printf("Enter n, c, f ");
	scanf_s("%d  %d %d", &n, &c, &f);

	printf("\n");

	for (int lc = f; lc <= n; lc++)
		printf("%d, ", lc);

	for (int lc = 1; lc <= n; lc++)
	{
		if (lc == 10)
			printf("%d", lc);
		else
			printf("%d, ", lc);
	}

	printf("\n\n\n\n");

	system("pause");

	return 0;
}


Now if the variables "n", "c" and "f" were to have better names the program would make more sense.

Hope that works,

Andy
Last edited on
Let me see. You want to print out c values. The valid values are from 1 to n and the first shown value is q.

1
2
3
4
5
for ( int counter=0; counter < c; ++counter )
{
  int value = /* function of counter, n and q */
  /* show value */
}


Do you know the operator %? That makes "cyclic" easy.
if there is a simple pattern, you can do stuff like:

for(i = 5; i < 100; i+=5) ... //5,10,15,20,...
or
for(i = 10, i < 25; i++) //10,11,12,...24

for more complex patterns you may need to fill in an array:

int nums[] = {1,3,5,2,8,6,10215, 11,-999}; //-999 is a special unused value to know when to quit, any value will do, so long as its not in your data.

int i = 0;
while (nums[i] != -999)
dosomethingwith(nums[i++]);

you can write that as a for loop, because you can write any while as a for.

for(i = 0; nums[i] != -999; i++)

Last edited on
Since C++11 you can do it like this:
1
2
3
4
for (int i: {1,3,5,2,8,6,10215, 11,-999})
{
  // use i
}
Hello sharbu,

I think I figured out your program and came up with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include <stdlib.h>

int main()
{
	int i{ 0 }, t{ 0 }, numKids{ 0 }, piecesChoclate{ 0 }, startKid{ 0 }, lastKid{ 0 };
	char ch[10];

	printf("\nEnter a value for t: ");
	scanf_s("%d", &t);

	printf("Enter number of kids, pieces of choclate, start Kid: ");
	scanf_s("%d  %d %d", &numKids, &piecesChoclate, &startKid);

	printf("\n");

	while (piecesChoclate > 0)
	{
		for (int lc = startKid; lc <= numKids; lc++)
		{
			printf("%d, ", lc);
			piecesChoclate -= 1;
		}

		while (piecesChoclate > 0)
		{
			for (int lc = 1; lc <= numKids && piecesChoclate > -1; lc++)
			{
				if (piecesChoclate == 1)
					printf("%d ", lc);
				else
					printf("%d, ", lc);

				piecesChoclate -= 1;
				lastKid = lc;

				//printf("");
			}  //  End for
		}  //  End while

		//if (piecesChoclate == -1)
		//{
			printf("\n\n Last Child is: %d", lastKid);
		//}
	}

	printf("\n\n\n\n");

	system("pause");

	return 0;
}


I have not programmed in C for many years, so my use of C may not be up to date.

The program has worked with what I have tested it with so far. But the program is not 100%. I am still having a problem with the if statement at line 29. Of course this is my idea for the output and may not be what you want to do. This can be adjusted or left out depending on what you want to do.

Notice the new names I gave the variables. they make the program easier to understand.

I am still not sure what "t" is or to be used for, so I left it out for now.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
   int n, c, q;
   cout << "Input n(children), c(chocolates), q(first child): ";
   cin >> n >> c >> q;
   cout << "The last chocolate is given to child " << 1 + ( q + c - 2 ) % n << endl;

   cout << q;
   while ( --c > 0 )
   {
      q = ( q == n ? 1 : q + 1);
      cout << "->" << q ;
   }
}


Input n(children), c(chocolates), q(first child): 10 15 6
The last chocolate is given to child 10
6->7->8->9->10->1->2->3->4->5->6->7->8->9->10
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main ()
{
    int n = 10; // number of children
    int c = 15; // number of chocolates
    int q = 6;  // first child served
    
    for(int child_no = q - 1; c != 0; child_no++)
    {
        std::cout << child_no % n + 1 << ' ';
        c--;
    }
    
    return 0 ;
}


Topic archived. No new replies allowed.