COSC 2425- Program Set #1
COSC 2425 S26
1
Professor C’s Help and Hints: Program Set 1 Notes:
• I cannot provide all possible solutions in each of the required languages- and not required to do so. Any language solution I provide can easily be adapted to another language. At this point in your academic career, you should be able to understand problems and modify code.
• Remember the Program and Grading Guidelines handouts as well. Review those before you turn in your assignments to Canvas.
• There are many possible ways to solve problems. These are a few ways I recommend solving the problems.
General Help:
• Look under the assignment for samples on how to read a filename from the keyboard.
Input validation (code snippet):
cout << "How many players do you wish per team? "; cin >> teamPlayers; // Validate the input. while (teamPlayers < MIN_PLAYERS || teamPlayers > MAX_PLAYERS) { // Explain the error. cout << "You should have at least " << MIN_PLAYERS << " but no more than " << MAX_PLAYERS << " per team.\n"; // Get the input again. cout << "How many players do you wish per team? "; cin >> teamPlayers; } cout << "How many players are available? "; cin >> players; // Validate the input. while (players <= 0) { // Get the input again. cout << "Please enter 0 or greater: "; cin >> players; } Run again (code snippet)
char again; // To hold Y or N input do { // Get three scores. cout << "Enter 3 scores and I will average them: "; cin >> score1 >> score2 >> score3;
COSC 2425 S26
2
// Calculate and display the average. average = (score1 + score2 + score3) / 3.0; cout << "The average is " << average << ".\n"; // Does the user want to average another set? cout << "Do you want to average another set? (Y/N) "; cin >> again; } while (again == 'Y' || again == 'y');
Problem Specific:
Ten Complement • Work out the sample data by hand and verify to understand how you are getting that
answer. ASCII Convert
• Work out the sample data by hand and verify to understand how you are getting that answer.
• ASCII information is discussed in your textbook. • Remember to format the output as per the directions 8 values per line.
Extra Credit: Rounding
• Work out the sample data by hand and verify to understand how you are getting that answer.
• Remember examples are provided on how to read a text file from the keyboard. • Round each value to the appropriate number of places before you add all up. • Also, make sure you can handle rounding to 1, 2 or 3 decimal places depending on the
data file used. • Here is some partial Java code to help with the problem:
public static float sum(BufferedReader br) throws IOException { String line = br.readLine(); int precision = Integer.parseInt(line); float sum = 0; while(line != null) { line = br.readLine(); //for the last line if(line == null) { continue; }
COSC 2425 S26
3
sum += Math.round(Float.parseFloat(line) * Math.pow(10, precision))/Math.pow(10, precision); } return sum; }
- Professor C’s Help and Hints: Program Set 1