Commit 1b9644ea authored by s-jdoyle4's avatar s-jdoyle4
Browse files

text file of HW1

parent cf329c2f
6. Find the errors in each set of C++ statements below:
 // (a) The first line of code needs a “;” to end the statement of the constant. There is no input of or output commands for the second line of code. The correct code is below in quotes:
const double pi = 3.141592654
double radius, circum = 2*pi*radius;
“
const double pi = 3.141592654; // Sets the constant and ends statement with “;” 
double radius, circum; // Sets up variable’s 
cout << "Enter radius: "; // Asks for input of radius value
cin >> radius; // Allows user to input value for radius
circum = 2*pi*radius; // Sets up calculation of circumference based on radius’ value
cout <<”Circumference = << circum; // Show the calculation of circumference
“
// (b) In the first line there needs to be space around the “=” sign when establishing the variable*. The second line ends “;” rather than “,” to establish the basic loop. The operator in the third line for x needs space around the “+” and “//” is used for comments not “\\” which is a different operator.
int i; double x=1;
for( i=1, i<10, i++ )
 x = x+i; \\ Increment x
“
int I; double x = 1; // Establishes variables
for( i=1; i<10; i++ ) // A basic loop; i goes from 1 to 9
 x += i // Increment x // Same as x = x + I and gives the comment of “Increment x”
“
// (c) The first line should have “,” rather than “;” after the “1”s to indicate all the variables are integers with spaces around the “=.”* The second line ends to end with “{“ and third line ends to end with “}” for the of the loop to contain multiple statements. The same goes for the fourth and fifth lines.
int new=1; a=1; b=2;
if( new > 5 )
 a -= new; new = a;
else
 b += new; new = b;


“
int new = 1, a = 1, b = 2; // Sets all integers variables
if( new > 5 ) { // Condition operator with multiple statements
 a -= new; new = a; // Two values for the condition
} // Ends the statements for “if”
else { // Gives “else” condition with multiple statements
 b += new; new = b; // Two values for the condition
} // Ends the statements for “else”
“
// (d) There needs to be space around “=” and the “*” in the first line*. In the while loop there needs to be a “&&” for the logical “and” for the conditions with each condition starting with the “y” variable.
Double y=1, y_max=50*50;
while( 0 < y <= y_max )
 y *= 2;
“
double y = 1, y_max = 50 * 50; // Sets variables and values
while( y > 0 && y <= y_max ) // Makes conditions for while loop
 y *= 2; // Calculations of y variable that meet condition of loop
“
*note some spacing in the above statements maybe for style
7. For each set of C++ statements below, find the value of x after the code executes:
int i, j; double x, y;
// (a) The value of x will be “1" by hand but the program had zero as output because the program wants integers for i/j values, so gives "0." 
i=3; j=4;
x = (i/j)*(j/i);
// (b) After the complete loop, x will equal 1/945 by hand, on the program x = 0.00105082
x=1;
for( i=1; i<10; i+=2 )
 x /= i;
// (c) After all the conditions x=-1+12+-33=-22 by hand, on the program x=-22
x=1
for( i=1; i<=10; i++ )
 if( i > 6 ) x -= i;
 else if( i > 3 ) x = 2*i;
 else x--
// (d) The program gets 1 and by hand if the xy is less than zero x is 1.
x=-1; y=1;
while( x<y )
x = ( x*y < 0 ) ? -x : y++:
8. For each set of C++ statements below, find the value of x after the code executes.
int i , j; double x , y;
// (a) The program gave x = 6.1875
x=1;
for( i=1; i<5; i++ )
x = i+x / 2.0
// (b) The value of x is .0010582. By hand x is 1/945
x=1;
for( i=1; i<10; i+=2 );
x /= i;
// © The value of x is 5.
j=1
for( i=1; i<=10; i++ )
if( i > 5 )
x -= i;
else
j = 2*i
x = j/2
// (d) The value of x is 2.
x = 1; y = 2;
for( i=1; i<=5; i++ )
y *= x; x *= y;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment