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: 3676217 • 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

Question 2 find the error of the following code.

Explain :- 2.a) question is

#include <stdio.h>

x=1;

while(x<10);

                x++;

}

Error in this code local variable definition not right. Variable type not define

Second error is while loop execution is not correct. In this code you add semicolon after condatioin and starting parentheses not add.

Correct program is   

#include <stdio.h>

int main()

{

    int x=1;

                while(x<=10){

                    printf("value of a: %d ", x);

                    x++;

                }

}

Explain :- 2.b) question

Error :- In for loop variable type not define y=.1 is not correct.

Error :- second error is you condition is not correct. In this code for loop count ion and never stop. Because y!=1.0 never true.

Error :- print_f is not correct. Correct is printf();

I explain you small program

#include <stdio.h>

int main()

{

    float y;

    for(y=.1;y<1.0;y+=.1){

        printf("%f ",y);

       

    }

}

Explain :- 2.c) question

Error :- in switch case case 1 not add break then its continue and enter in case 2 its not right.

Explain with right program :-

#include <stdio.h>

int main () {

   /* local variable definition */

   char grade = 1;

   switch(n) {

      case 1 :

         printf("Number is 1 " );

      case 2 :

         printf("Number is 2 " );

         break;

      default :

         printf("Number is not 1 or 2 " );

   }

  

   return 0;

}

Explain :- 2.d) question

Error :- Variable type not define

Error :- Condition is not right current condition is print 1 to 9 because first time in n assign 1 and condition is n <10   

#include <stdio.h>

int main()

{

    int n=1;

                while(n<=10)

                    printf("%d",nx++);

}

Explain :- 2.d) question

Current program return no error or no output

Because in this program we add 2 in counter variable. So if condition never true first time in counter is 1 and we add 2 after if now in counter 3....

Try with this code

#include <stdio.h>

int main()

{

    int n=1;

    do{

        if(n%2==0)

            printf("%d ",n);

        n +=1;

    }while(n<100);

}

Question 3:- what does the following program print out:

In this program we enter two integer value

Assume we add first value 4 and second 6 then

Now we look in inner for loop. We print @ if over second foe loop condition is true .

Program output is:-

@@@@

@@@@

@@@@

@@@@

@@@@

@@@@

First value show left to right and second show top to down.