C As A Programming Language example essay topic

3,737 words
Despite having very little knowledge of the world of computer programming, I have come to believe that C++ is currently the programming language of choice. If that is the case, it would be easiest to only learn C++ and to ignore the other programming languages. However, computer programmers should not ignore the other programming languages for at least three important reasons. One, some computers might not accept C++ as a programming language. Two, there may be features of other programming languages that are superior to C++. Three, there are many useful programs written in languages besides C++ that can only be modified with knowledge of those other languages.

For these reasons, it is important that all good programmers are able to adapt to other programming languages. The intention of this lab is to create computer programmers who are able to adapt to many different programming languages. Section I: Examining High-Level Programming Languages Introduction to Section I In this section, six previously-prepared programs, written in the programming languages Fortran, Pascal, and C, are looked at and compared. For each program a hypothesis is formed about the function the program serves, and the way in which the program performs that function. Also included in the hypothesis is a description of what makes the programming language easy or difficult to read. Testing the hypothesis is simply a matter of compiling and running the program using a variety of inputs.

For each experiment in this section, I wrote out a hypothesis for the program before I tested it. Then, after testing the program, I prepared a conclusion about how the program works. Experiment 1: odd even. f Hypothesis I believe that the Fortran program will first ask for how many numbers are in your list o numbers. Then it will read in all the numbers in your list, one at a time and tell you if the number that you just entered is even or odd. The program will keep track of how many of your numbers are even and how many are add. After you have entered in all the numbers in your list, it will print out how many of your numbers are odd and how many are even.

The Fortran language is fairly easy to understand, so forming my hypothesis of what the program would do was not a very difficult task. Most of the commands used in Fortran are words that represent their function like "PRINT, "READ, "IF,"THEN", and "END". On the other hand, Some of the commands used, like "DO 11 I = 1" and "MOD", are vague and not easily understood. The print commands are especially helpful for checking my hypothesis.

Because the print commands were written to give the user a good idea of what the program is doing, they also helped me figure out what the program is going to do. Conclusion The program behaved pretty much as I thought it would in my hypothesis. First the program instructed me to "Enter length of list". Then I was to enter in the numbers in my list one by one, and after each number the computer either responded with " [number] is even" or " [number] is odd.

After I had entered in all the numbers in my list, the program printed out how many of my numbers were even and how many were odd. The program did not even mess up when I entered in decimal numbers, instead it just truncated the number and proceeded as if the truncated number was the number to be evaluated. Unfortunately, the program did label zero an even number, which it isn't, but that is a fairly minor mistake. Also, I happened to have noticed that if I was entering numbers, and screwed up, the program wouldn't let me delete the last number I entered. This is kind of a drawback, but I don't know how one would go about fixing that problem.

Experiment 2: weather. p Hypothesis To begin with, the program will print the following "Good day. My name is Ronald Gollum. I'm stuck in this box until quitting time. Please chat with me about the weather. Is it raining now" The computer will store the user's answer to this question under a variable titled "Ans". If your answer to the question is Y or y, then the computer will store the value true under the Boolean variable titled "Raining".

If your answer to that question is not Y or y, then the program will store the value False in "Raining" and then print the message "Too bad. We need rain". No matter what was entered previously, the program will print the message "Do you think it will rain tomorrow" The program will replace the old value of "Ans" with the answer to this new question. If the answer is Y or y the program will print "I'll worry about that tomorrow". If the value stored under "Raining" is true, the computer will then print the message "It's finally quitting time! Good bye.

You brightened up this rainy day". Otherwise, the program will write, "It's finally quitting time! Good bye. I want to work on my tan". For Pascal, like for Fortran, most of the commands are words that represent something - commands like "begin,"writeln" (I am guessing that ln stands for line), "read ln,"if,"then", and "end".

In Pascal, though, the commands that store and interpret variables are not worded as I would expect them to be. For example, one command was "if not Raining then". I would expect a computer to have an easier time with a more defined command like "if raining = false then". That Pascal command was probably someone's attempt to make the program more like plain English.

