New Look At The Language At C English Language Essay

Published:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

For a C++ programmer, the tutorial offers a new look at the language. It shows how to avoid the pitfalls of C++ and use the language according to the way it should have been designed in the first place. I would lie if I said that C++ is a beautiful programming language. However, it is going to be, at least for some time, the most popular language for writing serious software. We may as well try to take advantage of its expressive power to write better software, rather than use it to find so many more ways to hurt ourselves. For a C++ programmer, this part of the book should be mostly easy reading. And, although the constructs and the techniques introduced there are widely known, I tried to show them from a different perspective. My overriding philosophy was to create a system that promotes maintainable, human-readable coding style. That's why I took every opportunity not only to show various programming options but also to explain why I considered some of them superior to others.

Finally, for a Java programmer, this book should be an eye-opener. It shows that, with some discipline, it is possible to write safe and robust code in C++. Everything Java can do, C++ can do, too. Plus, it can deliver unmatched performance.

Answer 1

Code:

//using switch statement

#include <iostream>

using namespace std;

int main()

{

int arithmetic;

int num1, num2;

int total;

cout<<"Enter first number: ";

cin>>num1;

cout<<"Enter second number: ";

cin>>num2;

cout<<"\n";

cout<<"Menu"<<"\n";

cout<<"1=> Addition"<<"\n";

cout<<"2=> Subtraction"<<"\n";

cout<<"3=> Multiplication"<<"\n";

cout<<"4=> Division"<<"\n";

cout<<"\n";

cout<<"Enter your choice: ";

cin>>arithmetic;

cout<<"\n";

switch (arithmetic)

{

case 1: total=num1 + num2;

cout<<"Num1+Num2= "<<total;

break;

case 2: total=num1 - num2;

cout<<"Num1+Num2= "<<total;

break;

case 3: total=num1 * num2;

cout<<"Num1+Num2= "<<total;

break;

case 4: total=num1 / num2;

cout<<"Num1+Num2= "<<total;

break;

default : cout<<"Invalid";

break;

}

cout<<"\n";

return 0;

}

//using switch statement

This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.

#include <iostream>

Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std;

All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.

int main()

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

int arithmetic;

int num1, num2;

int total;

The integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers needed to be represented. Signed types can represent both positive and negative values, whereas unsigned types can only represent positive values (and zero). This can be specified by using either the specifier signed or the specifier unsigned before the type name.

cout<<"Enter first number: ";

cin>>num1;

cout<<"Enter second number: ";

cin>>num2;

cout<<"\n";

cout<<"Enter first number: ";;

cout<<"Enter second number: ";

This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.

cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen).

cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.

cin>>num1;

cin>>num2;

The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even if you request a single character, the extraction from cin will not process the input until the user presses RETURN after the character has been introduced.

cout<<"Menu"<<"\n";

cout<<"1=> Addition"<<"\n";

cout<<"2=> Subtraction"<<"\n";

cout<<"3=> Multiplication"<<"\n";

cout<<"4=> Division"<<"\n";

cout<<"\n";

cout<<"Enter your choice: ";

cin>>arithmetic;

cout<<"\n";

