Fundamentals of Programming in C++ - CH 1
Fundamentals of Programming in C++ - CH 1
Chapter One
Fundamentals of C++ programming language & Problem-solving using
computer
What is program & programming?
Program: is a set of instructions, to make a computer to do something.
Programming: is a skill that can be acquired by a computer professional that gives
him/her the knowledge of making the computer perform the required operation or
task.
1.4 Algorithm designing and modeling the logic (using flow chart).
✓ A digital computer is a useful tool for solving a great variety of problems.
A solution to a problem is called an algorithm; it describes the sequence of steps to
be performed for the problem to be solved.
✓ Generally, an algorithm is a finite set of well-defined instructions for
accomplishing some task which, given an initial state, will terminate in a
corresponding recognizable end-state.
✓ The algorithm should be:
• Precise and unambiguous
• Efficient ..Correct
Simple
1. Design a flow chart algorithm which sums all the even numbers between 1 and 20
inclusive and then display the sum.
Mettu University
Collage of Engineering and technology
Worksheet 1
Course Title: Computer Programming / Fundamentals of Programming
Course No : __________
1:#include <iostream.h>
2:
3: int main()
4: {
5: cout<< "Hello World!\n";
6: return 0;
7: }
8 Compiled by: Dejene B. Dept. Computer Science, 2nd year Page 8
Fundamentals of Programming in C++ - CH 1 2021
On line 1, the file iostream.h is included in the file. The first character is the # symbol, which is a signal to
the preprocessor. Each time you start your compiler, the preprocessor is run. The preprocessor reads through
your source code, looking for lines that begin with the pound symbol (#), and acts on those lines before the
compiler runs.
include is a preprocessor instruction that says, "What follows is a filename. Find that file and read it in right
here." The angle brackets around the filename tell the preprocessor to look in all the usual places for this file.
If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file
iostream.h in the directory that holds all the H files for your compiler. The file iostream.h (Input-Output-
Stream) is used by cout, which assists with writing to the screen. The effect of line 1 is to include the file
iostream.h into this program as if you had typed it in yourself.
The preprocessor runs before your compiler each time the compiler is invoked. The preprocessor translates
any line that begins with a pound symbol (#) into a special command, getting your code file ready for the
compiler.
Line 3 begins the actual program with a function named main(). Every C++ program has a main() function.
In general, a function is a block of code that performs one or more actions. Usually functions are invoked or
called by other functions, but main() is special. When your program starts, main() is called automatically.
main(), like all functions, must state what kind of value it will return. The return value type for main() in
HELLO.CPP is int, which means that this function will return an integer value.
All functions begin with an opening brace ({) and end with a closing brace (}). The braces for the main()
function are on lines 4 and 7. Everything between the opening and closing braces is considered a part of the
function.
The meat and potatoes of this program is on line 5. The object cout is used to print a message to the screen.
cout is used in C++ to print strings and values to the screen. A string is just a set of characters.
Here's how cout is used: type the word cout, followed by the output redirection operator (<<). Whatever
follows the output redirection operator is written to the screen. If you want a string of characters written, be
sure to enclose them in double quotes ("), as shown on line 5. A text string is a series of printable characters.
The final two characters, \n, tell cout to put a new line after the words Hello World! All ANSI-compliant
programs declare main() to return an int. This value is "returned" to the operating system when your program
completes. Some programmers signal an error by returning the value 1.
Any C++ program file should be saved with file name extension “ .CPP ”
Type the program directly into the editor, and save the file as hello.cpp, compile it and then run
it. It will print the words Hello World! on the computer screen.
The end of a single statement ends with semicolon (;).
Compilation
This phase translates the program into a low level assembly level code. The compiler takes the preprocessed
file (without any directives) and generates an object file. Now, the object file created is in the binary form. In
the object file created, each line describes one low level machine level instruction.
Hence, the compilation phase generates the relocatable object program and this program can be used in
different places without have to compile again. But you still can’t run these object files until to convert them
into executable file, now here linker comes into play, which links all the object files to generate single
executable file
Linking
Linking as the name suggests, refers to creation of a single executable file from multiple object files. The file
created after linking is ready to be loaded into memory and executed by the system. There is difference in
linking and compilation when it comes to understanding errors. Compiler shows errors in syntax,
For example, semi-colon not mentioned, data type not defined etc but if there is an error that function has been
defined multiple times, then this error is from linker as it indicating that two or more source code files have the same
meaning and that is leading to an error.
2) Multiple Line Comment: Anything enclosed by the pair /* and*/ is considered a comment.
Eg: /* this is a kind of comment where Multiple lines can be enclosed in one
prograsm */
✓ Comments should be used to enhance (not to hinder) the readability of a program. The following two
points, in particular, should be noted:
✓ A comment should be easier to read and understand than the code which it tries to explain. A
confusing or unnecessarily- complex comment is worse than no comment at all.Over-use of
comments can lead to even less readability. A program which contains so much comment that
you can hardly see the code can by no means be considered readable.
✓ Use of descriptive names for variables and other entities in a program, and proper indentation of
the code can reduce the need for using comments.
Mettu University
Collage of Engineering and technology
Worksheet 2
Course Title: Fundamentals of Programming
Course code CoSc 1013
For each of the problems write a C++ code to perform the required task. Your program should be
based on the flow chart you drawn in the first worksheet.