Difference: CodeDay5 (1 vs. 7)

Revision 72016-09-13 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Line: 273 to 273
  return 0; } %ENDCODE%
Changed:
<
<

Prime 1

>
>

Switch Statement (grades)

  %CODE{"c++"}%
Added:
>
>
DISPLAY 3.6 A switch Statement //Program to illustrate the switch statement. #include using namespace std;

int main( ) { char grade;

cout << "Enter your midterm grade and press Return: "; cin >> grade;

switch (grade) { case 'A': cout << "Excellent. " << "You need not take the final.\n"; break; case 'B': cout << "Very good. "; grade = 'A'; cout << "Your midterm grade is now " << grade << endl; break; case 'C': cout << "Passing.\n"; break; case 'D': case 'F': cout << "Not good. " << "Go study.\n"; break; default: cout << "That is not a possible grade.\n"; }

cout << "End of program.\n"; return 0; } %ENDCODE%

A Menu

<-- SyntaxHighlightingPlugin -->
//DISPLAY 3.7 A Menu 
//Program to give out homework assignment information.
#include <iostream>
using namespace std;

 
int main( )
{
    int choice;

    do
    {
        cout << endl
             << "Choose 1 to see the next homework assignment.\n"
             << "Choose 2 for your grade on the last assignment.\n"
             << "Choose 3 for assignment hints.\n"
             << "Choose 4 to exit this program.\n"
             << "Enter your choice and press Return: ";
        cin >> choice;
 
        switch (choice)
        {
            case 1:
               //code to display the next assignment on screen would go here.
                break;
            case 2:
               //code to ask for a student number and give the corresponding 
               //grade would go here.
                break;
            case 3:
               //code to display a hint for the current assignment would go 
               //here.
                break;
            case 4:
                cout << "End of Program.\n";
                break;
            default:
                  cout << "Not a valid choice.\n"
                     << "Choose again.\n";
        }
    }while (choice != 4);

    return 0;
}
<-- end SyntaxHighlightingPlugin -->

Local Variables

<-- SyntaxHighlightingPlugin -->
//DISPLAY 3.8 Block with a Local Variable
//Program to compute bill for either a wholesale or a retail purchase.
#include <iostream>
using namespace std;
const double TAX_RATE = 0.05; //5% sales tax.


int main( )
{
    char sale_type;
    int number;
    double price, total;

    cout << "Enter price $";
    cin >> price;
    cout << "Enter number purchased: ";
    cin >> number;
    cout << "Type W if this is a wholesale purchase.\n"
         << "Type R if this is a retail purchase.\n"
         << "Then press Return.\n";
    cin >> sale_type;

    if ((sale_type = = 'W') || (sale_type = = 'w'))
    {
        total = price * number;
    }
    else if ((sale_type = = 'R') || (sale_type = = 'r'))
    {
        double subtotal;
        subtotal = price * number;
        total = subtotal + subtotal * TAX_RATE;
    }
    else
    {
        cout << "Error in input.\n";
    }
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << number << " items at $" << price << endl;
    cout << "Total Bill = $" << total;
    if ((sale_type = = 'R') || (sale_type = = 'r'))
        cout << " including sales tax.\n";

    return 0;
}
<-- end SyntaxHighlightingPlugin -->

For Statement

%CODE{"c++"}% //DISPLAY 3.12 A for Statement //Illustrates a for loop. #include using namespace std;

int main( ) { int sum = 0;

for (int n = 1; n <= 10; n++) //Note that the variable n is a local sum = sum + n; //variable of the body of the for loop!

cout << "The sum of the numbers 1 to 10 is " << sum << endl; return 0; }

  %ENDCODE%
Added:
>
>

break Statement

%CODE{"c++"}% //DISPLAY 3.14 A break Statement in a Loop //Sums a list of ten negative numbers. #include using namespace std;

int main( ) { int number, sum = 0, count = 0; cout << "Enter 10 negative numbers:\n";

while (++count <= 10) { cin >> number;

if (number >= 0) { cout << "ERROR: positive number" << " or zero was entered as the\n" << count << "th number! Input ends " << "with the " << count << "th number.\n" << count << "th number was not added in.\n"; break; }

sum = sum + number; }

 
Changed:
<
<

Prime 1

>
>
cout << sum << " is the sum of the first " << (count - 1) << " numbers.\n";

return 0; } %ENDCODE%

