I'm having problems explaining this code and I would really like a detailed explanation of how it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=-3, y=0, k,j;
for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
printf("x=%d y=%d k=%d j=%d",x,y,k,j);
getch();
}
By the way, the answers are x=-1 y=4 k=-3 j=0. The problem is that I have tried Explaining it and I don't actually know how to get past the increment part (++j).
int x=-3, y=0, k,j;
for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
int x=-3, y=0, k=-3, j=-3;
while(x = x+(k<j), ++j) {
y += 2;
}
int x=-3, y=0, k=-3, j=-3;
x += k<j;
while(++j) {
y += 2;
x += k<j;
}
int x=-3, y=0, k=-3, j=-3;
x += 0;
while(++j) {
y += 2;
x += k<j;
}
int x=-3, y=0, k=-3, j=-3;
while(++j) {
y += 2;
x += k<j;
}
Now, this is way easier to understand:
at the end, j will be zero, as it's the exit condition of the loop;
the loop will run twice, as the third time ++j is evaluated it will evaluate to zero;
y will end as 4, as it starts at zero, the loop runs twice and each time it's incremented by 2;
k is never touched, so it stays at -3;
in the loop body, k<j always, as they start equal but j has already been incremented once the control flow reach x+=k<j, so this statement becomes ++x. As it runs twice, x becomes -1.
Related
I made a code wherein I should print the square and cube of the first 10 counting numbers but I used for loops. What I'm looking for is how to print the same output but using nested (for) statement.
Here is my code:
#include<stdio.h>
int main()
{
int x;
printf("x\tx*x\tx*x*x\t\n");
for(x=1; x<=10; x++)
printf("%d\t%d\t%d\n", x, x*x, x*x*x);
return 0;
}
With these few items to print, a nested for loop seems unecessary, but if you really want one, it could look like this:
for(x = 1; x <= 10; x++) {
for(int i = 0, X = x; i < 3; ++i, X *= x) {
printf("%d\t", X);
}
putchar('\n');
}
Demo
Here I have added the solution of this code along with its output using the nested for loop
I am working on a functions HW for my programming class and Im trying to write a function that will allow me to do exponent math (in a simple form). (Oh and I can't use the actual exponent function, I have to write my own function using for loops, if statements or other things like that.)
EX: user enters base and then enters the power to raise it by. So the user enters:
5
3
it should be 5 to the 3rd power, so it should output 125 (5x5x5). However my for loop is not working properly. How should I structure my for loop to properly handle exponent math?
code:
int main(){
int base, pow;
scanf("%d", &base);
scanf("%d", &pow);
int i;
for (i=0; i<=pow; i++) {
i *= base;
printf("%d\n", i);
}
printf("%d", i);
Well you're using the same variable for the loop and the result. You don't want to do that.
int j = 1;
for(i = 0; i < pow; i++)
{
j*= base;
}
Is what you want. You're also off by one on the loop count. With i <= pow you have
i=0, j * 5 = 5, i=1
i=1, j * 5 = 25,i=2
i=2, j * 5 = 125,i=3. This is where you want to stop but 3 <= 3 so it goes again.
i=3, j *5 = 625,i=4. 4 is not <= 3 so it will then stop here.
The loop is obviously wrong:
for (i=0; i<=pow; i++) {
i *= base;
printf("%d\n", i);
}
You are multiplying the loop counter by base for some reason, which will not yield any good.
The right one would be:
int result = 1;
for (i=0; i < pow; i++) {
result *= base;
}
You want an accumulator that starts at 1 and is multiplied by the base each time. This accumulator should also not be i (since this will change at each step of the for loop).
Try
int base, pow;
scanf("%d", &base);
scanf("%d", &pow);
int i, accumulator;
accumulator = 1
for (i=0; i<pow; i++) {
accumulator *= base;
printf("%d\n", accumulator);
}
printf("%d", accumulator);
int main(void)
{
const char * attributeNames = "StrDexConIntWisCha";
int characterValues[7] = {0};
int characterBonuses[7] = {0};
characterStats(characterValues);
}
void characterStats(int * characterValues)
{
int numberOfDice = 4; int diceType = 6;
int x = 1;//because characterValues[0] is level.
printf("What is your level? > ");
scanf("%d",&characterValues[0]);
printf("Current Level [%d]", characterValues[0]);
printf("Rolling stats.\n");
for(x; x <= numberOfDice; x++)
{
characterValues[x] = diceRoll(diceType);//rolling a d6
}
}
int diceRoll(int diceType)
{
int numberOfDice = 4;
int x,y,z = 0;
int diceResult, finalValue, lowestResult = 0;
int diceRoll[4] = {0};
for(x; x <= numberOfDice; x++)
{
printf("%d", diceRoll[x]);
}
}
I'm trying to create a function that will roll a 6-sided dice, 4 times, for a character generator for dungeons and dragons. The last for loop in diceRoll, doesn't appear to execute, it just skips it over and I don't understand why. At the moment, I'm just testing to see if everything works before adding in the rand().
int x,y,z = 0;
Here you only initialized z to 0, leaving x and y uninitialized, and then in the for loop:
for(x; x <= numberOfDice; x++)
Again, x is not initialized.
You should always initialize your loop counter in the for loop:
for(x=0; x<numberOfDice; x++)
and too, notice I used < not <=; a loop from 0 will always do n iterations from 0 for <n (using <= will do n+1). Looping from 0 to n-1 is idiomatic in all C-like languages since it tends to match things like indexes an an array being iterated, or pointer manipulations, which are all 0-based.
either make
int x=0,y,z=0;
or make
for(x = 0; x <= numberOfDice; x++)
I am attempting to process a two-dimensional array in C. I tried two nested for loops, but it seems that the two loops execute separately. I expect that the inside loop loops eight times for each loop of the outside loop, resulting in eight times the number of outside loops being the total number of loops.
As a simplified test, I tried this:
#include <stdio.h>
int main() {
int x = 0;
int y = 0;
for (; x < 7; x++, printf("(%d,%d)", x, y)) {
for (; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
This resulted in these results:
(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(1,8)(2,8)(3,8)(4,8)(5,8)(6,8)
Could somebody please explain to me why this might be happening? Thanks.
The reason is simple: you do not reinitialize the y variable in inner loop: when it reaches 8, it stays this way and the inner loop does not execute any more. Change your code to:
#include <stdio.h>
int main() {
for (x=0; x < 7; x++, printf("(%d,%d)", x, y)) {
for (y = 0; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
I am trying to understand the for loop better. I have the following variables:
x = 1
y = 10
I want to increment x and double it ten times with a for loop to have the following output: 1, 2, 4, 8, 16, etc.
This is what I have, but it is not quite doing the trick:
int x = 1;
int y = 10;
for (int i = 0; i < y; i++)
{
x *= 2;
}
printf("%d\n", x);
Do I need another variable to do this?
It looks fine to me. If you want it to print at each iteration, you need to move the printf into the loop.
Also, your code will only work in C99 or with GCC's default extensions since you have int i inside the for loop.
If you want it to display a running count then you should place printf inside the for-loop, so it gets executed with each iteration.
Do I need another variable to do this?
No. You could actually remove a variable - y. It is unneeded and you can specify 10 directly in the loop's conditional:
int i = 0;
int x = 1;
for (i = 0; i < 10; i++)
{
x *= 2;
printf("%d\n", x);
}
I want to increment x and double it ten times with a for loop to have the following output: 1, 2, 4, 8, 16, etc.
Your example contradicts your requirement. Incrementing an integer and doubling it would look produce 1, 4, 6, 8, 10, 12, 14...n. They are simply multiples of two. Your example produces powers of two, i.e.,
int x;
for( x = 0; x < 10; ++x )
{
printf("%d\n", pow(2, x) );
}
for (int i = 0; i < y; i++)
{
x *= 2;
printf("%d\n", x);
}
Think you want the printf statement inside the for loop... between the { and the }.
int x = 1;
int y = 10;
int i = 0;
int main() {
for(i = 0; i < y; i++) {
x *= 2;
printf("%d\n", x);
}
return 0;
}
Output:
2
4
8
16
32
64
128
256
512
1024
your answer contradict with your source code,if you want to print 1,2,4,8,16,... you will get first element 2, because you are multiplying every iteration 2 times than its value, you do not need to use extra variable,moreover you can remove y,put directly 10 and use printf statement inside {}.hope it will help
if you know that you have to increment it for only 10 times. Then why to use extra variable ? use this....
for(x = 1; x<=1024; x*=2)
{
printf("%d ",x);
}