C++ programming pleas i need all code for this homework Lecture 9. For, While 1.
ID: 3583967 • Letter: C
Question
C++ programming
pleas i need all code for this homework
Lecture 9. For, While
1. FOR
for(x=0; x<10; x++){
printf("ok");
}
2. WHILE
x=0; // step 1: initialize the loop variable x
while (x<10){ // step 2: check loop exit condition
printf("ok"); // step 3: loop body
x++; // step 4: loop variable increment
}
3. Homework
1) Modify the example code so that it prints 20 ok's.
2) Modify the code such that it prints 30 *'s in the first line, 29 *'s in the second, and 28 *'s in the third line.
****........*****
****........****
****........***
3) Modify the code such that it prints 0 to 99.
0 1 2 3 .... 99
4) Modify the code such that it prints following
1 3 5 7 9 ...... 99
5) Modify the code such that it prints following.
300 299 298 297 ................ 32 31 30
6) Predict the result of following code without programming. Confirm your prediction by programming. Explain the result. Change them to "while" loop.
6.1)
for(x=0;x<10;x++){
printf("ok");
}
printf("%d",x);
6.2)
for(x=0;x<10;x++){
x=5;
printf("%d",x);
}
6.3)
for(x=0;x<10;x++){
x=9;
printf("%d",x);
}
Explanation / Answer
1) for(int i=0; i<20; i++){
cout<<"ok"<<endl;
}
int i=0;
while(i<20){
cout<<"ok"<<endl;
i++;
}
2)
3) for(int i=0; i<100; i++){
cout<<i;
}
int i=0;
while(i<100){
cout<<i;
i++;
}
4) for(int i=1; i<100; i+=2){
cout<<i;
}
int i=1;
while(i<100){
cout<<i;
i+=2;
}
5) for(int i=300; i>29; i--){
cout<<i;
}
int i=300;
while(i>29){
cout<<i;
i--;
}
6.1) result is 10
int i=0;
while(i<10){
cout<<"ok";
i++;
}
cout<<i;
6.2) output is 5 and loop will not terminate.
int i=0;
while(i<10){
i=5;
cout<<i;
i++;
}
6.3) output is 9 and loop will be terminate after first entry
int i=0;
while(i<10){
i=9;
cout<<i;
i++;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.