CSC 212 Problems For Chapter 2 with Solution, Summer 2013

Notre Dame University–Louaize · Program Design and Data Abstraction I · 21 pages · with solution

All CSC 212 files
CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 1

Stuck on a question? A tutor can walk you through it.

CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 2 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 3 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 4 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 5 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 6 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 7 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 8 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 9 CSC 212 Problems For Chapter 2 with Solution, Summer 2013, page 10

Showing the first 10 of 21 pages. Download the PDF to see all of it.

More CSC 212 files

Show the text of this chapter
// Write a C++ program that checks if an entered integer is prime or not. // An integer is said to be prime if it is divisible by only 1 and itself. #include <iostream.h> int main() { int Number, Flag = 0; cout << "Enter a number: "; cin >> Number; if ( Number <= 1 ) { cout << Number << " is not a prime number" << endl; } else { for ( int i = 2; i <= Number / 2 ; i++ ) { if ( ( Number % i ) == 0 ) { cout << Number << " is not a prime number" << endl; Flag = 1; break; } } if ( Flag == 0 ) { cout << Number << " is a prime number" << endl; } } return 0; } /* A sample screen output look like: Enter a number: 23 23 is a prime number Another sample screen output look like: Enter a number: 16 16 is not a prime number */ // An integer is said to be prime if it is divisible by only 1 and itself. // Write a program that prints all prime number from 1 up to a number you enter. #include<iostream.h> int main() { int Number, Flag = 0; cout << "Enter a positive number: "; cin >> Number; cout << endl; cout << "The prime numbers from 1 to " << Number << " are:" << endl; for ( int i = 2; i <= Number; i++ ) { Flag = 0; for ( int j = 2; j <= i / 2; j++ ) { if ( ( i % j ) == 0 ) { Flag = 1; } } if ( Flag == 0 ) { cout << i << endl; } } return 0; } /* A sample screen output look like: Enter a number: 23 The prime numbers from 1 to 23 are: 2 3 5 7 11 13 17 19 23 */ Exercice Chapter 2 1 Problems Write a C++ program that inputs a series of 10 numbers, and determines and prints the largest of the numbers. #include <iostream.h> int main() { int counter = 0, number, largest; cout << "Enter the first number: "; cin >> largest; while(++counter < 10){ cout << "Enter the next number : "; cin >> number; if(number > largest){ largest = number; } } cout << "Largest is " << largest << endl; return 0; } Sample Output: Exercice Chapter 2 2 Exercice Chapter 2 3 The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n * (n-1) * (n-2) * ... * 1 and n! = 1 for n = 0 For example 5! = 5 * 4 * 3 * 2 * 1 which is 120 Write a program that reads a nonnegative integer and computes and prints its factorial. #include <iostream.h> int main() { int n = 0, number; unsigned factorial = 1; do { cout << "Enter a positive integer: "; cin >> number; } while(number < 0); while(n++ < number){ factorial = factorial * n; } cout << number << "! is " << factorial << endl; return 0; } Sample Output: Exercice Chapter 2 4 Write a program that evaluates the factorials of the integers from 1 to 5. Print the results in tabular format. #include <iostream.h> int main() { int factorial; cout << "Number \t Factorial of number \n"; for(int i = 1; i <= 5; ++i){ factorial = 1; for(int j = 1; j <= i; ++j) factorial = factorial * j; cout << i << '\t' << factorial << '\n'; } cout << endl; return 0; } Sample output: Exercice Chapter 2 5 Write a C++ program that utilizes looping and the tab escape “\t” to print the following table of values: N 10*N 100*N 1000*N 1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000 #include <iostream.h> int main() { int n = 0; cout << "N" << "\t" << "10*N" << "\t" << "100*N" << "\t" << "1000*N" << "\n"; while(++n <= 5){ cout << n << '\t' << 10*n << '\t' << 100*n << '\t' << 1000*n << '\n'; } cout << endl; return 0; } Sample Output: Exercice Chapter 2 6 Write a program that reads in the size of the side of a square and then prints a hollow square of that size out of asterisks and blanks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 4, it should print: * * * * * * * * * * * * #include <iostream.h> int main() { int row, col, size; cout << "Enter the size of the square’s side:"; cin >> size; row = size; while(row > 0) { col = size; while(col > 0) { if(size == row || row == 1 || col == 1 || col == size) { cout << '*'; } else { cout << ' '; } --col; } cout << '\n'; --row; Exercice Chapter 2 7 } cout << endl; return 0; } Sample Output: Exercice Chapter 2 8 Write a program that uses repetition and switch structures to print the song “The Twelve Days of Christmas.” One switch structure should be used to print the day (i.e., “First,” “Second,” etc.). A separate switch structure should be used to print the remainder of each verse. #include <iostream.h> int main() { for(int day = 1; day < 13; day++){ cout << "On the "; switch(day){ // switch for current day case 1: cout << "first"; break; case 2: cout << "second"; break; case 3: cout << "third"; break; case 4: cout << "fourth"; break; case 5: cout << "fifth"; break; case 6: cout << "sixth"; break; case 7: cout << "seventh"; break; case 8: cout << "eighth"; Exercice Chapter 2 9 break; case 9: cout << "ninth"; break; case 10: cout << "tenth"; break; case 11: cout << "eleventh"; break; case 12: cout << "twelfth"; break; } cout << " day of Christmas,\n"; cout << " My true love sent to me:\n"; switch(day){ // switch for gifts case 12: cout << "\t Twelve drummers drumming, \n"; case 11: cout << "\t Eleven pipers piping, \n"; case 10: cout << "\t Ten lords a-leaping, \n"; case 9: cout << "\t Nine ladies dancing, \n"; case 8: cout << "\t Eight maids a-milking, \n"; case 7: cout << "\t Seven swans a-swimming, \n"; case 6: cout << "\t Six geese a-laying, \n"; case 5: cout << "\t Five golden rings, \n"; case 4: Exercice Chapter 2 10 cout << "\t Four calling birds, \n"; case 3: cout << "\t Three French hens, \n"; case 2: cout << "\t Two turtle doves, and \n"; case 1: cout << "A partridge in a pear tree.\n\n"; } } cout << endl; return 0; } Sample Output: Exercice Chapter 2 11 Write a program that prints a diamond shape. You may use output statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for structures) and minimize the number of output statements. #include <iostream.h> int main() { // top half of the diamond for(int row = 1; row <= 5; ++row){ for(int space = 1; space <= 5 - row; ++space) cout << ' '; for(int ast = 1; ast <= 2 * row - 1; ++ast) cout << '*'; cout << '\n'; } // bottom half of the diamond for(row = 4; row >= 1; --row){ for(int space = 1; space <= 5 - row; ++space) cout << ' '; for(int ast = 1; ast <= 2 * row - 1; ++ast) cout << '*'; cout << '\n'; } cout << endl; return 0; } Sample Output: Exercice Chapter 2 12 Exercice Chapter #2 1 What is the output of the following program? #include <iostream.h> void main () { int x = 1; for ( ; ; ) { if ( (x++ % 15) == 0 ) break; if ( (x % 5) != 0 ) continue; cout << x << " "; } cout << endl; } Answer: 5 10 15 If we had: if ( (++x % 15) == 0) then answer is: 5 10 What is the output of the following program? #include <iostream.h> void main () { int x = 10; while ( x ) { if ( --x == 5 ) break; cout << x << " "; } cout << endl; } Answer: 9 8 7 6 If we had: if ( x-- == 5 ) then answer is: 9 8 7 6 5 Exercice Chapter #2 2 What is the output of the following program? #include <iostream.h> void main () { int x = 10; while ( x ) cout << --x << “ “; cout << endl; } Answer: 9 8 7 6 5 4 3 2 1 0 If we had: cout << x-- << " "; then answer is: 10 9 8 7 6 5 4 3 2 1 What is the output of the following program? #include <iostream.h> void main () { int j; for ( j = 1; j <= 10; j++ ) { if ( (j > 5) && (j <= 7) ) cout << j << endl; } cout << endl; } Answer: 6 7 Exercice Chapter #2 3 What is the output of the following program? #include <iostream.h> void main () { int j; for ( j = 1; j <= 10; j++ ) { if ( (j < 5) || (j >= 7) ) cout << j << endl; } cout << endl; } Answer: 1 2 3 4 7 8 9 10 What is the output of the following program? #include <iostream.h> void main () { int j; for ( j = 1; j <= 10; j++ ) { if ( !((j < 5) || (j >= 7)) ) cout << j << endl; } cout << endl; } Answer: 5 Exercice Chapter #2 4 6 What is the output of the following program? #include <iostream.h> void main () { int i, j, k; for ( i = 1; i <= 2; i++ ) for ( j = 1; j <= 2; j++ ) for ( k = 1; k <= 3; k++ ) cout << k << " "; cout << endl; } Answer: 1 2 3 1 2 3 1 2 3 1 2 3 What is the output of the following program? #include <iostream.h> int main () { int count = 0; while ( count++ <= 5 ) cout << ( count % 2 ? "***" : "---" ) << endl; cout << count << endl; return 0; } Answer: *** --- *** --- *** --- Exercice Chapter #2 5 7 What is the output of the following program? #include <iostream.h> int main () { int j = 10, b = 5; for ( ; b <= j; j -= 1 ) { if ( (j % b == 0) && (j * 5 <= 25) ) continue; if ( (j < 7) || (j >= 9) ) cout << j << endl; } return 0; } Answer: 10 9 6 What is the output of the following program? #include <iostream.h> void main () { for ( int i = 45; i > 1; i = i / 5 ) cout << i++ << endl; cout << "continue"; } Answer: 45 9 2 continue Exercice Chapter #2 6 What is the output of the following program? #include <iostream.h> void main () { int i, j, k; int x = 0; for ( i = 1; i <= 5; i++ ) for ( j = 1; j <= 7; j += 2 ) for ( k = 1; k < 15; k += 3 ) x++; cout << x << endl; } Answer: 100 What is the output of the following program? #include <iostream.h> void main () { int x; for ( x = 1; x <= 5; x++ ) { switch ( x ) { case 1: cout << x << endl; case 2: cout << x << endl; case 3: Exercice Chapter #2 7 cout << x << endl; break; default: cout << x + 1 << endl; } } } Answer: 1 1 1 2 2 3 5 6