1. Consider the lab problem below and develop an algorithm to solve it based on the information provided. Be sure to follow the steps in the software development process as you develop and implement your algorithm.
THE SOFTWARE DEVELOPMENT PROCESS
a) Specify the problem requirements
b) Analyze the problem
c) Design an algorithm to solve the problem and tests to demonstrate the algorithm is working correctly
d) Implement the algorithm in a programming language
e) Test and verify that the implemented algorithm (program) is working correctly
f) Maintain and update the program
Write a summary of your algorithm development, showing your test plan and final refined algorithm. You algorithm development should be typed in a text or word processor file. You may add hand drawn diagrams or hand written equations.
2. When you have a completed executable file, integerSums submit your integerSums.c file it to the submission server. Remember you need to submit a zipped tar file. To use tar and zip visit the instructions for using tar and zip.
1. Print a copy of the C text file containing your program (integerSums.c), a copy of you algorithm development for your program and a copy of the lab exercise cover sheet from the class website. Fill out and sign the cover sheet. Be sure that all members in the group must sign the cover sheet. Hand in the hard copy of your lab to the CMPT 102 drop box. The hard copy of this lab problem includes a listing of your C text file, your algorithm development, and a signed cover sheet.
LAB PROBLEM 1: (submit this problem your C code should be saved in a file named integerSums.c)
a) Your program should consider a range of integers including a smallest integer, minInteger, and all integers up to and including a largest integer, maxInteger. This range can include both positive and negative integers.
b) Your program should prompt the user to enter the values of the smallest and largest integers defining this range of integers.
c) Your program should prompt the user to supply an integer value for increment.
d) You will be looking for integers exactly divisible by increment (divisible without remainder) in the range on integers selected above.
e) Your program should print all the numbers in the range that are exactly divisible by increment. Five numbers should be printed in each line of output (see below), the last line of output may have fewer than five numbers.
f) Your program should calculate the sum of all the integers in the range that are exactly divisible by another integer, increment
g) Your program should also calculate the sum of all integers in the range that are not exactly divisible by increment
h) The output that appears when you run your program should look like the sample output below. Note that the numbers are examples only; your program must work for any values of minInteger, maxInteger and increment. Note that there is meant to be a blank line after the last visible line of output.
Enter
the smallest integer to consider as part of the sum: 5
Enter
the largest integer to consider as part of the sum: 100
Enter
the divisor of interest: 7
The
numbers between 5 and 100 that are divisible by 7 are:
7,
14, 21, 28, 35,
42,
49, 56, 63, 70,
77,
84, 91, 98,
The
sum of all integers between 5 and 100
that
are divisible by 7 is 735
The
sum of all integers between 5 and 100
that
are not divisible by 7 is 4305
i) If you enter a minInteger that is larger than you maxInteger your program should produce the following output. Again there is a blank line following the last visible line of output.
Enter the
smallest integer to consider as part of the sum: 50
Enter the
largest integer to consider as part of the sum: 10
Enter the
divisor of interest: 7
No integers
larger than 50 and smaller than 10
j) If you enter an increment larger than the value you entered for maxInteger your programs should produce the following output
Enter the
smallest integer to consider as part of the sum: 20
Enter the
largest integer to consider as part of the sum: 60
Enter the
divisor of interest: 77
Divisor is
larger that largest integer,
no integers
are exactly divisible by 77
The sum of
all integers between 20 and 60
that are not
divisible by 77 is 1640
PRACTICE PROBLEM 1: Write a program to count the number of
vowels in a file containing some text.
This program gives you an example in which a case statement is useful.
1. Enter your program into the source window, Your program should
a) Prompt the user for the name of the file containing the text
b) Open the file and read the characters from the file. (read one character at a time using fscanf). The file can contain any number of characters in any number of lines. .
c) Count the number of each of the following vowels in the file a e i o u y
d) Count the number of other characters (excluding white space such as spaces and tabs) in the file
e) When you have counted the number of vowels and characters as specified above, close the file and print the summary statistics as shown below.
f) Prompt the user the see if the user wants to count the vowels in a second file
g) If the user decides to count the vowels in another file return to a) and continue, otherwise end the program execution.
h) When you run your program the input and output should look like the example given below. The input strings are shown in italics and the resulting counts are, of course, an example. Your program should work for all possible input files..
Enter
the name of the file containing the text: myfilename
The
number of instances of character a in the string is 2
The
number of instances of character e in the string is 3
The
number of instances of character i in the string is 2
The
number of instances of character o in the string is 3
The
number of instances of character u in the string is 1
The
number of instances of character y in the string is 1
The
number of instances of other characters in the string is 19
Would
you like to count the vowels in another file (y/n)? y
Enter the name of the file containing the text: myfilename2
The
number of instances of character a in the file is 6
The
number of instances of character e in the file is 22
The
number of instances of character i in the file is 5
The
number of instances of character o in the file is 7
The
number of instances of character u in the file is 1
The
number of instances of character y in the string is 1
The
number of instances of other characters in the file is 75
Would
you like to count the vowels in another file (y/n)? n
2. A few notes to make your programming easier
a) This is an ideal case for the use of a C case statement
b) Remember that the statement
nscan = fscanf(“%c”, nextChar);
reads a single character into the character variable (char nextChar;) and places the actual number of characters read into nscan. When you reach the end of the file and then try to read the next character nscan will be zero indicating that no character could be read.
c) Use a while loop that executes while you have not yet reached the end of file. C will read the string of characters one at a time (one each time you go through this while loop read one more character) even if the characters are all in a file, in multiple lines, or in a single line. Spaces, tabs and newlines will be ignored.
d) Remember that the literal form of a character is ‘a’ (not “a”) so the case labels in the case statement will be of the form: case ‘A’:
e) To read in the filename use each of the following
a. Declare a character variable to hold a name up to 16 characters long: char filename[16];
b. Read the name of the file using the scanf statement scanf(“%s”, filename);
c. Follow the scanf to read the filename with an additional scanf scanf(“%c”, nextChar);
This scanf reads the line feed after the filename
d. To open the file use the statement filep = fopen( filename, “r”);
Note that because filename is a variable and not a literal constant there are not “” around filename.
If you want more practice try some of the end of chapter problems in your textbook.