Explicitly Nested Loops

<-- SyntaxHighlightingPlugin -->
//DISPLAY 3.15 Explicitly Nested Loops
//Determines the total number of green-necked vulture eggs
//counted by all conservationists in the conservation district.
#include <iostream>
using namespace std;

void instructions ();

int main()
{
cout << "This program tallies conservationist reports\n"
         << "on the green-necked vulture.\n"
         << "Each conservationist's report consists of\n"
         << "a list of numbers. Each number is the count of\n"
         << "the eggs observed in one๎ 
         << "green-necked vulture nest.\n"
         << "This program then tallies 
         << "the total number of eggs.\n";

    int number_of_reports;
    cout << "How many conservationist reports are there? ";
    cin >> number_of_reports;

    int grand_total = 0, subtotal, count;
    for (count = 1; count <= number_of_reports; count++)
    {
        cout << endl << "Enter the report of "
             << "conservationist number " << count << endl;
        cout << "Enter the number of eggs in each nest.\n"
             << "Place a negative integer at the end of your list.\n๎;๎
        subtotal = 0;
       int next;
        cin >> next;
        while (next >=0)
        {
            subtotal = subtotal + next;
            cin >> next;
        }
        cout << "Total egg count for conservationist "
             << " number " << count << " is " 
             << subtotal << endl;
        grand_total = grand_total + subtotal;
    }
    cout << endl << "Total egg count for all reports = "
         << grand_total << endl;

    return 0;
}

<-- end SyntaxHighlightingPlugin -->

x

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

x

  %CODE{"c++"}%

Revision 62016-09-13 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Line: 249 to 248
  cout << "\nDone listing primes less than or equal to " << max << ".\n"; } %ENDCODE%
Changed:
<
<

Prime 1

>
>

Find Factors

  %CODE{"c++"}%
Added:
>
>
#include #include <math.h> using namespace std;
 
Added:
>
>
int main() { int n; bool prime = true; cout << "Enter a number to find it's factors:"; cin >> n; cout << "Factors: "; for (int i = 2; i<= sqrt(n) ; i++) { if (n%i == 0) { cout << i << " "; prime = false; } } if (prime) { cout << "Prime, no factor other then 1 and " << n << endl; } return 0; }
 %ENDCODE%

Prime 1

Revision 52016-09-08 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Added:
>
>

Using do-while to repeat program code

<-- SyntaxHighlightingPlugin -->
int main() {
    int x, y;
    char done;
    do {
        cout << "Input two numbers:";
        cin >> x >> y;
        cout << "Sum:" << x + y << endl;

        cout << "Do again? (Type y to repeat, anything else to stop):";
        cin >> done;
    } while (done == 'y');
    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->
 

Prime 1 - not quite right

%CODE{"c++"}%

Revision 42015-09-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Revision 32015-09-10 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Line: 134 to 134
 } %ENDCODE%
Changed:
<
<

Prime 1

>
>

Prime with break

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 
using namespace std;
 
int main() {
    int num,count=0;
    bool prime = true;
    cout<<"Enter the number to check : ";
    cin>>num;
 
        if (num > 1) {
        for(int i=2;i<num;i++){
                if(num%i==0) {       // checks if the number is fully divisible or not
                prime = false;
                break;
                }
        }
        if(prime) {
                cout<<num << " is a Prime Number \n";
        } else {
                        cout<<num << " is not a Prime Number \n";
        }
    } else {
            cout << num << " is not prime by definition";
    }
 
}
<-- end SyntaxHighlightingPlugin -->

Prime using square root

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 
using namespace std;
#include <math.h>
 
int main() {
    int num,count=0;
    bool prime = true;
    cout<<"Enter the number to check : ";
    cin>>num;
    int max = sqrt(num);
 
 	if (num > 1) {
    	for(int i=2;i<=max;i++){
        	if(num%i==0) {       // checks if the number is fully divisible or not
            	prime = false;
            	break;
        	}
    	}
    	if(prime) {
        	cout<<num << " is a Prime Number \n";
    	} else {
                cout<<num << " is not a Prime Number \n";
    	}
     } else {
 	cout << num << " is not prime by definition";
     }
 
} 
<-- end SyntaxHighlightingPlugin -->

