Vous êtes sur la page 1sur 8

Question 1

To convert temperatures written in Fahrenheit to Celsius (Centigrade), you subtract 32, multiply by 5 and then divide by 9. To convert Celsius to Absolute Value (Kelvin), you add 273.15. Write a program that displays a temperature conversion chart on the screen as follows:
Fahrenheit 0 20 40 ... 300 Celsius -17.78 -6.67 4.44 ...... 148.89 Absolute Value 255.37 266.48 277.59 ...... 422.04

Answer:
#include <iostream> using namespace std; const int LOWER = 0; const int UPPER = 300; const int STEP = 20; int main() { int fahr = LOWER; double celsius = 0; /* Print table heading */ cout.width(15); cout << "Fahrenheit"; cout.width(17); cout << "Celsius"; cout.width(20); cout << "Absolute Value" << endl << endl; /* set format of individual values */ cout.precision(2); cout.setf(ios::fixed); /* print table from LOWER to UPPER */ for (fahr = LOWER ; fahr <= UPPER ; fahr += STEP) { cout << " "; cout.width(15); cout << fahr; celsius = (static_cast<double>(5)/9) * (fahr - 32); cout.width(15); cout << celsius; cout.width(15); cout << celsius + 273.15 << endl; } return 0; }

Question 2
Alter the program for question 1 so that it prompts the user for the lowest and highest Fahrenheit temperature wanted in the table, and also prompts the user for the desired step size between rows of the table (the step size is 20 in question 1). The program should begin by explaining to the user what it does, and should appropriately echo the user's input before printing the table.

Answer:
#include <iostream> using namespace std; int main() { int fahr = 0; double celsius = 0; int lower = 0; int upper = 0; int step = 1;

/* prompt user for table specification */ cout << "This program prints out a conversion table of temperatures.\n\n"; cout << "Enter the minimum (whole number) temperature\n"; cout << "you want in the table, in Fahrenheit: "; cin >> lower; cout << "Enter the maximum temperature you want in the table: "; cin >> upper; cout << "Enter the temperature difference you want between table entries: "; cin >> step; cout << "\n\n"; /* echo the input */ cout << "Tempertature conversion table from " << lower << " Fahrenheit\n"; cout << "to " << upper << " Fahrenheit, in steps of " << step << " Fahrenheit:\n\n"; /* Print table heading */ cout.width(15); cout << "Fahrenheit"; cout.width(17); cout << "Celsius"; cout.width(20); cout << "Absolute Value\n\n"; /* set format of individual values */ cout.precision(2); cout.setf(ios::fixed); /* print table from lower to upper */ for (fahr = lower ; fahr <= upper ; fahr += step) { cout << " "; cout.width(15); cout << fahr; celsius = (static_cast<double>(5)/9) * (fahr - 32); cout.width(15); cout << celsius; cout.width(15); cout << celsius + 273.15 << endl; } return 0; }

Question 3
Write a program that reads in a character <char> from the keyboard, and then displays one of the following messages: (i) if <char> is a lower case letter, the message "The upper case character corresponding to <char> is ...", (ii) if <char> is an upper case letter, the message "The lower case character corresponding to <char> is ...", or (iii) if <char> is not a letter, the message "<char> is not a letter". You will need to refer to a table of ASCII characters.

Answer:
#include <iostream> using namespace std; int main() { char letter = 'a'; /* prompt user for letter */ cout << "This program displays upper and lower case\n"; cout << "versions of any letter typed from the keyboard.\n\n"; cout << "Type in a letter and press RETURN: "; cin >> letter; cout << "\n"; /* print the appropriate message */ if (letter >= 65 && letter <= 90) { cout << "The lower case character corresponding to '" << letter << "' is '"; cout << static_cast<char>(letter + 32) << "'.\n";// char is used here as a type cast } else if (letter >= 97 && letter <= 122) { cout << "The upper case character corresponding to '" << letter << "' is '"; cout << static_cast<char>(letter-32) << "'.\n";//again, char is used as a type cast } else cout << "'" << letter << "' is not a letter.\n"; return 0; }

Question 4

Write a program which will raise any number x to a positive power (Is there any way to improve the efficiency of your program?)

using a "for loop".

Answer:
#include <iostream> using namespace std; int main() { double number = 0, answer = 0; int power = 0, count = 1; /* prompt the user */ cout << "This program raises a number x to a non-negative integer power n.\n\n"; cout << "Please enter a number: "; cin >> number; cout << "To what power would you like it raised? "; cin >> power; cout << "\n"; /* echo the input */ cout << number << " raised to the power " << power << " is "; /* raise number to power */ answer = number; for (count = 1 ; count < power ; count++) answer *= number; /* print answer */ cout << answer << ".\n"; return 0; }

