1 / 30

CS 141 Computer Programming 1

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.

moke
Télécharger la présentation

CS 141 Computer Programming 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 141Computer Programming 1 Iteration Statements

  2. Question #1 Question What are the syntax and/or logic errors in the following codes?

  3. 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

  4. 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

  5. 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)

  6. 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;

  7. 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

  8. Question #2 Answer Question Find the output for each loop: 13 for (int a = 1; a <= 1; a++) cout << a++; cout << a;

  9. Question #2 Answer Question Find the output for each loop: Good luck int counter = 12; do { cout << "Good Luck \n"; counter++; } while (counter < 12);

  10. Question #2 Answer Question Find the output for each loop: *** inti = 5 ; do{ cout << "***" << endl; i-- ;} while(i > 5);

  11. 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?

  12. Question #3 Answer • initialize i with 0 • inti=0; • while (i < 10) • { • cout << i << endl; • i++; • }

  13. 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”?

  14. Question #3 Answer • inti; • while (i < 10) • { • cout << i << endl; • i++; • } • inti =1; • while (i <=10) • { • cout << i << endl; • i++; • }

  15. 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

  16. 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 << “,”;

  17. 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;

  18. 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

  19. Problems

  20. Question #1 Question Write a program that asks the user to enter 5 integers and writes the smallest value.

  21. #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

  22. 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

  23. #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

  24. 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

  25. 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"); }

  26. 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

  27. #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<<" ";

  28. Evaluation

  29. Write a C++ program to calculate (a) to the power (b) .

More Related