Prime list

  %CODE{"c++"}%
Added:
>
>
#include
 
Added:
>
>
using namespace std; #include <math.h>

int main() { int max,count=0; bool prime = true; cout<<"Enter the maximum number to check : "; cin>>max;

for (int j=2 ; j<=max; j++) { prime = true; for(int i=2;i<= sqrt(j);i++){ if(j%i==0) { prime = false; break; } } if(prime) { cout << j << " "; } } cout << "\nDone listing primes less than or equal to " << max << ".\n"; }

 %ENDCODE%
Added:
>
>

Prime 1

%CODE{"c++"}%
 
Added:
>
>
%ENDCODE%
 

Prime 1

%CODE{"c++"}%

Revision 22015-09-10 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Day 5 Sample code

Line: 81 to 81
  %ENDCODE%
Changed:
<
<

Prime 1

>
>

Prime with multiway

  %CODE{"c++"}%
Added:
>
>
#include using namespace std;

int main() { int num,count=0; cout<<"Enter the number to check : "; cin>>num;

int i = 2; while (i<num && num%i!=0){ i++; } if (num<2) { cout << num << " is not prive by definition"; } else if (i==num) { cout << num <<" is a Prime Number \n"; } else { cout<<"It is not a Prime Number \n"; cout<<"The Number " << num << " is divisable by " << i << "\n"; } }

 %ENDCODE%

Prime with for statement

Line: 95 to 117
  int main() { int num,count=0;
Added:
>
>
bool prime = true;
  cout<<"Enter the number to check : "; cin>>num;

for(int i=2;i<num;i++){ if(num%i==0) { // checks if the number is fully divisible or not

Changed:
<
<
count++;
>
>
prime = false;
  } }
Changed:
<
<
if(count==0) {
>
>
if(prime) {
  cout<<"It is a Prime Number \n"; } else { cout<<"It is not a Prime Number \n";

Revision 12015-09-09 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Day 5 Sample code

Prime 1 - not quite right

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 
using namespace std;
 
int main() {
    int num,count=0;
    cout<<"Enter the number to check : ";
    cin>>num;
    int i = 2;
    while (i<num){
        if(num%i==0) {       // checks if the number is fully divisible or not
            count++;
        }
        i++;
    }
    if(count==0) {
        cout<<"It is a Prime Number \n";
    } else {
 cout<<"It is not a Prime Number \n";
    }
}
 
<-- end SyntaxHighlightingPlugin -->

Prime correct (for 1)

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 
using namespace std;
 
int main() {
    int num,count=0;
    cout<<"Enter the number to check : ";
    cin>>num;
    
	int i = 2;
    while (i<num){
        if(num%i==0) {       // checks if the number is fully divisible or not
            count++;
        }
        i++;
    }
    if(num < 2 || count>0) {
    	cout << num <<" is not a Prime Number \n";
    } else {
        cout << num <<" is a Prime Number \n";
	}
}
 
<-- end SyntaxHighlightingPlugin -->

Prime faster

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 using namespace std;
 
int main() {
    int num,count=0;
    cout<<"Enter the number to check : ";
    cin>>num;
    
   	int i = 2;
    while (i<num && num%i!=0){
        i++;
    }
    if (i==num) {
        cout<<"It is a Prime Number \n";
    } else {
 cout<<"It is not a Prime Number \n";
 cout<<"The Number " << num << " is divisable by " << i << "\n";
 }
 
}

 
<-- end SyntaxHighlightingPlugin -->

Prime 1

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Prime with for statement

<-- SyntaxHighlightingPlugin -->
#include<iostream>
 
using namespace std;
 
int main() {
    int num,count=0;
    cout<<"Enter the number to check : ";
    cin>>num;
 
    for(int i=2;i<num;i++){
        if(num%i==0) {       // checks if the number is fully divisible or not
            count++;
        }
    }
    if(count==0) {
        cout<<"It is a Prime Number \n";
    } else {
 cout<<"It is not a Prime Number \n";
 }
 
}
<-- end SyntaxHighlightingPlugin -->

Prime 1

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Prime 1

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Prime 1

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback