300 likes | 387 Views
CS 141 Computer Programming 1. Iteration Statements. Question #1. Question. What are the syntax and/or logic errors in the following codes?. Question #1. Question. For(x=100,x>=1,x++) Cout <<x<< endl ;. Answer. logic error is infinite loop . to correct it x-- so it counts 100 to 1.
E N D
CS 141Computer Programming 1 Iteration Statements
Question #1 Question What are the syntax and/or logic errors in the following codes?
Question #1 Question • For(x=100,x>=1,x++) • Cout<<x<<endl; Answer logic error is infinite loop . to correct it x-- so it counts 100 to 1
Question #1 Question • The following code should output the odd integers from 19 to 1: • For( x=19;x>=1;x+=2) • Cout<<x<<endl; Answer logic error infinite loop , to correct it x -= 2
Question #1 Question • int x; • while (x) { • cout << x << endl; • x+=2;} Answer x used without initialization , to correct it , int x =10 Infinite loop . to correct it , while(x<50)
Question #2 Answer Question 14 Find the output for each loop: int x=1, t=0 , y; while(x<=3) { y = x*x; t=t+y; x++; } cout << t << endl;
Question #2 Question Answer Find the output for each loop: inti, j=0; for (i=1; i<6; i++) { if (i%2!=0) { cout << i*i << " , "; j+=i*i; } } cout << "\nj = " << j; 1 ,9 ,25 j=35
Question #2 Answer Question Find the output for each loop: 13 for (int a = 1; a <= 1; a++) cout << a++; cout << a;
Question #2 Answer Question Find the output for each loop: Good luck int counter = 12; do { cout << "Good Luck \n"; counter++; } while (counter < 12);
Question #2 Answer Question Find the output for each loop: *** inti = 5 ; do{ cout << "***" << endl; i-- ;} while(i > 5);
Question #3 Question Consider the following code segment: • inti; • while (i < 10) • { • cout << i << endl; • i++; • } How could you initialize i so that the loop would be traversed 10 times?
Question #3 Answer • initialize i with 0 • inti=0; • while (i < 10) • { • cout << i << endl; • i++; • }
Question #3 Question Consider the following code segment: • inti; • while (i < 10) • { • cout << i << endl; • i++; • } If the body of the loop was kept as it is above, how can you change the initialization and the condition to print the same output “the numbers 1 to 10”?
Question #3 Answer • inti; • while (i < 10) • { • cout << i << endl; • i++; • } • inti =1; • while (i <=10) • { • cout << i << endl; • i++; • }
Question #4 Question Write for statements that print the following sequences of values: A) 20, 14, 8, 2, -4, -10 B) 19, 27, 35, 43, 51
Question #4 Answer A) 20, 14, 8, 2, -4, -10 for(i =20 ; i>=-10; i-=6) cout << i << “,”; B) 19, 27, 35, 43, 51 for(i =19 ; i<=51; i+=8) cout << i << “,”;
Question #5 Question Compute the number of times the statement in the body of the following FOR loops will be executed. int i , sum = 0;for (i=10;i>=1;i--)sum+=i; int i , sum = 0;for (i=10;i>=1;i-=3)sum+=i;
Question #5 Answer int i , sum = 0;for (i=10;i>=1;i--)sum+=i; 10 Times int i , sum = 0;for (i=10;i>=1;i-=3)sum+=i; 4 Times
Question #1 Question Write a program that asks the user to enter 5 integers and writes the smallest value.
#include<iostream> usingnamespace std; int main() { • inti,smallest,n; • for(i=0;i<5;++i) • { • cout<<"Enter an integer "; • cin>>n; • if(i==0) // first number • smallest = n; • elseif(n<smallest) • smallest = n; • }//end for • cout<<"The smallest value is "<<smallest<<endl; • return 0; } Answer
Question #2 Question Write a program that reads a set integers, and then finds and prints the sum of the even and odd integers. How many numbers in your set of integers? : 6 Start Entering: 2 4 3 6 7 1 Sum of odd numbers = 11 Sum of even numbers = 12
#include<iostream> usingnamespace std; int main() { • int numbers , x , sumOdd =0, sumEven=0; • cout << "How many numbers in your set of integers? " ; • cin >> numbers ; • for (inti=0 ; i< numbers ; i++) • { • cin >> x; • if (x%2==0) • sumEven+=x; • else • sumOdd+=x; • } • cout << "Sum of odd numbers = " << sumOdd << endl; • cout << "Sum of even numbers = " << sumEven << endl; • return 0; } Answer
Question #3 Question Write a program using for loop that generates the following output: 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1
Answer #include<iostream> usingnamespacestd; void main() { inti, j; for (i=1,j=10; i<=10 || j>=1 ; i++,j--) cout << i << " "<< j << " "; cout<< endl; system("pause"); }
Question #4 Question Write a program that reads two even integers then prints all even integers between them, following is a sample output Enter min and max even numbers respectively: 4 20 The even numbers between 4 and 20: 6 8 10 12 14 16 18 20
#include<iostream> usingnamespacestd; voidmain() { intnum1, num2, i; cout<< "Enter min and max even numbers respectively : \n"; cin>>num1>>num2; cout<< "The even numbers between "<<num1<<" and "<<num2<<" : \n"; for(i=num1;i<=num2;i++) if(i%2 ==0) cout<<i<<" "; cout<<endl; system("pause"); } Answer for(i=num1;i<=num2;i+=2) cout<<i<<" ";