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...
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.
#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.
#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 ;
}