0% found this document useful (0 votes)
31 views

Assignment

This document provides an overview of the basic structure of a C++ program. It discusses the following key points in 3 sentences or less: 1) C++ programs have a header with name, class, date, and roll number. They are submitted to the head of department. 2) C++ programs use preprocessor directives like #include to import header files, #define for macros, and #ifdef/#ifndef for conditional compilation. They also have a main() function containing program statements terminated with semicolons. 3) The document discusses common C++ header files like iostream, conio, math, and string that provide input/output, console, math, and string functions respectively. It also

Uploaded by

Hamid O6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment

This document provides an overview of the basic structure of a C++ program. It discusses the following key points in 3 sentences or less: 1) C++ programs have a header with name, class, date, and roll number. They are submitted to the head of department. 2) C++ programs use preprocessor directives like #include to import header files, #define for macros, and #ifdef/#ifndef for conditional compilation. They also have a main() function containing program statements terminated with semicolons. 3) The document discusses common C++ header files like iostream, conio, math, and string that provide input/output, console, math, and string functions respectively. It also

Uploaded by

Hamid O6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

C++

ASSIGNMENT

NAME: Muhammad Hamid


CLASS: BS-IT (2nd SEMESTER)
DATE: AUGUST 10, 2023
ROLL NO: 2214

SUBMITTED TO:
Sir Musadaq Fida (HOD)
BASIC STRUCTURE OF C++
PROGRAM:

The way a computer programme is organised and written is called its


"programme structure." It's like a plan that guides how all the parts of
the programme work together. Just like different types of buildings have
different structures, various programming languages have their own
unique programme structures.
C++ program has the following structure:
 PREPROCESSOR DIRECTIVES:
