Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Program Control Homework Assignment #5 (6%) Due Date: March 10, 2016 Loop Some

ID: 3676072 • Letter: C

Question

C Program Control Homework Assignment #5 (6%) Due Date: March 10, 2016 Loop Some homework problems taken directly from the textbook: 1) Write a statement or a set of statements to accomplish each of the following tasks: a) Sum the odd numbers between 1 and 99 using a for structure. Assume the integer variables sum and count have been declared. b) Print the value 333.546372 in a field width of 15 characters with precisions of 1,2,3,4, and 5. Left justify the output. What are the five values that print? c) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only 5 integers per line. Hint: Use the calculation x%5. When the value of this is 0, print a newline character( n), otherwise print a tab charactert) d) Repeat part c using a for structure. 2) Find the error in each of the following code segments and explain how to correct it. a) x=1; while (x

Explanation / Answer

Multiple Questions with multiple subquestions. Answering 1st question.

1.a)
for(count = 1,sum = 0 ; count <= 99 ; count++ ){
   sum += count;
}

1.b)
#include<stdio.h>
int main(){
double number = 333.546372;
printf("%-15lf",number);
printf(" %-15.1lf",number);
printf(" %-15.2lf",number);
printf(" %-15.3lf",number);
printf(" %-15.4lf",number);
printf(" %-15.5lf",number);

return 0;
}

output
-----------
333.546372
333.5
333.55
333.546
333.5464
333.54637

1.c)
x = 1;
while(x<=20){
printf("%d",x);
if(x%5==0)
printf(" ");
else
printf(" ");
x++;
}

1.d)
for(x = 1 ; x<=20 ; x++){
printf("%d",x);
if(x%5==0)
printf(" ");
else
printf(" ");
}