C Programming: Simulate Program to Calculate xxx(17)

Click For Summary
The discussion revolves around simulating a C program that calculates the number of times an integer can be halved until it is less than 2. The provided function `xxx(int n)` correctly initializes a count and uses a while loop to divide `n` by 2, incrementing the count each time. The user confirms their calculations for `xxx(17)`, showing that the count reaches 4 after halving 17 down to 1. The output of the program, which prints "xxx(17)=4", is verified as correct. The overall consensus is that the implementation and the calculations are accurate.
Maybe_Memorie
Messages
346
Reaction score
0

Homework Statement



Simulate the following program

#include <stdio.h>

int xxx (int n)
{
int count;

count = 0;

while ( n >=2)
{
n = n/2;
++count;
}

return count;
}

main()
{
printf( "xxx(17)=%d\n", xxx(17) );
}

Homework Equations





The Attempt at a Solution



Is this correct?

n, count
17 0
8 1
4 2
2 3
1 4

Return 4, 4 gets printed
 
Physics news on Phys.org
Looks right.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K