By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout. Cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs). The << operator inserts the data that follows it into the stream preceding it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and variable x into the standard output stream cout. Notice that the sentence in the first instruction is enclosed between double quotes (") because it is a constant string of characters. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variable names.

switch (arithmetic)

{

case 1: total=num1 + num2;

cout<<"Num1+Num2= "<<total;

break;

case 2: total=num1 - num2;

cout<<"Num1+Num2= "<<total;

break;

case 3: total=num1 * num2;

cout<<"Num1+Num2= "<<total;

break;

case 4: total=num1 / num2;

cout<<"Num1+Num2= "<<total;

break;

default : cout<<"Invalid";

break;

}

It works in the following way: switch evaluates arithmetic and checks if it is equivalent to case 1, if it is, it executes total=num1 + num2 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure.

If arithmetic was not equal to case 1 it will be checked against case 2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure.

Finally, if the value of arithmetic did not match any of the previously specified constants (you can include as many case labels as values you want to check). The switch statement is a bit peculiar within the C++ language because it uses labels instead of blocks. This forces us to put break statements after the group of statements that we want to be executed for a specific condition. Otherwise the remainder statements -including those corresponding to other labels- will also be executed until the end of the switch selective block or a break statement is reached.

For example, if we did not include a break statement after the first group for case one, the program will not automatically jump to the end of the switch selective block and it would continue executing the rest of statements until it reaches either a break instruction or the end of the switch selective block. This makes it unnecessary to include braces { } surrounding the statements for each of the cases, and it can also be useful to execute the same block of instructions for different possible values for the expression being evaluated.

return 0;

The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Output

Addition:

Subtraction:

multiplication:

Division:

Question 2

Write a function "print_pyramid(...)" which takes a single integer argument "height" and displays a "pyramid" of this height made up of of "*" characters on the screen. Test the function with a simple "driver" program, which should be able to reproduce the following example output:

This program prints a 'pyramid' shape of

a specified height on the screen.

how high would you like the pyramid?: 37

Pick another height (must be between 1 and 30): 6

**

****

******

********

**********

************

Answer 2

Code:

#include <iostream>

using namespace std;

void print_pyramid(int height);

int main()

{

int pyramid_height;

cout << "This program prints a 'pyramid' shape of"<<"\n";

cout << "a specified height on the screen."<<"\n\n";

cout << "how high would you like the pyramid?: ";

cin >> pyramid_height;

while (pyramid_height < 1 || pyramid_height > 30)

{

cout << "Pick another height (must be between 1 and 30): ";

cin >> pyramid_height;

}

print_pyramid(pyramid_height);

return 0;

}

void print_pyramid(int height)

{

int line;

int const MARGIN = 10;

cout << "\n\n";

for (line = 1 ; line <= height ; line++)

{

int count;

int total_no_of_spaces = MARGIN + height - line;

for (count = 1 ; count <= total_no_of_spaces ; count++)

cout << ' ';

for (count = 1 ; count <= line * 2 ; count++)

cout << "*";

cout << "\n";

}

cout << "\n\n";

}

#include <iostream>

using namespace std;

void print_pyramid(int height);

int main()

// my first program in C++

This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.

#include <iostream>

Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std;

All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.

int pyramid_height;

cout << "This program prints a 'pyramid' shape of"<<"\n";

cout << "a specified height on the screen."<<"\n\n";

cout << "how high would you like the pyramid?: ";

cin >> pyramid_height;

while (pyramid_height < 1 || pyramid_height > 30)

{

cout << "Pick another height (must be between 1 and 30): ";

cin >> pyramid_height;

}

print_pyramid(pyramid_height);

return 0;

Its format is while (expression) statement and its functionality is simply to repeat statement while the condition set in expression is true.

When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown end.

void print_pyramid(int height)

{

int line;

int const MARGIN = 10;

cout << "\n\n";

for (line = 1 ; line <= height ; line++)

{

int count;

int total_no_of_spaces = MARGIN + height - line;

for (count = 1 ; count <= total_no_of_spaces ; count++)

cout << ' ';

for (count = 1 ; count <= line * 2 ; count++)

cout << "*";

cout << "\n";

}

cout << "\n\n";

}

Its format is for (initialization; condition; increase) statement;and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.

The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs between them must be written. For example we could write: for (;n<10;) if we wanted to specify no initialization and no increase; or for (;n<10;n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before).

Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) is an expression separator, it serves to separate more than one expression where only one is generally expected. For example, suppose that we wanted to initialize more than one variable in our loop:

Output

Conclusion

I have shown how arithmetic operations vary across language families. And while operations  were not carefully defined in early languages, modern languages are much more carefully in defining exactly ow arithmetic is done. But there are still quite a few differences in modern languages. The standard C++ library is a collection of functions, constants, classes, objects and templates that extends the C++ language providing basic functionality to perform several tasks, like classes to interact with the operating system, data containers, manipulators to operate with them and algorithms commonly needed. C is a great programming language that can make programming a lot of fun. One of the best ways to learn is by taking a programming class. Programming in C is a skill that could end up saving you your job or help you get a better job.

Writing Services

Essay Writing
Service

Find out how the very best essay writing service can help you accomplish more and achieve higher marks today.

Assignment Writing Service

From complicated assignments to tricky tasks, our experts can tackle virtually any question thrown at them.

Dissertation Writing Service

A dissertation (also known as a thesis or research project) is probably the most important piece of work for any student! From full dissertations to individual chapters, we’re on hand to support you.

Coursework Writing Service

Our expert qualified writers can help you get your coursework right first time, every time.

Dissertation Proposal Service

The first step to completing a dissertation is to create a proposal that talks about what you wish to do. Our experts can design suitable methodologies - perfect to help you get started with a dissertation.

Report Writing
Service

Reports for any audience. Perfectly structured, professionally written, and tailored to suit your exact requirements.

Essay Skeleton Answer Service

If you’re just looking for some help to get started on an essay, our outline service provides you with a perfect essay plan.

Marking & Proofreading Service

Not sure if your work is hitting the mark? Struggling to get feedback from your lecturer? Our premium marking service was created just for you - get the feedback you deserve now.

Exam Revision
Service

Exams can be one of the most stressful experiences you’ll ever have! Revision is key, and we’re here to help. With custom created revision notes and exam answers, you’ll never feel underprepared again.