I also noticed that the words in Pascal are not capitalized like in Fortran. In general, Pascal appears to be much more like normal paragraph then Fortran. Tests Input Ouput at end Input Immediate Output Y You brightened up this Y I'll worry about that tomorrow. yes You brightened up this YES I'll worry about that tomorrow N I want to work on my tan. N none No I want to work on my tan.

NO none Yahoo You brightened up this YOyO I'll worry about that tomorrow Hallo I want to work on my tan. Ween none 1999 I want to work on my tan. / none Conclusion The program ran pretty much like I said that it would in my hypothesis. The only real difference is that the program reacts to any word beginning with a Y or a y, like it is a Y or a y. That is because only the first letter of any word that is entered in is stored. When I entered the word yummy, the computer treated it like I had entered an affirmative y. In the same respect, when I entered the completely unrelated word Odd as a response, the computer treated my answer like a negative response, and later responded like I had said no.

I tried answering with a bunch of different responses including numbers, symbols, and even Pascal commands, but in every case the computer treated my answer like a negative response, unless my response began with a Y or a y. Experiment 3: triangle. c Hypothesis The program will ask for three side lengths for a triangle. If all three side lengths are equal, the program will print "Equilateral". If two side lengths are equal, the program will print "Isosceles". Otherwise, the program will print "Scalene". The program will repeat until 0 is entered as a side length, and then it will stop.

