For loop output in C - c

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);
}

Related

How to make this a nested For Loop?

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

Increasing a digit end of string value

I want to increase a value end of string with for loop in C.
You will see 2 for loop i actually wanted to increase i and x value simultaneously. I am new in C I moved from python and I am also open to your advice about C.
P.S: DL_ numbers actually is not static I increment them with external button so I should get their value dynamically
For example :
uint8_t DL_1=7;
uint8_t DL_2=4;
uint8_t DL_3=2;
uint8_t DL_4=1;
for(int i=0;i<=180;i+=16) {
for(int x=1;i<=4;i++) {
printf(i,DL_x)
}
}
for(int i=0;i<=180;i+=16) {
for(int x=1;i<=4;i++) {
printf(i,DL_x)
}
}
You're increasing i twice.
Maybe the second loop should use x everywhere: for(int x = 1; x <= 4; x++).
To print two integers separated by a space and terminated with a newline, printf() should be used as printf("%d %d\n", i, DL_1);
Consider using an array (and remember array indexes go from 0 to n - 1) rather than 4 separate variables
int DL[4] = {7, 4, 2, 1};
printf("%d\n", DL[1]); // prints 4
To increment x and i simultaneously, you can use just a single loop with comma separated statements:
for(int i = 0, x = 1; i <= 180 ; i += 16, x++)
{
printf(i,DL_x);
}
But there are other problems with your code, so it still will not compile, which I hope you understand.

How to use exponents using a 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);

Strange error with a For loop in C

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++)

Converting an int array to an int value in c

I have an integer array
int number[] = {1,2,3,4};
What can I do to get int x = 1234?
I need to have a c version of it.
x = 1000*number[0] + 100*number[1] + 10*number[2] + number[3];
This is basically how decimal numbers work. A more general version (when you don't know how long 'number' is) would be:
int x = 0;
int base = 10;
for(int ii = 0; ii < sizeof(number); ii++) x = base*x + number[ii];
Note - if base is something other than 10, the above code will still work. Of course, if you printed out x with the usual cout<<x, you would get a confusing answer. But it might serve you at some other time. Of course you would really want to check that number[ii] is between 0 and 9, inclusive - but that's pretty much implied by your question. Still - good programming requires checking, checking, and checking. I'm sure you can add that bit yourself, though.
You can think of how to "shift over" a number to the left by multiplying by ten. You can think of appending a digit by adding after a shift.
So you effectively end up with a loop where you do total *= 10 and then total += number[i]
Of course this only works if your array is digits, if it is characters you'll want to do number[i] - '0' and if it is in a different base you'll want to multiply by a different number (8 for instance if it is octal).
int i = 0, x = 0;
for(; i < arrSize; i++)
x = x * 10 + number[i];
x is the result.
int i;
int x = 0;
for ( i = 0; i < 4; i++ )
x = ( 10 * x + number[i] );
int number[]={1,2,3,4}
int x=0,temp;
temp=10;
for(i=0;i<number.length;i++)
{
x=x*temp+number[i];
}
cout>>x;
You could do something with a for loop and powers of 10
int tens = 1;
int final = 0;
for (int i = arrSize - 1; i <= 0; ++i)
{
final += tens*number[i];
tens*=10;
}
return final;
Answer is quite easy.Just list a complete function here.
int toNumber(int number[],arraySize)
{
int i;
int value = 0;
for(i = 0;i < arraySize;i++)
{
value *=10;
value += number[i];
}
return value;
}

Resources