COP 3014 EXAM
Write a program to find the nth power of a number x. That is, read in a real number x and an integer n, and caclulate x raised to n. You can assume that neither x nor n will be 0. n will always be a positive number. Sample Output 1 : Enter x: 2 Enter the exponent n: 7 2^7 = 128 Sample Output 2 :Enter x: 10.75 Enter the exponent n: 5 10.75^5 = 143562.93261
#include <iostream> using namespace std; int main() { double x, result = 1.0; int n; cout << "Enter x: "; cin >> x; cout << "Enter the exponent n: "; cin >> n; for (int i = 1; i <= n; ++i) { result *= x; }
Write a C++ code snippet to read in the length, width, and height of a rectangular prism. Check to make sure all 3 are positive. If they are all positive, calculate its volume by multiplying the 3 quantities and print the volume. Then if the volume were between 0 and 500, print "small". If the volume were between 501 and 1000, print "medium". Otherwise, print "large". If all 3 values were not positive, print an error message.
#include <iostream> using namespace std; int main() { int length,width, height; cout<<"enter a length width and height"<<endl; cin>>length; cin>>width; cin>>height; int vol; while(length<=0 || width<=0 || height<=0){ //boundary check cout<<"Error not a positive number"<<endl; cout<<"try again"<<endl; cin>>length; cin>>width; cin>>height; } vol=length * width *height; if(vol>=0 && vol<=500){ cout<<"small"<<endl; } else if(vol>=501 && vol<=1000){ cout<<"medium"<<endl; } else{ cout<<"large"<<endl; } return 0; }
Write a program to find the Lowest Common Multiple of 2 numbers. An easy way is to start at the higher number, and keep multiplying the numbers up alternatively unti we get a common number. Sample Run: Enter 2 numbers: 14 36 The Lowest Common Multiple is 252
#include <iostream> using namespace std; int main() { int num1, num2; cout << "Enter 2 numbers: "; cin >> num1 >> num2; int lcm, maxNum; // Determine the maximum of the two numbers maxNum = (num1 > num2) ? num1 : num2; // Start from the higher number and keep multiplying until we find the LCM lcm = maxNum; while (lcm % num1 != 0 || lcm % num2 != 0) { lcm += maxNum; } cout << "The Lowest Common Multiple is " << lcm << endl; return 0; }
Write a C++ Code snippet to read in an integer. Then, if the integer were either of 25, 79, or 192, print "acceptable". Otherwise, ask for a second number, and print 2.5 * the second number.
#include <iostream> using namespace std; int main() { int num; cout<<"enter in a num"<<endl; //getting user input cin>>num; if(num==25 || num==79 || num==192) { cout<<"acceptable"<<endl; //printing out } else { cout<<"enter a second number "<<endl; //getting a 2nd number cin>>num; //saving to memory cout<<2.5*num<<endl;; //printing out the product } return 0; }
Write a program to print the sum of the first 200 even numbers (0 included).Sample Output:The sum of the first 200 even numbers is : 39800
#include <iostream> using namespace std; int main() { int sum = 0; for (int i = 0; i <= 400; i += 2) { sum += i; } cout << "The sum of the first 200 even numbers is: " << sum << endl; return 0; }
Write a C++ program to read a series of numbers from the user. Stop when the user enters0. Print the sum of the squares of the numbers. Sample Run: Enter the numbers: 15 -20 123.68 -5.321 0 The sum is 15950.1
#include<iostream>using namespace std; int main(){double num, sum=0; cout<<"Enter the numbers: "<<endl; cin>>num; while(num != 0){sum = sum + num*num; cin>>num;} cout<<"The sum is "<<sum<<endl; return 0; }
Write a program to calculate the sum of all the multiples of a user-entered positive number between 1 and 3000 (inclusive). Sample Run: Enter the number: 12 The sum is 376500
#include<iostream>using namespace std; int main(){int sum =0, num; cout<<"Enter the number: "; cin>>num; for(int i=num; i<=3000; i+=num) sum+=i; cout<<"The sum is "<<sum<<endl;return 0; }
The increment operator is used to increase the value of a variables by 1. Which of the following is the increment operator >> ++ << ?:
++
Which of the following is a valid case label for a switch statement? Select an answer and submit. a "Hello" b 4.5 c -14 d endl
-14
List 4 of the programming practices/techniquees we have learned
1. Testing 2. error/statements validating input 3. formatting properly 4. building code inside out 5. comments to keep track of thought process 6. psuedocode
double sum=0; for(int=20; i<50; i+=8) { sum*sum+i; if(i%4 ==0) { i++ cout<<"i is "<< i<<endl; } What is the value of sum after the code runs
131
2. Evaluate the following expression. x = 5, y = 15, z = 3 (All integers) x * 4 + y / 2 % z
21
int x=39, y=30; int z = ++x *y--/4; What is the value of z after the code runs
300
Which of the following has the lowest precedence == ++ - %
==
A C++ variable can begin with (a) A letter (b) underscore (c) '$' sign (d) All of the above
All of the above
Which of the following conditions is equivalent to !(x >= 10 && y!=19)? A. ( y!=19 &&x x>10) B. (y==19 || x<10) C.( 10 <x || y == 19) D. (x <10 && y==19)
B
Which of the following is true A. The order in a switch statement matters B. A program cannot contain multiple variables with the same name C. A for loop cannot contain multiple comma seperated increments D. All loops check the condition at least once
D
1. Which of the following is NOT a C++ reserved word?(a) switch(b) continue(c) char(d) main
D. Main
What is integer overflow? Explain with an example
Integer overflow occurs when an arithmetic operation on integers exceeds the maximum or minimum representable value for the data type being used, causing an unexpected result. This typically occurs in languages where integers have a fixed size, and when the result of an operation exceeds the maximum or minimum value that can be stored in that fixed size. An example would be a 32-bit signed integer that is trying to hold a maximum value. Say the integer was assigned one over the limit the value of the integer would be the minimum because it wraps around.
What is the difference bewtween many if statements and multiple if statements
With
Which of the following is a valid variable name in c++ character Garnet&Gold const 4numbers
character
What is the difference between constant variables and symbolic constants?
constant variables are variables that have specific data type and not need be changed during the duration of the program. They are stored in memory. Symbolic constants are symbolic names that are replaced with their associated values in preprocessing. Symbolic Constants do not require memory, they are more difficult in readability of the program
Which of the following is a c++ keyword? staticcast double elseif integer
double
What is a valid c++ variable A. int 123digit B. float variable-name C. double goodStudent D. bool while
double goodStudent
double sum=0; for(int=20; i<50; i+=8) { sum*sum+i; if(i%4 ==0) { i++ cout<<"i is "<< i<<endl; } what is the output of the following code snippet
i is 21 i is 29 i is 37 i is 45
Show the use of static_cast in an example
int num; cout<<static_cast<double>(num);
what are literals
values such as characters, integers, and strings
int x=39, y=30; int z = ++x *y--/4; What is the value of x and y after the code runs
x=40 y=29
Which of the following operators contains a code block {} [] () <>
{}