Trying to guess what this program is going to do was really difficult. This program is much longer than the first two, and many of the commands seem to be unnecessary (commands like "#include", "int clarify,"break", and "scanf"). The commands are very difficult to read, and involve complex syntax (What the heck is the command "scanf ("%If%If", "&s 2", &s 3) " supposed to do). The only reason that I have any idea what the program is going to do is because of the print statements, which print what the user needs to know. However, even within the quotation marks of the print statements there is C syntax, like in this command "printf ("Isosceles / n ") ". The if commands, at the end of the program, led me to believe that the computer just compares the lengths to see whether or not they are equal to each other.

The computer instructed me to enter three lengths. After I entered three lengths the computer told me what kind of triangle those lengths corresponded to (scalene, isosceles, or equilateral). The program did not print isosceles / n etc like I hypothesized it would, instead it printed isosceles. When I entered zero as a side length the program ended just like it was supposed to... The program would accept negative lengths, despite the fact that a negative length is not possible. The only stipulation the program had to separate the 3 different kinds of triangles is that the correct number of side lengths was equal.

Experiment 4: Language Comparison Before Running the Programs C is a very difficult programming language to understand. C commands are full of syntax that does not seem to represent the plain English equivalent of the commands. C also requires all sorts of symbols, parentheses, and brackets, which makes the program look cluttered and cryptic. The bright side to the symbols, parentheses, and brackets is that the programmer does not have to follow any specific format. Compared to the C program, the Fortran program is very easy to read. Fortran reads almost like a normal language algorithm.

The commands used are REAL, PRINT, READ, and END. The programmer still needs to know some syntax, but not nearly as much as C. Each line contains one complete command, and nothing else. There are no brackets, and parentheses cluttering up the program. Pascal is very similar to Fortran, except that it is a little bit more complex and cluttered. Each line in Pascal has a semicolon at the end, whereas Fortran did not need those semicolons.

Pascal also has colons and parentheses within the commands that Fortran does not have. Pascal is still much easier to read than C. After Running the Programs When I ran them, the three programs were almost identical. The only difference between the three programs was the way the final print was formatted. Pascal represented the numbers in scientific notation with 8 decimal places, while Fortran and C represented the numbers in standard notation with about 6 decimal places.

Fortran also had large spaces before and after each number, whereas C did not. adda. f Adds 3 Numbers instead of 2. PROGRAM ADD 3 REAL NUM 1, NUM 2, NUM 3, SUM PRINT, 'Enter three numbers: ' READ, NUM 1, NUM 2, NUM 3 SUM = NUM 1 + NUM 2 + NUM 3 PRINT PRINT, 'The sum of ', NUM 1, ', ', NUM 2, ' and ', NUM 3, 'is ', SUM PRINT END All I needed to do to modify the program was add a third number to the declaration, to the initial "PRINT" command, to the "READ" command, to the "SUM = " statement, and to the final "PRINT" command. add. f subtracts two numbers PROGRAM SUBTRACT REAL NUM 1, NUM 2, SUB PRINT, 'Enter two numbers: ' READ, NUM 1, NUM 2 SUB = NUM 1 - NUM 2 PRINT PRINT, NUM 1, ' minus ', NUM 2, ' is ', SUB PRINT END All I needed to do to modify this program was to change the SUM variable to SUB, change the addition sign in the "SUB = " statement to a subtract sign, and change the final printout to print the appropriate statement. add. f Divides two numbers PROGRAM MULTIPLY OR DIVIDE REAL NUM 1, NUM 2, PROD PRINT, 'Enter two numbers: ' READ, NUM 1, NUM 2 PROD = NUM 1 / NUM 2 PRINT PRINT, NUM 1, ' / ', NUM 2, ' is ', PROD PRINT END All I needed to do to modify this program was to change the SUM variable to PROD, change the addition sign in the "SUM = " statement to a division symbol, and change the final printout to print the appropriate statement. Conclusion to Section I Writing the hypothesis's for each the three programs turned out to be more difficult than I would have thought. Each program had it's own syntax, and figuring out the syntax was not an easy task. In experiment four, I decided that Fortran was the easiest language to understand, Pascal was the next easiest, and C was extremely difficult to understand. Without looking at the print statements for C, I would not have been able to figure out what the triangle program did at all.

Minimizing the syntax and making the commands represent their functions is extremely important for producing a readable, programmable language. Section II: Programming In C++ Introduction to Section II For the last week in class, we have been working exclusively with C++ without using computers, so I have been looking forward to seeing the programs I've written in action. After programming in assembly language, the prospect of writing programs that will write out lines of characters that actually mean something is thrilling to me. I have heard that programming in C++ is a pain in the butt, but I can not imagine that it is nearly has laborious as programming in assembly language. In the section, I will find out if I can cut it as a C++ programmer. Experiment 5: taxes. cc Hypothesis First the program reads in your taxable income.

If your taxable income is less than $20,000 a year, the program multiplies your taxable income by the low-rate tax (15%), and that value is your tax. If your taxable income is less than $50,000 and greater than or equal to $20,000, the program multiplies your taxable income by the mid-rate tax (28%), and that value is your tax. If your taxable income is greater than or equal to $50,000, the program multiplies your taxable income by the high-rate tax (. 31), and that value is your tax. Finally, the program prints your tax. Because we have been studying C++ for the last week, understanding this program was not very difficult at all.

If it were not for that experience, this would probably be one of the more difficult programs to read. The program uses quiet a bit of syntax, and the parentheses, brackets, and symbols make it look cluttered and unorganized. On the good side, the program has all the constants and variables at the beginning were they can be easily changed. Conclusion The program worked as I thought it would in my hypothesis for all positive numbers.

For 0, the program returned zero, and for negative numbers, the program returned the corresponding negative answers. Experiment 6: determine grade. cc The Algorithm Get a value for minimum satisfactory score. Get a value for student's score. If student's score is greater than or equal to minimum satisfactory score then Print the message "Student's score is satisfactory". Else (student's score is less than minimum satisfactory score) Print the message "Student's score is unsatisfactory". Stop.

The Source Code // Program: Determine Grade // Author: Nathan Lindquist // Date Written: November 3, 1999 // Purpose of Program: To read in a minimum satisfactory score and a // student's score, and determine if the student's score is satisfactory // or unsatisfactory, based on the minimum satisfactory score. // #include iostream. h int main { double minsatisfactscore; double studentscore; cout endl "Enter the lowest satisfactory score". endl; cin minsatisfactscore; cout endl "Enter the student's score". endl; cin studentscore; if (studentscore = minsatisfactscore) { cout endl "Student's score of " studentscore; cout " is satisfactory". endl endl; } else { cout endl "Student's score of " studentscore; cout " is unsatisfactory". endl endl; } } Experiment 7: Modifying A C++ Program The Algorithm Get a value for points scored. If the value of points scored is greater than or equal to 90, Print the message "This vegetable is Grade A Fancy". Else if the value of points scored is greater than or equal to 84, Print the message "This vegetable is Extra Standard". Else if the value of points scored is greater than or equal to 10, Print the message "This vegetable is Standard". Else, Print the message "This vegetable is Substandard". Stop.

The Source Code // Name of Program: Determine Veggie Class // Author: Nathan Lindquist // Date written: November 3, 1999 // Purpose of Program: To read in the point value of a U S Department // of Agriculture graded canned vegetable and decide which class (Grade A // Fancy, Extra Standard, Standard, or Substandard) that canned vegetable // fits into. // #include iostream. h int main { int pointscore; cout endl "Enter the USDA point score of a canned vegetable " endl; cout " (the score should be an integer.) " endl; cin pointscore; if (pointscore = 90) { cout endl "This vegetable is Grade A Fancy" endl; } else if (pointscore = 85) { cout endl "This vegetable is Extra Standard" endl; } else if (pointscore = 70) { cout endl "This vegetable is Standard" endl; } else { cout endl "This vegetable is Substandard" endl; } } Tests Input Output 105 This veg. is Grade A Fancy 93 This veg. is Grade A Fancy 90 This veg. is grade A Fancy 87 This veg. is Extra Standard 85 This veg. is Extra Standard Input Output 76 This veg. is Standard 70 This veg. is Standard 65 This veg. is Substandard 0 This veg. is Substandard -13 This veg. is Substandard Experiment 8: Comparing C++ And Assembly Language The Algorithm The values of Integer and Total are 0. While Integer is not equal to -1 Get a new value for Integer. Set the value of Total equal to the previous value of Total + Integer. Print the value of Total. The Source Code // Name of Program: Add Until // Author: Nathan Lindquist // Date Written: November 3, 1999 // Purpose of program: To read in integers and add them // until an end number (-1) is entered. When the end // number (-1) is entered the program ends. // #include iostream. h int main { const int EndNumber = -1; // When this integer is entered, the total // is printed, and the program ends. int Integer; int Total; Integer = 0; Total = 0; while (Integer!

= EndNumber) // This will do the following steps until // the end number is entered. { Total = Total + Integer; cout endl "Enter an integer to be added. Enter the integer " endl; cout EndNumber " to print the total and end this program". endl; // These two cout commands tell the user to "Enter an // integer to be added. Enter the integer [EndNumber] // to print the total and end this program". cin Integer; } cout endl "The total of all the integers you " endl; cout "entered, except for the "; cout EndNumber ", is " Total". " endl endl; // These final three cout commands print "The total of // all the integers you entered, except for the // [EndNumber] is [Total]. The C++ program actually tells the user what value to enter and what the output means. Whereas as in assembly language, a user would not know what to input or what the output means.

In the above program the constant that ends the program can be easily switched from -1 to some other number; in assembly language, switching a constant like that could take an hour. In the above program, changing the program from adding numbers, to multiplying them would be a matter of changing one sign; in assembly language, such a change quadruples plus the size of your program. Conclusion to Section II Compiling the C++ programs actually worked out fairly well for me. If my program had an error in it when I tried to compile it, the computer would tell me what line the error was on, and it would point to what it thought the error was.

Fortunately, I did not end up with any serious problems, most of my mistakes were just typos. I heard that many others where having a very difficult time compiling their C++ programs. Compared to assembly language, programming in C++ is no problem. Conclusion Programming in Fortran, Pascal, and C++ is much more productive then programming in assembly language.

A whole array of much more advanced tools is at our disposal. Now, we can write whole lines of characters, label variables and constants, use Boolean expressions with variables and numbers other than zero, write comments into the program, and the list of new tools goes on, and on. Each one of these tools by itself would take hours, if not days to write an efficient assembly language program for. In high level programming language, we don't have to worry about registers, memory, storage, retrieval, and other assembly language functions. To write a program that instructs the user what to enter, inputs the side lengths of a triangle, and outputs the kind of triangle it is, would take weeks of boring, meticulous assembly language programming.

With Pascal, Fortran, and C++, the same program takes an hour or less.