Assignment 5
1. Sum of Numbers
#include <iostream>
using namespace std;
int main(){
int x, sum=0, i;
cout<<" Enter the user for a positive integer value: ";
cin >> x;
while(x<=0){
cout<<"Do not accept a negative starting number";
cin >> x;
for (i=1; i <= x; ++i){
sum+=i;
cout<<" The sum of all the integers from 1 up to the number entered "<<x<<" is "<<sum;
return 0;
2. Characters for the ASCII Codes
#include <iostream>
using namespace std;
int main(){
int i;
cout<<" the characters for the ASCII codes 0 through 127: ";
for(i=0; i<=127; i++){
cout<<static_cast<char>(i)<<" ";
if ((i + 1) % 16 == 0){
cout << endl;
}}
return 0;
3. Ocean Levels
#include <iostream>
using namespace std;
int main(){
int i;
float total;
cout<<"Year\tOcean Level (mm)\n";
for(i =1; i<=25; i++){
total=1.5*i;
cout << i << "\t" << total<< "\n";
return 0;
4. Distance Traveled
#include <iostream>
using namespace std;
int main(){
int distance, speed, time, i;
cout<<" What is the speed of the vehicle (in mph)? ";
cin >> speed;
cout<<" How many hours has it traveled? ";
cin >> time;
while(speed<=0 & time<1){
cout<<" Do not accept a negative number for speed and do not accept any value less than 1 for time
traveled. ";
cin >>speed>>time;
cout<<" Hour Distance Traveled "<<endl;
cout<<" ----------------------------- "<<endl;
for (i = 1; i <= time; i++){
distance = speed*i;
cout<< i << "\t"<< distance << "\n";
return 0;
5. Population Bar Chart
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream Inputfile;
Inputfile.open("People.txt");
if (!Inputfile)
cout << "Error opening file.\n";
int i, asterisks, population;
cout<<"PRAIRIEVILLE POPULATION GROWTH"<<endl;
cout<<"(each * represents 1,000 people)"<<endl;
for(i=1; i<=6; i++){
switch (i){
case 1: cout<<"1990 ";
break;
case 2 : cout << "1920 ";
break;
case 3 : cout << "1940 ";
break;
case 4 : cout << "1960 ";
break;
case 5 : cout << "1980 ";
break;
case 6 : cout << "2000 ";
break;
Inputfile>>population;
population /=1000;
for (int asterisks = 1; asterisks <= population; asterisks++)
cout<<"*";
cout << endl;
}
Inputfile.close();
return 0;
6, Random Number Guessing Game
a.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
int Random_Number_Guessing_Game=rand() % 50 + 1,
number;
do{
cout <<" Asks the user to guess what the number(1-50): ";
cin >> number;
if(number<Random_Number_Guessing_Game){
cout<< " Too low, try again. Guess another number: "<<endl;
}else if (number>Random_Number_Guessing_Game){
cout<<" Too high, try again. Guess another number: "<<endl;
}while( number != Random_Number_Guessing_Game);
cout<<" Exactly! ";
return 0;
b.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
int Random_Number_Guessing_Game=rand() % 50 + 1,
number,
number_of_guesses = 0;
do{
cout <<" Asks the user to guess what the number(1-50): ";
cin >> number;
number_of_guesses++;
if(number<Random_Number_Guessing_Game){
cout<< " Too low, try again."<<endl;
}else if (number>Random_Number_Guessing_Game){
cout<<" Too high, try again."<<endl;
}while( number != Random_Number_Guessing_Game);
cout<<" Exactly! "<<endl;
cout<<"Number of guesses: " << number_of_guesses ;
return 0;
7. Sales Bar Chart
#include <iostream>
using namespace std;
int main(){
int i, sale_store1, sale_store2, sale_store3, sale_store4, sale_store5, asterisks, salestore;
cout << " Here is an example of the program’s output. "<<endl;
cout << " Enter today's sales for store 1: ";
cin >> sale_store1;
cout << " Enter today's sales for store 2: ";
cin >> sale_store2;
cout << " Enter today's sales for store 3: ";
cin >> sale_store3;
cout << " Enter today's sales for store 4: ";
cin >> sale_store4;
cout << " Enter today's sales for store 5: ";
cin >> sale_store5;
cout << " SALES BAR CHART "<<endl;
cout << " (Each * = $100) "<<endl;
for(i=1; i<=5; i++){
switch (i){
case 1 : salestore = sale_store1 ;
break;
case 2 : salestore = sale_store2;
break;
case 3 : salestore = sale_store3;
break;
case 4 : salestore = sale_store4;
break;
case 5 : salestore = sale_store5;
break;
cout << "Store " << i << ": ";
for ( asterisks=1; asterisks <= salestore/100; asterisks++)
cout <<"*";
cout << endl;
return 0;
8. The Greatest and Least of These
#include <iostream>
using namespace std;
int main(){
int numbers, smallest = INT_MAX, largest = INT_MIN;
cout<<" Allows users to enter a range of integers "<<endl;
while (true){
cout<<" A range of integers: ";
cin >> numbers;
if (numbers == -99)
break;
if ( numbers < smallest )
smallest = numbers ;
if ( numbers > largest )
largest = numbers;
cout << "Smallest Number: " << smallest << endl;
cout << "Largest Number: " << largest << endl;
return 0;
9. Square Display
#include <iostream>
using namespace std;
int main(){
int i,j, numbers;
cout<<" Asks the user for a positive integer no greater than 15: ";
cin >> numbers;
if (numbers>15){
cout<<" Please enter another number! ";
return 0;
}
if (numbers <= 1 & numbers >= 15){
cout << numbers; }
for(i=0; i < numbers; i++){
for( j=0; j <numbers; j++)
cout<<"X";
cout<<endl;
return 0;
10. Pattern Displays