# Tag Info

3

It's because you did a calculation and didn't do anything with it. You have cents - 25; Where are you putting the result? All of these would work: int change = cents - 25; cents = cents - 25; cents -= 25; The infinite loop is another story. To fix that loop, you'd use either of the latter two. If this answers your question, please accept this answer to ...

3

It actually prints 4,8,16,32,64 etc. until it hits the maximum integer value of 232-1 and from then on, it can only print 0 because you've overflowed the integer. It happens so quickly that you miss the first numbers. Try substituting the while loop with a for loop so you can see what happens. #include <stdio.h> int main (void) { int x = 2; ...

2

Q1. Should I use fgetc() or fread() A1.: You can make both work! Choose the one you're more comfortable with. Q2. I don't know whether to use a for loop or a while loop. Is one better for this pset? A2.: In C you can translate a for loop to a while loop and vice-versa. So, again, choose the one you like best. Here's some pseudocode for you to ...

2

Try initializing your velocity outside the loop. Right now you are reinitializing it every iteration of the loop, so your velocity is always 2.0, and is never changing.

2

Well, you're not actually using the variable that's declared inside of the loop, but we'll get to that. Here is a couple of points Every loop has a continuation condition (the one between the parentheses after while in this case). As long as this condition is true, the loop will continue executing. The do...while loop is special in the sense that it always ...

2

Let's take a look at the man page of strcat(): char *strcat(char *dest, const char *src); The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and adds a terminating null byte. So, it seems that strcat() doesn't just return to you a new string with the result of the ...

2

The while loop is not random. The sort function doesn't handle duplicates (ie values[i] == values[i + 1]) so the while loop goes "infinite" because if( sortcount == (n - 1) ) is never true. I suspect if you run the program 10 times in a row with 100, it will eventually fail. As n gets bigger, it is more likely there will be duplicates in randomNumbers.

1

You have created an infinite loop. If the loop starts, it will never exit the loop. It tests the value contained in amount to decide to continue or exit, but since the value of amount is not changed inside the loop, it will never exit. You either need to change the value of amount inside the loop or you need to use an if statement instead, so that it only ...

1

More accurately, it isn't counting pennies correctly. In fact, it isn't counting a lot of things correctly. (Try \$4.20) The cause of the problem is the whole purpose of this pset. Because of that, I'll suggest going back and reviewing the class lectures and other material related to the inaccurate storage of floats. Hint: try printing out the value of f ...

1

For the first inner loop, use a while loop, not do..while, as you don't want any space printed in front on the last line. The difference between while and do..while is that the do..while runs at least once. Here, I'd prefer for loops instead.

Only top voted, non community-wiki answers of a minimum length are eligible