Question 5
Write a program which outputs its own C++ source file to the screen.

Answer:
#include <iostream> #include <fstream> using namespace std; int main() { char character; ifstream in_stream; in_stream.open("Sheet4Ex1.cpp"); in_stream.get(character); for ( ; ! in_stream.fail() ; ) /* alternative: while (! in_stream.eof()) */ { cout << character; in_stream.get(character); } in_stream.close(); return 0; }

Question 6
Write a program that (i) starts with the output test statement:
cout << "Testing: " << 16/2 << " = " << 4*2 << ".\n\n";

and then (ii) outputs its own C++ source file to a file called "WithoutComments.cpp" and to the screen, but omitting any of the comments enclosed in " /* ... */" markers (and omitting the markers themselves). The new program in the file "WithoutComments.cpp" should compile and run in exactly the same way as the program from which it was generated. Hints: (i) you might find the "putback()" function useful, (ii) you might want to use the idea of a "flag" to keep track of whether you're inside a comment or not.

Answer:

#include <iostream> #include <fstream> using namespace std; const int False = 0; const int True = 1; int main() { char character; char character_after; int inside_comment = False; ifstream in_stream; ofstream out_stream; /* Output test statement to the screen: */ cout << "Testing: " << 16/2 << " = " << 4*2 << ".\n\n"; in_stream.open("Sheet4Ex2.cpp"); out_stream.open("WithoutComments.cpp"); in_stream.get(character); for ( ; ! in_stream.fail() ; ) /* alternative: 'while (! in_stream.eof())' */ { /* Test to see if a comment is starting. */ if (inside_comment == False && character == '/') { in_stream.get(character_after); /* get the next character, ... */ if (character_after == '*') /* see if it's a '*', ... */ inside_comment = True; /* if so, set flag to 'True', ... */ else in_stream.putback(character_after); /* if not, put the character back. */ } /* End test */ /* Test to see if a comment is finishing. */ if (inside_comment == True && character == '*') { in_stream.get(character); if (character == '/') { inside_comment = False; /* set flag to 'False' ... */ in_stream.get(character); /* and get the first character after the comment */ } } /* End test */ /* Output the character if it's not inside a comment */ if (inside_comment == False) { out_stream.put(character); /* output to the file ... */ cout << character; /* ... and to the screen */ } /* get the next character, ready to repeat the above process */ in_stream.get(character); } in_stream.close(); out_stream.close(); return 0; }

Question 7
Write a program which counts and displays the number of characters (including blanks) in its own source code file.

Answer:
#include <iostream> #include <fstream> using namespace std; int main() { char character; char previous_character; int count = 0; ifstream in_stream; in_stream.open("Sheet4Ex3.cpp"); in_stream.get(character); for ( ; ! in_stream.fail() ; ) /* alternative: 'while (! in_stream.eof())' */

{ count++; previous_character = character; in_stream.get(character); } in_stream.close(); cout << "The total number of characters in 'Sheet4Ex3.cpp', "; cout << "is " << count << ".\n"; if (count > 1) cout << "The last character is a '" << previous_character << "'.\n"; return 0; }

Question 8
Without using an array (we will learn about arrays in Lecture 6), write a program that prints itself out backwards on the screen. Hints: (i) the answer to question 3 might help at the start of your program, (ii) be careful not to re-use an input stream once an operation has failed on it - use a new stream instead. Remark:don't worry if your program takes a while before it prints to the screen - opening and closing files takes time.

Answer:
#include <iostream> #include <fstream> using namespace std; int main() { char character; char previous_character; int count = 0; ifstream in_stream; in_stream.open("Sheet4Ex3.cpp"); in_stream.get(character); for ( ; ! in_stream.fail() ; ) /* alternative: 'while (! in_stream.eof())' */ { count++; previous_character = character; in_stream.get(character); } in_stream.close(); cout << "The total number of characters in 'Sheet4Ex3.cpp', "; cout << "is " << count << ".\n"; if (count > 1) cout << "The last character is a '" << previous_character << "'.\n"; return 0; }

Question 9
What screen output does the following program produce, and why?
#include <iostream> #include <fstream> using namespace std; int main() { char character; int integer; ofstream out_stream; ifstream in_stream; /* Create a file containing two integers */ out_stream.open("Integers"); out_stream << 123 << ' ' << 456; out_stream.close(); /* Attempt to read a character, then an integer, then a character again, then an integer again, from the file "Integers" just created. */ in_stream.open("Integers"); in_stream >> character >> integer; cout << "character: '" << character << "'\n"; cout << "integer: " << integer << "\n"; in_stream >> character >> integer;

cout << "character: '" << character << "'\n"; cout << "integer: " << integer << "\n"; in_stream.close(); return 0; }

