Sample Programs Thursday 7/11/02 Example #1 ---------- // This program computes all the primes from 2 to 100. #include int main() { const int num = 100; int counter = 2; int check; bool isprime; cout << "The prime numbers from 2 to " << num << " are :\n"; while (counter <= num) { isprime = true; check = 2; while (check < counter) { if (counter%check==0) { isprime = false; check = counter; } check = check + 1; } if (isprime) cout << counter << " "; counter = counter + 1; } return 0; } Example #2 ---------- //Computes the average of all the test grades entered by user. #include void main() { int grade = 0; int sum = 0; int numtests = -1; double average; cout << "Enter all of your grades, followed by a -1 to signify you are done.\n"; do { sum = sum + grade; numtests++; cin >> grade; } while (grade !=-1); if (numtests==0) cout << "Sorry no grades entered to average.\n"; else { average = double(sum)/numtests; cout << "The average of your grades was " << average << endl; } } Example #3 ---------- #include #include //Function prototypes. char askchar(); int countchar(char); void main() { char search1, search2, search3; cout << "This program computes the number of occurances of a three\n"; cout << "characters in the dictionary.\n"; cout << "To quit, enter a #.\n"; // Uses function call to determine the character entered by user, and how many // of that character are in the dictionary. search1 = askchar(); int count1 = countchar(search1); cout << "The number of " << search1 << "'s in the dictionary is " << count1 << endl; // The two following blocks of code do exactly what the first does. search2 = askchar(); int count2 = countchar(search2); cout << "The number of " << search2 << "'s in the dictionary is " << count2 << endl; search3 = askchar(); int count3 = countchar(search3); cout << "The number of " << search3 << "'s in the dictionary is " << count3 << endl; } // This function asks the user for a character, reads it in and returns that char. char askchar() { char c; cout << "Enter the first character you want to search for.\n"; cin >> c; return c; } // This function counts the number of occurances of the character c passed into it // in the dictionary. int countchar(char c) { char dummy; int count = 0; ifstream fin("input.dat"); while (fin >> dummy) { if (dummy==c) count++; } return count; } Example #4 ---------- #include #include char askchar(); int countchar(char); void oneiter(); void main() { char search1, search2, search3; int i; cout << "This program computes the number of occurances of a three\n"; cout << "characters in the dictionary.\n"; cout << "To quit, enter a #.\n"; for (i=0;i<3;i++) oneiter(); } char askchar() { char c; cout << "Enter the first character you want to search for.\n"; cin >> c; return c; } int countchar(char c) { char dummy; int count = 0; ifstream fin("input.dat"); while (fin >> dummy) { if (dummy==c) count++; } return count; } // This is a void function that executes one block of code from example #4 void oneiter() { char c = askchar(); int count = countchar(c); cout << "The number of " << c << "'s in the dictionary is " << count << endl; }