cs 141 computer programming 1
DESCRIPTION
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++) CoutTRANSCRIPT
![Page 1: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/1.jpg)
1
CS 141Computer
Programming 1
Iteration Statements
![Page 2: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/2.jpg)
2
Question #1
Question
What are the syntax and/or logic errors in the following codes?
![Page 3: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/3.jpg)
3
Question #1
For(x=100,x>=1,x++) Cout<<x<<endl;
logic error is infinite loop . to correct it x-- so it counts 100 to 1
Answer
Question
![Page 4: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/4.jpg)
4
Question #1
The following code should output the odd integers from 19 to 1:
For( x=19;x>=1;x+=2)Cout<<x<<endl;
logic error infinite loop , to correct it x -= 2
Answer
Question
![Page 5: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/5.jpg)
5
Question #1
int x; while (x) { cout << x << endl;
x+=2;}
•x used without initialization , to correct it , int x =10•Infinite loop . to correct it , while(x<50)
Answer
Question
![Page 6: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/6.jpg)
6
Question #2Question
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;
Answer
14
![Page 7: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/7.jpg)
7
Question #2Question
Find the output for each loop: Answer
int i, j=0;for (i=1; i<6; i++){
if (i%2!=0){
cout << i*i , " << ;"
j+=i*i;} }cout << "\nj = " << j;
1, 9, 25j=35
![Page 8: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/8.jpg)
8
Question #2Question
Find the output for each loop:
Answer
for (int a = 1; a <= 1; a++) cout << a ;++
cout << a;
13
![Page 9: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/9.jpg)
9
Question #2Question
Find the output for each loop:
Answer
int counter = 12;do{
cout << "Good Luck \n;"counter;++
}while (counter < 12);
Good luck
![Page 10: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/10.jpg)
10
Question #2Question
Find the output for each loop:
Answer
int i = 5 ;do{cout << "***" << endl; i-- ;} while(i > 5);
***
![Page 11: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/11.jpg)
11
Question #3Question
Consider the following code segment:
int i;while (i < 10){cout << i << endl;i++;}
How could you initialize i so that the loop would be
traversed 10 times?
![Page 12: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/12.jpg)
12
Question #3Answer
int i=0;while (i < 10){cout << i << endl;i++;}
initialize i with 0
![Page 13: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/13.jpg)
13
Question #3Question
Consider the following code segment:
int i;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”?
![Page 14: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/14.jpg)
14
Question #3Answer
int i;while (i < 10){cout << i << endl;i++;}
int i =1;while (i <= 10){cout << i << endl;i++;}
![Page 15: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/15.jpg)
15
Question #4Question
Write for statements that print the following sequences of values:
A) 20, 14, 8, 2, -4, -10B) 19, 27, 35, 43, 51
![Page 16: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/16.jpg)
16
Question #4Answer
A) 20, 14, 8, 2, -4, -10
B) 19, 27, 35, 43, 51
for(i =20 ; i>=-10; i-=6)cout << i << “,”;
for(i =19 ; i<=51; i+=8)cout << i << “,”;
![Page 17: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/17.jpg)
17
Question #5Question
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;
![Page 18: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/18.jpg)
18
Question #5
Answer
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;
10 Times
4 Times
![Page 19: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/19.jpg)
19
Problems
![Page 20: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/20.jpg)
20
Question #1Question
Write a program that asks the user to enter 5 integers and writes the
smallest value.
![Page 21: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/21.jpg)
21
Answer#include<iostream>
using namespace std;int main(){
int i,smallest,n;
for(i=0;i<5;++i){
cout<<"Enter an integer ";cin>>n;if(i==0) // first number
smallest = n;else if(n<smallest)
smallest = n;}//end for
cout<<"The smallest value is "<<smallest<<endl;return 0;
}
![Page 22: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/22.jpg)
22
Question #2Question
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 1Sum of odd numbers = 11Sum of even numbers = 12
![Page 23: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/23.jpg)
23
Answer#include <iostream>using namespace std;int main(){
int numbers , x , sumOdd =0, sumEven=0;cout << "How many numbers in your set of integers? " ;cin >> numbers ;for (int i=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;
}
![Page 24: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/24.jpg)
24
Question #3Question
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
![Page 25: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/25.jpg)
25
Answer
#include <iostream>using namespace std;void main(){ int i, j;
for (i=1,j=10; i<=10 || j>=1 ; i++,j--) cout << i << " "<< j << " "; cout << endl;
system("pause");}
![Page 26: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/26.jpg)
26
Question #4Question
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 20The even numbers between 4 and 20:6 8 10 12 14 16 18 20
![Page 27: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/27.jpg)
27
#include <iostream>using namespace std;void main(){ int num1, 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<<" ";
![Page 28: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/28.jpg)
28
Evaluation
![Page 29: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/29.jpg)
29
Write a C++ program to calculate (a) to the power (b) .
![Page 30: CS 141 Computer Programming 1](https://reader035.vdocument.in/reader035/viewer/2022070414/56814e32550346895dbb9672/html5/thumbnails/30.jpg)
30