Loop Practice 3/5/08

advertisement
Loop Practice 3/5/08
1. Write a fragment of code that will compute the sum of the first n positive odd
integers, for example if n is 5, you should compute 1 + 3 + 5 + 7 + 9. You can
assume that variables n and sum exist and are appropriately initialized.
sum=0;
for (int k=1; n > 0; k+=2, n- -) {
sum = sum + k;
}
2. Convert the following code so that it uses nested while statements instead of for
statements.
int s=0;
int t=1;
for (int i=0; i < 10; i++)
{
s = s + i;
for ( int j = i; j > 0; j--) {
t = t * ( j – i);
}
s = s * t;
System.out.println(”T is ” + t);
}
System.out.println( “ S is “ + s);
int s=0;
int t=1;
int i, j;
i = 0;
while ( i < 10)
{
s = s + i;
j = 1;
while ( j > 0) {
t = t * ( j – i);
j= j -1;
}
s = s * t;
System.out.println(”T is ” + t);
i ++;
}
System.out.println( “ S is “ + s);
3. What does the following fragment of code display? What do you think the
programmer intended the code to do, and how would you fix it?
int product = 1;
int max = 20;
for (int i=0; i <= max; i++)
product = product * i;
System.out.println(”The product is ” + product);
This program is supposed to calculate the product of the first 20 integers, what is
prints however is :
The product is 0
The reason is that the first time i is zero, when multiplied by product, it produces
zero which is stored back into product, and then each iteration multiplies i by zero
again…
to fix this initialize i to 1 instead of zero
for (int i=1 ; i <= max; i++)
4. What does the following fragment of code display? What do you think the
programmer intended the code to do, and how would you fix it?
int sum =0;
int product =1;
int max = 20;
for (int i=1; i <= max; i++)
sum = sum + i;
product = product + i;
System.out.println(”The sum is ” + sum + ” and the product is ” + product );
This program is supposed to calculate the sum and product of the first 20 integers,
However there are 2 problems:
First, only the first statement sum=sum + i is the body of the loop since there are no
braces!
Secondly, After the loop exits, I no longer exists and the next calculation
product = product * i would produce an error.
To fix this add braces so that both calculations are part of the loop body..
for (int i=1; i <= max; i++) {
sum = sum + i;
product = product + i;
}
5. Write a for statement to compute the sum 1 + 22 + 32 + …+ n2
int sum=0;
for (int k=1; k <= n; k++)
sum = sum + ( k * k);
AS proposed in class what if we try to sum 1 + 2m + 3m + …+ nm
int sum=0;
for (int k=1; k <= n; k++)
sum = sum + Math.pow(k, m);
AS proposed in class what if we try to sum 1 + 22 + 33 + …+ nn
int sum=0;
for (int k=1; k <= n; k++)
sum = sum + Math.pow(k, k);
6. Assume that there exists a String variable quote, assume that it contains some
value for example: “All for one, and one for all.”. Write a code fragment
containing a for loop that counts and prints out the number of blank characters in
the string.
char current;
int numblanks = 0;
for (int k=0; k < quote.length(); k++) {
current = quote.charAt(k);
if (current == ‘ ‘) numblanks = numblanks + 1;
}
System.out.println(“The number of blanks is : “ + numblanks);
7. Assume that there exists a String variable quote, assume that it contains some
value for example: “All for one, and one for all.”. Write a code fragment
containing that prints the string as is, and then prints the string in reverse!!
char current;
for (int k = quote.length()-1; k >= 0; k-- ) {
current = quote.charAt(k);
System.out.print(current);
}
System.out.println();
Download