(chimes) - So how did you make out? Remember, just like everyone uses language slightly differently, every programmer probably writes code in a slightly different manner. So I'd like to show you my solution, but remember, yours might be a little bit different. So far, I've only written the code to calculate the circumference so that I can show you how to add the remaining code for the area. Let's start there, and then we can walk through the entire program. I'm going to start on line 44, right below where I displayed the circle's circumference.
The first thing I need to do is to calculate the area. So I'm going to use the compute verb, COMPUTE, and I want to compute the area, so I'll do WS-AREA = and remember to back to your math days, the area of a circle is pi r squared. So I'll use my working storage pi value *, times in Cobalt is an asterisk, I want to multiply that by the radius, so WS-RADIUS squared.
So I have to do RADIUS two times. There. Now I have the area value. Now what I want to do though before I display it to the screen, I want to move it to my variable that I declared on line 29, the WS-DISPLAY-AREA because that variable actually has a pick clause that has a ZZ99.99. What that does is it presses any leading zeroes. So instead of having a bunch of leading zeroes, it will just print the values of the area with two decimal points.
So let's go ahead and move the WS-AREA to the WS-DISPLAY-AREA. Now I can display a message that says Circle area is :, it will print the literal first, and then it's going to display the WS-DISPLAY-AREA. Okay, before we run the program, let's just take a quick look. I'm going to scroll to the top. You can see on line two I changed my program ID to say the new name is circles.
I updated my comment on lines four through six. In working storage, on line 22, I have a variable called WS-CONSTANTS. This is a group item, and the only elementary item I needed for this program was WS-PI. So notice WS-PI is a 05 level variable, and I had to make sure that pi had up to five decimal points. So my pick clause has nine, and then v for the implied decimal, and then five nines after it, and I give it a literal value of 3.14159 to start.
The next group item is WS-CIRCLE, and it has several elementary items, one for the working storage area, one for the radius, and one for the circumference. Notice I used some of the figurative constants such as value zeros. I didn't put it for the circumference, but I can do that now. Notice how it turned blue because it is a key word. And finally, on line 28, I have a group item called WS-DISPLAY-VALUES. And I have two variables there because I wanted to be able to display the area and the circumference without leading zeros.
So I use Zs in front of the 99.99 to allow the program to display the number with a decimal point, but I added a pick clause that says ZZ99.99 so that my number will suppress the first two zeros, and it will give me the answer with two decimal points of precision. Okay, I think I'm ready to compile the program, but first I need to save it. If you're using Notepad++, notice in the top left corner there's a little picture of an old floppy disk, and it's red.
So as soon as I save it, it will change back to a blue color. So if you are ever working on a program and you're not sure if you saved it, you could look at that and it will tell you. All right, I'm going to open up my terminal window. The first thing I want to do is compile this program. I have it saved into my Programs folder in my OC Directory on my C Drive. So I'm going to do cobc for Cobalt compiler space -x to create the executable, space and then the program name. In this case, it's 02_05_solution.cbl.
Remember, when you compile the program, you need to include the extension. I hit Enter. I did not get any error messages, so that tells me that the compiler compiled the program, and now I have an executable that I can run. So now I'm going to do ./, again, the name of the program, 02_05_solution, with no extension. And now I'm going to hit Enter. It asks me to enter in the radius. I'll enter a radius of 10 because that's easy to calculate in our head if the program is working.
The circumference is 62.83, and the area is 314.15. Well we know the area is pi r squared, so the radius of 10 would be square would be 100 times pi, which we had defined as 3.14159. So you can see that the area looks like it's good, and I believe the circumference is also correct. Okay, if you didn't get your program to work the first time, take a look at the code here and go back and see if you can figure out what you did wrong.
Otherwise, if you got it right the first time, congratulations! You've written your first Cobalt program.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings