DOC PREVIEW
CSUN COMP 106 - Homework Solutions

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoEngineering Building Room 2303 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062March 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 3Homework Solutions – March 14, 2006Page 308, Program 7 – Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage) and the number of days they will multiply. A loop should display the size of the populationfor each day.Input validation: Do not accept a number less than two for the starting size of the population. Do not accept a negative number for the daily population increase, Do not accept a number less than one for the number of days they will multiply#include <iostream>#include <iomanip>using namespace std;int main(){// Program to compute organism growth based on initial population, // growth rate and total simulated days input by the user.// Get input for the initial polulation of organisms. Use a while// loop for data validataion that the population >= 2 int organisms; cout << "Programs to compute organism growth\n\n"; do { cout << "Enter the initial number of organisms(>=2): "; cin >> organisms; if ( organisms < 2 ) { cout << "\n*** ERROR ***initial number must be >= 2." << "\nYou entered " << organisms << ". Reenter data.\n"; } } while ( organisms < 2 );// Get input for the total days to be simulated. Use a while// loop for data validataion that the total days are >= 2 int days; do { cout << "Enter the total days for the simulation(>=1): "; cin >> days; if ( days < 1 ) { cout << "\n*** ERROR ***total days must be >= 1." << "\nYou entered " << days << ". Reenter data.\n"; } } while ( days < 2 );// Get input for the growth rate in percent. Use a while// loop for data validataion that growth rate is positiveMarch 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 4 double growth; do { cout << "Enter the daily percentage growth rate: "; cin >> growth; if ( growth < 0 ) { cout << "\n*** ERROR ***growth rate must be > zero." << "\nYou entered " << growth << ". Reenter data.\n"; } } while ( growth < 0 );// Start calculations. Convert growth rate to a fraction, // print header, initialize counter and set maximum integer. growth /= 100; cout << " Day Number of organisms\n"; int day = 0; const double MAX_INT = 2147483647;// Loop to compute growth rate. Make sure that we do not // exceed the maximum integer count. do { cout << setw(4) << day << setw(20) << organisms << endl;if ( (1 + growth) * organisms > MAX_INT ){ cout << "\nReached limit for integer organisms."; cout << "\nProgram will halt now.\n\n"; return EXIT_FAILURE; } organisms *= (1 + growth); day++; } while ( day <= days ); return EXIT_SUCCESS;}Page 308, Program 9 – Write a program with a loop that lets a user enter a series of integers. The user should enter -999999 to signal the end of the series. After allthe numbers have been entered the program should display the largest and smallest numbers entered.#include <iostream>using namespace std;int main(){ // Program to find max and min in a set of user inputint const exit = -999999; // Number to exit input processint data; // Individual data entry// Tell user about program and get initial inputcout << "Enter a series of numbers.\n"March 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 5 << "The program will find the maximum and minimuim.\n" << "Enter " << exit << " to end input.\n" << "\nEnter your first number: ";cin >> data; // Declare variables min and max to hold the current minimum // and maximum values. Initialize these values to the first // data point entered by the user.int min = data;int max = data;// Loop over repeated user entrywhile ( data != exit ){cout << "Enter your next number (" << exit << " to end input): "; cin >> data; // If user enters code to quit input leave loop // Otherwise see if the new data entry has to // replace the current max or min.if ( data == exit ) break;if ( data > max ) max = data;else if ( data < min ) min = data;}if ( max == exit && min == exit ){// This condition obtains when the user entered the code to// leave the input process before entering any data.cout << "\nNo data entered.\n\n";}else{// Print the minimum and maximum values found in the loop. cout << "\n\nThe minimum value is " << min << ", and" << "\nThe maximum value is " << max << endl << endl;}return


View Full Document

CSUN COMP 106 - Homework Solutions

Download Homework Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?