Answer:
#include <iostream> #include <fstream> using namespace std; int main() { char character; int integer; ofstream out_stream; ifstream in_stream; /* Create a file containing two integers */ out_stream.open("Integers"); out_stream << 123 << ' ' << 456; out_stream.close(); /* Attempt to read a character, then an integer, then a character again, then an integer again, from the file "Integers" just created. */ in_stream.open("Integers"); in_stream >> character >> integer; cout << "character: '" << character << "'\n"; cout << "integer: " << integer << "\n"; in_stream >> character >> integer; cout << "character: '" << character << "'\n"; cout << "integer: " << integer << "\n"; in_stream.close(); return 0; }

Question 10
Write a logical (i.e. Boolean) valued function which takes a single integer parameter, and returns "True" if and only if the integer is a prime number between 1 and 1000 (note that 1 is not usually counted as a prime number). Test your function by calling it from an appropriate "driver" program with different inputs. Hints: (i) if a number is not prime, it has at least one prime factor less than or equal to its square root, (ii) (32 x 32) = 1024 and 1024 > 1000.

Answer:
#include <iostream> using namespace std; enum Logical {False, True}; int get_valid_number(); Logical small_prime(int integer); /* START OF MAIN PROGRAM */ int main() { int number; cout << "This program tests to see if an integer\n"; cout << "is a prime between 1 and 1000.\n\n\n"; number = get_valid_number(); while (number != 0) { cout << "The number " << number << " is "; if (!small_prime(number)) cout << "not "; cout << "a prime between 1 and 1000.\n\n"; number = get_valid_number(); } return 0; } /* END OF MAIN PROGRAM */ /* FUNCTION TO READ IN A NUMBER FROM 0 TO 1000 */ int get_valid_number() { int number; do { cout << "Enter an integer between 1 and 1000 (incl) (or 0 to end program): "; cin >> number; if (number < 0 || number > 1000)

cout << "number out of range, try again!" << endl; } while (number < 0 || number > 1000); return number; } /* FUNCTION TO EVALUATE IF THE ARGUMENT IS A PRIME BETWEEN 1 AND 1000 */ Logical small_prime(int integer) { for (int factor = 2; factor<integer; factor++) { if ((integer % factor) == 0) return False; } return True; }

Question 11
Write a function "print_pyramid(...)" which takes a single integer argument " height" and displays a "pyramid" of this height made up of of "*" characters on the screen. Test the function with a simple "driver" program, which should be able to reproduce the following example output:
This program prints a 'pyramid' shape of a specified height on the screen. how high would you like the pyramid?: 37 Pick another height (must be between 1 and 30): 6 ** **** ****** ******** ********** ************

Answer:
#include <iostream> using namespace std; void print_pyramid(int height); /* START OF MAIN PROGRAM */ int main() { int pyramid_height; cout << "This program prints a 'pyramid' shape of\n"; cout << "a specified height on the screen.\n\n"; /* input with check using a "while" loop */ cout << "how high would you like the pyramid?: "; cin >> pyramid_height; while (pyramid_height > 30 || pyramid_height < 1) { cout << "Pick another height (must be between 1 and 30): "; cin >> pyramid_height; } /* input OK */ print_pyramid(pyramid_height); return 0; } /* END OF MAIN PROGRAM */ /* FUNCTION TO PRINT PYRAMID */ void print_pyramid(int height) { int line; int const MARGIN = 10; cout << "\n\n"; for (line = 1 ; line <= height ; line++) { int count; int total_no_of_spaces = MARGIN + height - line; for (count = 1 ; count <= total_no_of_spaces ; count++) cout << ' '; for (count = 1 ; count <= line * 2 ; count++) cout << '*'; cout << '\n'; } cout << "\n\n";}/*END OF FUNCTION */

Q1
Write a program that get 3 numbers from the user and calculate the average of there float number and display them?

Q2
Write a program that get 3 numbers from the user and display the greatest one?

ARRAY
Q3
using array - write programe to get 4 numbers from the user into an array and then display all the elements that that add to the array

Q4
write program to creat 3 arrays the 1rst called arr1, the 2nd called arr2 and the 3rd called arr3 the first content the numbers between 0 and 9 and then read the arr1 to fill arr2 by the odd numbers in arr1and even numbers in arr2 then print arr2 and arr3 .

STRUCT
Q5
creat struct called student to store id , name , class , and phone number of the student and then fill the struct

Q6
creat 2 struct one for student detail which content his id and name and the other is for his grade which content subject name and the grade that he get....

CLASS
Q7
write a programe to creat struct called student to store id , name and phone number of the student and then fill the struct.

Q8
write a programe to creat class called student to store id , name and phone number of the student Ass public object and then fill the class.

Vous aimerez peut-être aussi