2. Problem Solving Skills
ā¢ STEP 1: ANALYSE THE PROBLEM
ā¢ STEP 2: PLANNING THE ALGORITHM
ā¢ STEP 3: CHECK THE ALGORITHM
ā¢ STEP 4: CODING THE ALGORITHM
ā¢ STEP 5: EVALUATE AND MODIFY
10. Problem solving skills
ā¢ Step 1: Understanding the Problem:
ā¢ Here we try to understand the problem to be solved in totally. Before with the next stage
or step, we should be absolutely sure about the objectives of the given problem.
ā¢ Step 2: Analyzing the Problem:
ā¢ After understanding thoroughly the problem to be solved, we look at different ways of
solving the problem and evaluate each of these methods.
ā¢ The idea here is to search for an appropriate solution to the problem under
consideration. The end result of this stage is a broad overview of the sequence of
operations that are to be carried out to solve the given problem.
ā¢ Step 3: Developing the solution:
ā¢ Here, the overview of the sequence of operations that was the result of the analysis
stage is expanded to form a detailed step by step solution to the problem under
consideration.
11. Problem solving skills
ā¢ Step 4: Coding and Implementation:
ā¢ The last stage of problem-solving is the conversion of the detailed sequence of operations into a language
that the computer can understand. Here, each step is converted to its equivalent instruction or instructions
in the computer language that has been chosen for the implantation.
ā¢ The vehicle for the computer solution to a problem is a set of explicit and unambiguous instructions
expressed in a programming language. This set of instruction is called a program with problem solving
through programming in C++.
ā¢ A program may also be thought of as an algorithm expressed in a programming language. an algorithm,
therefore, corresponds to a solution to a problem that is independent of any programming language.
ā¢ To obtain the computer solution to a problem once we have the program we usually have to supply the
program with input or data. The program then takes this input and manipulates it according to its
instructions. Eventually produces an output which represents the computer solution to the problem.
ā¢ The problem solving is a skill and there are no universal approaches one can take to solving problems.
Basically one must explore possible avenues to a solution one by one until she/he comes across the right
path to a solution.
ā¢ In general, as one gains experience in solving problems, one develops oneās own techniques and strategies,
though they are often intangible. Problem-solving skills are recognized as an integral component of
computer programming.
15. Pre processor directive
ā¢ Nothing but a program
ā¢ It makes the C++ program portable
ā¢ User can create own pre-processor directive
ā¢ Can be placed before the main function
ā¢ Cannot be terminated by semi colon since it is a special instruction
ā¢ E.g # include ā includes the header file (containing library functions)
16. Namespace in C++
ā¢ What is a namespace in C++?
ā¢ A namespace is a declarative region that provides a scope where
identifiers like variables, functions, classes etc are declared.
ā¢ Main purpose is to prevent ambiguity that may occur when two
identifiers have same name.
ā¢
22. If else conditional statement
ā¢ Greet according to time of the day
ā¢ Check if integer is positive, negative or zero
ā¢ Find largest number amongst 3 nos
24. ā¢ The switch expression is evaluated once
ā¢ The value of the expression is compared with the
values of each case
ā¢ If there is a match, the associated block of code is
executed
ā¢ The break and default keywords are optional
25. ā¢ int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
26. ā¢ Write an algorithm, draw a flowchart and write a C program to accept
the percentage and print the grade A (90 to 100), B (80 to 90), C (50
to 80) and Fail below 50.
ā¢ Write a C++ program to check whether input character is vowel or
consonant.
ā¢ Write a program to give a choice to user to find area of a shape ā
circle, rectangle, triangle, square
38. Nested loop
ā¢ do
ā¢ {
ā¢ do
ā¢ {
ā¢ // statement of inside loop
ā¢ }
ā¢ while(condition);
ā¢ // statement of outer loop
ā¢ }
ā¢ while(condition);
39. 2D array
ā¢ Write a program to take values for 2-dimensional array and to give
choice to the user for performing below operations:
ā¢ 1. Sum of elements of each row.
ā¢ 2. Sum of diagonal elements.
ā¢ 3. Finding transpose of matrix
40. 2D array
ā¢ Develop, implement and execute a C++ program that reads two
matrices A (m x n ) and B (p x q ) and Compute the product A and B.
Read matrix A and matrix B in row major order and in column major
order respectively. Print both the input matrices and resultant matrix
with suitable headings and output should be in matrix format only.
Program must check the compatibility of orders of the matrices for
multiplication. Report appropriate message in case of incompatibility.
41. Functions
1. Write a function to find sum and average of upper diagonal elements
in 2-dimensional matrix. Write a main program () to take number of
rows, cols and values of matrix from the user.
2.Write a menu-based program in C++ (Use Switch case and do while)
that uses a set of functions to perform the following operations:
ā¢ a. Addition of two complex numbers
ā¢ b. Subtraction of two complex numbers
ā¢ c. Multiplication of two complex numbers
42. Inline function example
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "n";
return 0;
}
43. Inline function example
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{ return (x > y)? x : y; }
// Main function for the program
int main() { cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; }
44. Some facts about inline function
ā¢ Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return
statement doesnāt exist in function body.
5) If a function contains switch or goto statement.
45. Some facts about inline function
ā¢ The added variables from the inlined function consumes additional
registers, After in-lining function if variables number which are going
to use register increases than they may create overhead on register
variable resource utilization. This means that when inline function
body is substituted at the point of function call, total number of
variables used by the function also gets inserted. So the number of
register going to be used for the variables will also get increased. So if
after function inlining variable numbers increase drastically then it
would surely cause an overhead on register utilization.
46. Some facts about inline function
ā¢ If you use too many inline functions then the size of the binary
executable file will be large, because of the duplication of same code.
ā¢ Too much inlining can also reduce your instruction cache hit rate, thus
reducing the speed of instruction fetch from that of cache memory to
that of primary memory.
47. Structures
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
ā¢ Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
ā¢ Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
48. Example
ā¢ // Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
// Print members of myStructure
cout << myStructure.myNum << endl;
cout << myStructure.myString << endl;
49. Example of structure
#include <iostream>
using namespace std;
struct Person
{ char name[50];
int age; float salary; };
int main()
{ Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary; return 0; }
50. Structure as function argument
#include<iostream>
using namespace std;
struct Person
{ int citizenship; int age; };
void func(struct Person p);
int main()
{ struct Person p; p.citizenship = 1; p.age = 27; func(p); return 0; }
void func(struct Person p) { cout << " Person citizenship: " <<
p.citizenship<<endl;
cout << " Person age: " << p.age; }
51. Pointers
ā¢ Pointers store the address of variables or a memory location
ā¢ pointer āptrā holds the address of an integer variable or holds the
address of memory whose value(s) can be accessed as integer values
through āptrā
ā¢ Pointers:
ā¢ Pointers save memory space.
ā¢ Execution time with pointers is faster because data are manipulated
with the address, that is, direct access to memory location.
ā¢ Memory is accessed efficiently with the pointers. The pointer assigns
and releases the memory as well. Hence it can be said the Memory of
pointers is dynamically allocated.
ā¢ Pointers are used with data structures. They are useful for
representing two-dimensional and multi-dimensional arrays.
52. Pointers
ā¢ pointer is a symbolic representation of a memory address.
ā¢ Here is an example of valid pointer declarations in C++:
ā¢ int *x; // a pointer to integer
ā¢ double *x; // a pointer to double
ā¢ float *x; // a pointer to float
ā¢ char *ch // a pointer to a character
53. Pointers
ā¢ Reference operator (&) and Deference operator (*)
ā¢ The reference operator (&) returns the variableās address.
ā¢ The dereference operator (*) helps us get the value that has been
stored in a memory address.
54. Example of Pointers
#include <iostream>
using namespace std;
int main()
{ int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl; return 0; }
55. More examples in Pointers
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int i = 10;
int *Ptr;
Ptr = &i;
cout << "nValue Of i :" << i;
cout << "nAddress Of i :" << i;
cout << "nValue Of Ptr :" << Ptr;
cout << "nAddress Of Ptr :" << &Ptr;
cout << "nPtr's Pointer Value:" << *Ptr;
cout << "nPtr Equal to &i :" << *(&i); }
57. Array using Pointers
#include <iostream>
using namespace std;
int main()
{ int *ip;
int arr[] = { 10, 34, 13, 76, 5, 46 };
ip = arr;
for (int x = 0; x < 6; x++)
{ cout << *ip << endl; ip++; }
return 0; }
58. Functions in Pointers
#include <iostream>
using namespace std;
void test(int*, int*);
int main()
{ int a = 5, b = 5;
cout << "Before changing:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
test(&a, &b);
cout << "nAfter changing" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0; }
void test(int* n1, int* n2)
{ *n1 = 10; *n2 = 11; }
59. Swapping numbers using pointers
#include <iostream>
#include<conio.h>
using namespace std;
// Declare Swap Function Using Pointer
void swap_numbers(int *value1, int *value2)
{ int temp;
temp = *value1;
*value1 = *value2;
*value2 = temp; }
int main()
{ // Declare Variables int number1, number2;
cout << "Simple Example Program for Swap Numbers Using Pointers In C++ā<<endl;
// Read User Input
60. Swapping numbers using pointers
//Print Values before Swapping
cout << "Enter value of Swap Number # 1: ";
cin>>number1;
cout << "Enter value of Swap Number # 2: ";
cin>>number2;
cout << "Before Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 <<
endl;
//Call Swap Function By Passing Reference
swap_numbers(&number1, &number2);
//Print Values after Swapping
cout << "After Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 <<
endl;
getch();
return 0; }
61. Print string using pointers
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{ // Declare Variables
char str[20], *pt;
cout << "Pointer Example C++ Program : Print Stringā<<endl;
cout << "Enter Any string [below 20 chars] : ";
cin>>str;
// Assign to Pointer Variable
pt = str;
while (*pt != '0')
{ cout << *pt;
pt++; }
return 0; }
62. Declaring a Pointer to a pointer
ā¢ int *ptr1 = &var; // Declaration of single pointer.
ā¢ int **ptr2 = &ptr1; // Declaration of double pointer or pointer to
pointer in C++.
63. Declaring a Pointer to a pointer
#include<iostream>
using namespace std;
int main()
{ // Declaring an integer variable.
int age = 21;
// Declaring a single pointer variable, // storing address of age variable.
int *ptr1 = &age;
// Declaring a double pointer variable storing address of ptr1.
int **ptr2 = &ptr1;
// Printing value using age variable.
cout<<"Value stored in the var variable "<<var<<"n";
// Printing value using single pointer variable.
cout<<"Value accessed using single pointer "<<*ptr1<<"n";
// Printing value using double pointer variable.
cout<<"Value accessed using double pointer "<<**ptr2<<"n";
return 0; }
64.
65.
66. Examples of pointers
ā¢ WAP to demonstrate different arithmetic operations on pointers.
ā¢ WAP to find largest of the elements of an array using a function and
passing argument using pointer.
ā¢ WAP to demonstrate use of a pointer to pointer.
ā¢ WAP to demonstrate string handling using pointer.
67.
68. To do Tasks
ā¢ WAP to demonstrate different arithmetic operations on pointers.
ā¢ WAP to swap values of two variables using pointers.
ā¢ WAP to largest of the elements of an array using a function and
passing argument using pointer.
ā¢ WAP to demonstrate use of a pointer to pointer.
ā¢ WAP to demonstrate string handling using pointer.
69. Advantages of using Pointers
ā¢ Pointers are variables which store the address of other variables in
C++.
ā¢ More than one variable can be modified and returned by function
using pointers.
ā¢ Memory can be dynamically allocated and de-allocated using
pointers.
ā¢ Pointers help in simplifying the complexity of the program.
ā¢ The execution speed of a program improves by using pointers.
75. Identifiers
ā¢ Identifiers ā variables, functions, arrays, classes created by
programmer.
ā¢ Only alphabetic characters, digits and underscore
ā¢ Name cannot start with a digit
ā¢ Uppercase and lowercase letters are distinct
ā¢ Declared keyword cannot be used as variable name
76. Constant
ā¢ Refers to fixed values that do not change during execution of a
program
ā¢ constant int a = 25;
82. Macros
ā¢ A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive.
Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions
need not be terminated by a semi-colon(;)
// C++ program to illustrate macros
#include <iostream>
using namespace std;
// Macro definition
#define LIMIT 5
int main()
{
// Print the value of macro defined
cout << "The value of LIMIT"
<< " is " << LIMIT;
return 0;
}
83. Macros example
// C++ program to illustrate macros
#include <iostream>
using namespace std;
#define AREA(l, b) (l * b)
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
cout << "Area of rectangleā << " is: "<< area;
return 0;
}
97. ā¢ A hospital management want to enhance their software in the
following manner :
ā¢ The software should be able to display departments like medicine,
surgery, gynaecology, obstetrics, paediatrics, eye and ENT (any 4)
ā¢ The user should be able to view the availability of respective doctors
in the respective department with their charge (fees)
ā¢ Once user selects department he/she wants to visit , a tentative bill is
to be generated
ā¢ Give the option the user to exit
98. Array
ā¢ Declaration :
Datatype arrayName[arraySize]
Few Things to Remember:
ā¢ The array indices start with 0. Meaning x[0] is the first element stored at
index 0.
ā¢ If the size of an array is n, the last element is stored at index (n-1). In this
example, x[5] is the last element.
ā¢ Elements of an array have consecutive addresses. For example, suppose
the starting address of x[0] is 2120.
Then, the address of the next element x[1] will be 2124, the address
of x[2] will be 2128, and so on.
Here, the size of each element is increased by 4. This is because the size
of int is 4 bytes.
100. Another method to initialize array during
declaration:
ā¢ / declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
101. To store and print array
#include<iostream.h>
using namespace using std;
main()
{
int i;
int a[6] = {6,23,15,2,18,9};
for(i=0;i<6;i++)
{cout<<a[i]<<endl;}
return 0;
}
102. Examples
For below statements declare array variable.
a. To store integer marks of 10 students.
b. To store yearly average rating of 50 employees.
c. To store 10 characters.
103. Examples
For below arrays find the address of said position.
a. int marks[10]; Find the address of element stored at index 5. Base
address is 1000.
ā¢ char c[10]; Find the address of element stored at index 7. Base
address is 1000.
45 70 80 30 67 85 98 34 25 56
A C D R Y N O P E F
105. 2 dimensional array
Write a program to take values for 2-dimensional array and to give
choice to the user for performing below operations:
1. Sum of elements of each row.
2. Sum of diagonal elements.
3. Finding transpose of matrix.
106. Declaring a function
return_type function_name( parameter list )
{ body of the function }
ā¢ Return Type ā A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
ā¢ Function Name ā This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
ā¢ Parameters ā A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
ā¢ Function Body ā The function body contains a collection of statements that
define what the function does.
107. Example
ā¢ // function returning the max between two numbers
int max(int num1, int num2) { // local variable declaration
int result;
if (num1 > num2)
result = num1;
else result = num2;
return result; }
108. In main
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100; int b = 200; int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}