Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to print two strings in console
int k = 3;
for (int i = 0; i < k; i++)
{
printf("\ra: %d\n\rb: %d", i*2, i*3);
}
I want to get this result:
a: 4
b: 6
But I get:
a: 0
a: 2
a: 4
b: 6
The reason why you are getting that output is that you have just one \r before a, but \n\r between a and b in the:
printf("\ra: %d\n\rb: %d", i*2, i*3);
Every cycle, the cursor returns to the beginning of line where b is and it writes OVER it, with new value for a :
1st 2nd 3rd run
a=0 a=0 a=0
b=0 a=2 a=2
b=3 a=4
b=6
since that is quick, you can see only the last one. It's really not clear what you wanna do, but if you need only the output of the last run, you can write the printf after the cycle:
for (int i = 0; i < k; i++)
{
...
}
printf("\r\na: %d\n\rb: %d", i*2, i*3);
If you change the loop to
for (int i = 2; i < k; i++)
this will print the result you asked for.
a: 4
b: 6
You could do a if inside the for like:
if (i * 2 == 4)
printf ...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
How can I change line 6 to handle the integer overflow so that this code will never terminate (while still increasing i in each loop iteration)?
1 int main() {
2 unsigned int i;
3 i = 1;
4 while (i > 0) {
5 printf("%u \n", i);
6 i *= 2;
7 }
8 printf("%u \n", i);
9 }
Because i is unsigned, it is never less than zero, but it may at some point be zero.
I might try to guarantee it is always at least 1 with something like this:
i = i*2? i*2 : 1;
That is:
If i*2 is non-zero, then that is the new value of i.
Otherwise, i*2 would be zero, so instead set i = 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
basically, I want to create a Position/location ruler which shows each numeric position starting at 1. let's say, if the characters in the above statement are 20 then in the next line 12345678901234567890 should be printed. similarly, if the characters are 30 then 123456789012345678901234567890 should be printed.
how can I do that?
Let's say n is the length you need to match
int n = 20; // or 30, or whatever
for (int i = 0; i < n; i++) printf("%d", (i + 1) % 10);
puts(""); // add newline
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In this piece of code:
int main()
{
int i=0;
while(i<10)
printf("%d",i++)
return 0;
}
If I am not wrong, the printf will always print the value +1 of i, so the first print will not be 0 but actually 1, am I right?
You are wrong. The post-increment i++ is evaluated to the value of i before incrementing, so what is printed in the first iteration is 0.
The expression x++ evaluates to x before incrementing.
int x = 5;
printf("%d\n", x++);
// Output: 5
printf("%d", x);
// Output: 6
On the other hand, ++x evaluates to x + 1, or x after incrementing:
int x = 5;
printf("%d\n", ++x);
// Output: 6
printf("%d", x);
// Output: 6
When you compile code it is read from left to right like text,
so for example your line 6
printf("%d",i++)
would first print the decimal and then add to variable.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
So I'm trying to make a guessing program in C, where the user has to input 5 numbers and the computer generates 5 random numbers. My code for the user to input the numbers is this:
for (int i = 0; i < NUM_SIZE; ++i) {
scanf("%d", &(userGuess[i]));
}
I want to output the positions where the user input was right so for example if user input 2 3 4 5 7 and the computer's 5 random number was 2 3 9 8 7 it would print out correct position(s): 0, 1, 4
This is what I have currently tried, but it can only print 0, 1 and when it tries to print a number after it skipped on like in the example I put, it would print 0, 0, and a long random number.
for (int i = 0; i < NUM_SIZE; ++i){
if (userGuess[i] == winNum[i]) {
matchedPos[i] = i;
}
}
The problem is that you never initialized the elements of matchedPos for elements that the user didn't guess right. You need to initialize all the array elements.
Also, there's no point in putting i into the matchedPos array, since it's the same as the array index. Instead, assign 0 or 1 depending on whether the guess matches. Then you just print the indexes that contain 1.
int matchedPos[NUM_SIZE];
for (int i = 0; i < NUM_SIZE; i++) {
matchedPos[i] = userGuess[i] == winNum[i];
}
printf("Correct guesses: ");
for (int i = 0; i < NUM_SIZE; i++) {
if (matchedPos[i]) {
printf("%d ", i);
}
printf("\n");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
int attack_grid[10][10] = { {0} };
void drawAttackGrid()
{
int i, j, x = 0, y = 0;
for (i = 0; i <= 10 - 1; i++) {
for (j = 0; j <= 10 - 1; j++) {
if (attack_grid[x][y] > 0)
printf(" * ");
else if (attack_grid[x][y] < 0)
printf(" ~ ");
else
printf(" ? ");
y++;
}
printf("\n");
x++;
}
}
So I am trying to fill this 10x10 array with different characters based on the value of the coordinate in other 10x10 array which is filled with zeros only(I'm gonna change those values later so that's why I need it to be general). According to my code it should print only " ? ", but there are some " * " in the output too. Can someone explain me why do i get those " * " there, please?
Your program has undefined behavior due to the value of y.
y gets incremented in the inner loop but never gets reset to 0 when the outer loop is repeated.
In the second run of the outer loop, the value of y will start at 10 instead of starting at 0.
In the third run of the outer loop, the value of y will start at 20 instead of starting at 0.
That goes on for the remaining iterations of the outer loop.
You can remove the redundant indices x and y. Use attack_grid[i][j] instead of attack_grid[x][y]