Preprocessor directives are
commands that tell the C++ compiler to do something before it starts
compiling your code. They start with a hash symbol (#) and can be used
to do things like include other files, define macros, and control the flow
of execution.
Some commonly used preprocessor directives include:
 include : This directive is used to include header files in the C++
program. Header files contain function prototypes, class
declarations, and other necessary declarations. Including header
file allows the program to use functions and classes defined in
those files.

Example: #include <iostream> includes the input/output stream


header file.
 #define: The #define directive is used to define macros, which are
symbolic names or constants. When the preprocessor encounters a
macro name in the code, it replaces it with its respective value or
expression. The #define preprocessor directive is a powerful tool
that can be used to make your code more readable and
maintainable. By defining macros, you can avoid having to type
out long and complicated expressions multiple times.
Example:
/* This program defines a macro called `PI` and uses it to calculate the area of a circle. */

#include <iostream>

#define PI 3.14159

int main() {

float radius = 10.0f;

float area = PI * radius * radius;

std::cout << "The area of the circle is " << area << std::endl;

return 0;

 #ifdef, #ifndef, #else, #endif: These directives are used for


conditional compilation. They allow certain code blocks to be
included or excluded from the final executable based on certain
condition.
Example:
// This program demonstrates the working of the #ifdef, #ifndef, #else, #endif preprocessor directives.

#include <iostream>

using namespace std;

int main() {

// Check if the macro `DEBUG` is defined.

#ifdef DEBUG

// The macro `DEBUG` is defined, so print a message to the console.


cout << "DEBUG mode is enabled." << endl;

#else

// The macro `DEBUG` is not defined, so do nothing.

#endif

return 0;

 C++ HEADER FILES:


C++ header files contain that declaration or information of
standard library functions/objects. The Header files also contain a
definition of various data types and constants needed by the program.
The extension of header file is ”.h”. C++ contains many header files.
Each file contains information about different types of predefined
functions or objects. A header file must be included in the program if it’s
any function or object is used in the program. The “include”
preprocessor directive is used to include the header file in the program.
The name of the header file is written in angle brackets “< >” or in
double quotes after the “include”.
General-Syntax:
The general syntax to include a header file is as follows:

#include<name of header file>

The commonly used header files of C++ are as follows:


 iostream.h: The words “iostream.h” stands for input and output
stream. In most of C++ program the header file “iostream” is used.
This header file contains information about various input or output
stream objects such as “cout” and “cin”. The “cin” object is used
for getting input data while the “cout” object is used for displaying
output on the screen.

Example:
#include <iostream>

int main() {
// This code uses the `cout` object from the `iostream` header file.
cout << "Hello, world!" << endl;

return 0;
}
 conio.h: The word “conio” stands for console input/output . The
conio.h header file contains functions for controlling the console
input and output. The clrscr() function clears the screen, and the
getch() function waits for the user to press a key and returns the
character.
Example:
// This program shows the working of the conio.h header file.

#include <iostream>
#include <conio.h>

int main() {
// This code uses the `clrscr()` function from the `conio.h` header file to clear the screen.
clrscr();

// This code uses the `getch()` function from the `conio.h` header file to wait for the user to
press a key.
char ch = getch();

// This code prints the character that the user pressed to the console.
std::cout << "You pressed the character " << ch << std::endl;

return 0;
}

 math.h: The math.h header file contains a number of


mathematical functions, including sqrt(), pow() and sin(). The
sqrt() function calculates the square root of a number, and the sin()
function calculates the sine of an angle and the pow() function
calculates the power.
Example:
// This program uses the `math.h` header file to calculate the square root
// of a number and the sine of an angle.

#include <iostream>
#include <math.h>

int main() {
// Declare a variable to store the number.
double number = 16;

// Calculate the square root of the number.


double square_root = sqrt(number);

// Calculate the sine of the number.


double sine = sin(number);

// Print the results.


std::cout << "The square root of " << number << " is " << square_root << std::endl;
std::cout << "The sine of " << number << " is " << sine << std::endl;

return 0;
}
 string.h: This header file contains information about various
string function such as str.cpy() for copying a string, str.cmp() for
comparing strings , str.length() to returns the length of the string,
str.find() to find the first occurrence of a substring in the string,
str.uppercase() to converts the string to uppercase, str.lowercase()
to converts the string to lowercase etc. These functions are used for
manipulating strings.
Example:
// This program shows the working of the `string.h` header file.

#include <iostream>
#include <string>
using namespace std;

int main() {
// Create a string variable.
string str = "This is a string.";

// Print the length of the string.


cout << "The length of the string is: " << str.length() << endl;

// Check if the string contains the word "string".


if (str.find("string") != string::npos) {
cout << "The string contains the word 'string'." << endl;
} else {
cout << "The string does not contain the word 'string'." << endl;
}

// Convert the string to uppercase.


string uppercase_str = str.uppercase();
cout << "The uppercase version of the string is: " << uppercase_str << endl;

// Convert the string to lowercase.


string lowercase_str = str.lowercase();
cout << "The lowercase version of the string is: " << lowercase_str << endl;

// Replace all the spaces in the string with hyphens.


string hyphen_str = str.replace(" ", "-");
cout << "The string with all the spaces replaced with hyphens is: " << hyphen_str << endl;

// Split the string into a list of words.


vector<string> words = str.split(" ");
cout << "The words in the string are: ";
for (string word : words) {
cout << word << " ";
}
cout << endl;

// Join the list of words back into a string.


string joined_str = " ".join(words);
cout << "The joined string is: " << joined_str << endl;

return 0;
}
main() function:
The main function indicates the beginning of the C++ program.
When a C++ program is executed the control goes directly to the
main function and starts executing its statements. All C++
programs must have main functions if a C++ program does not
contain a main function it can be compiled successfully but cannot
be executed.

General-Syntax:
The general syntax of the main function is as follows:

Void main (void)


{
Body of main () function
}

In this syntax:

The keyword “void” is used before the main() function it


indicates the data type of the value that is return by the main()
function. By definition, a function may except one or more
parameters and returns a single value.

C++ STATEMENTS
A program statement is a
fundamental unit of any programming language. It is an
instruction for the computer to do something. The statements
of C++ program are written under the main () function
between the curly brackets { } the set of statement written
under the main function or any other user defined function is
known as body of the function.

Each statement of C++ program ends with a semicolon (;) it


is called statement Terminator. It marks the end of first
statement (like period (.) in the English language) if the
statement terminated is missing at the end of any statement
then the computer compiler gives a Syntax error and display
an error message. Usually the following error message will
be displayed by C++ compiler during the compilation
process:

Diagnostic Message:
Statement missing;

DEBUGGING IN C++
ERRORS IN C++
Different errors may arise in C++ program. Errors in the
program are called bugs. The process of finding and
removing bugs in a program is known as the Debugging.

There are three types of errors that can arise in C++


program:
 Syntax error
 Error Logical
 Runtime error

1. SYNTAX ERROR:
The set of rules of a programming language
for writing statements of the computer program is known as the
syntax of the language the program statements are written strictly
according to these rules.

Syntax error occur when rules of the programming language are


not followed in writing the source code the compiler detects these
errors at the compile time of the source code the compiler reports a
proper error message about the error the compiler does not compile
up program that contains and text error the Syntax error easier to
detect and remove.

In C++ program there can be many causes of syntax errors some


causes are as follows:

 A missing semicolon”; “at the end of a statement.


 Missing any of the Delimiters such as {or}.
 Incorrect spelling of any keyword.
 Using variables without declaration etc.

2. LOGICAL ERRORS:

The errors in the logic of a program are called logical errors.


The compiler cannot detect logical errors a program with
logical errors is compiled and run successfully but it does
not produce the correct result.

The logical error may occur due to the following reasons:

 The sequence of instructions used in a program may be


in correct.
 Mathematical formula can be used in program
instructions may be in correct etc.

The logical errors are difficult to detect logical errors can


only be detected by examining all the units of the program
one by one it may be time consuming and lengthy process.

3. RUNTIME ERRORS:
The errors that occur during the
execution of a program are called runtime errors or execution
errors. These types of errors may occurred due to the following
reasons:
When a program attempts to perform an illegal operation such
as division of a number by zero
 If input data given to the program is not in a correct format
or input data file is not found in the specified path.
 If hardware problem occur such as hard disc error or disc
full or printer etc.

When a run time error occurs the computers Jobs the execution
of the program and displays an error message.

PROGRAMS
PROGRAM NO 1: (Concept of integer overflow and under flow.)

PROGRAM NO 2:
Convert 2.5 miles into kilometers.
PROGRAM NO 3:
Write program that declare and initialize two variables of int data type.

PROGRAM NO 4:
Write a program that computes the volume of a cylinder.

l
PROGRAM NO 5:
Write a program that computes the area of a circle.

Program no: 6

Write a program that performs the arithmetic operations


Program no 7

Write a program that interchange the value of two variables.


PROGRAM NO 8:
Write a program that assigns a value 3 to int type variable a,b,c
and calculate the product of these numbers.
PROGRAM NO 9:
Write a program that uses compound assignment statement and
operators.
PROGRAM NO 10:
Write a program that evaluates the expression “a+b++”.
PROGRAM NO 11:
Write a program that evaluates an expression “ –a+b—“.
Question no 3:
A:
B:
C:

D:
E:
F:

G:
H:

I:
J:

You might also like