hw2 - new y value which is equal to y*i. It then increases...

This preview shows pages 1–2. Sign up to view the full content.

ing namespace std; nclude<iostream> t main() t i, x=0; out<<"The sum of integers 1 to 7 is "; o r(i=1; i<8; i++)x+=i; out<<x<<endl; ystem("pause"); e turn 0; ng namespace std; nclude<iostream> main() i, x; ut<<"The sum of integers 1 to 7 is "; r(i=2, i>8, i++)x=+i; ut<<x<<endl; stem("pause"); urn 0; Problem 1: This program goes through an if loop. The first 2 statements are false so the computer reads the line else and gives the following: x log10(x) ln(x) 25 1.39794 3.21888 The following line is what the computer reads 5^2 log10(5^2) ln(5^2) Problem 2: This program uses a for loop as well and decreases the value of x by 1 each time the loop returns until it is false at x=5: x= 10 a=50 b=97.6974 x= 9 a=45 b=95.5002 x= 8 a=40 b=93.4207 x= 7 a=35 b=91.4748 x= 6 a=30 b=89.6831 x=5 Problem 3: This do while loop starts I at -3 and increase it by 1 until I is equal to zero. i= -2 m= -2 n= 4 i= -1 m= -1 n= 1 i= 0 m= 0 n= 0 i= 1 m= 1 n= 1 Problem 4: This program begins with i=1 and y=3. The while loop then creates a

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: new y value which is equal to y*i. It then increases the value of i AFTER it determines the new value of y and increase i until it is equal to 4. The y values in order are as follows y=y*1 y=(3*2)=6 y=(6*3)=18 y=(18*4)=72 This is where the program stops because i is equal to 4 The final display answer is: The result is 72 Problem 5: Error 1: In the for loop there should be a semicolon after i=2 and i>8 Error 2: Instead of x=+1, it should be x+=i Error 3: In the for loop you have to start I out at 1 instead of 2; if you run the program like it is now it would exclude the value of 1 to the sum of the intgers Error 4: I has to be i<8, not i>8 because you are looking for the sum of the numbers before I reaches 8 The output of the program then becomes The sum of integers 1 to 7 is 28; Old Program = New program =...
View Full Document

This note was uploaded on 02/01/2009 for the course ENGR 001 taught by Professor Dr.smith during the Spring '08 term at Lehigh University .

Page1 / 2

hw2 - new y value which is equal to y*i. It then increases...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online