4. Strings, Arrays and Function (2)
4. Strings, Arrays and Function (2)
2
String in C++
3
String Comparison in C++
4
String Comparison in C++
5
String Comparison in C++
#include <iostream>
6
String Comparison in C++
7
String Concatenation
8
String Concatenation
int x = 10;
int y = 20;
int z = x + y; 1020
string x = "10" ;
string y = "20" ;
string z = x + y;
9
String inserting and erasing
string s1("Miss Summer");
s1.insert(5, "Ashley "); // Insert at position: 5
10
String Access
11
String Access
• Taking a string as input with whitespace in it.
string name;
cin >> name; // Enter “Anjaneya Dixit”
cout << name;
string name;
cout << "Enter your name" <<endl;
getline (cin, name);
cout << name;
12
Arrays
13
Arrays
Value [] is an array of
type int with 1000
‘elements’
14
Arrays
15
Arrays
16
Arrays
17
Arrays
18
Arrays
Let us try to create an array, which has size of 10, and elements are sum of previous two
elements. Initial two elements are 0 and 1.
int list[10];
list[0] = 0;
list[1] = 1;
TODO: write a program to determine the maximum element from an integer array.
19
Copying an Array
int firstArr[50];
int secondArr[50];
firstArr = secondArr;
Similarly, there is no aggregate input/ output for arrays, aggregate arithmetic, etc.
20
Arrays
21
Arrays: Two dimensional
22
Arrays: Two dimensional
int rows = 2;
int cols = 2;
int mat[rows][cols];
23
Arrays: Two dimensional
Array initialization
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
int x[3][4] = {
{0, 1 ,2 ,3},
{4, 5 , 6 , 7}, 0 1 2 3
{8 , 9 , 10 , 11} 4 5 6 7
}
8 9 10 11
24
Arrays: User input
int rows = 2;
int cols = 2;
int mat[rows][cols];
25
Arrays: Three dimensional
26
Arrays: Three dimensional
int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};
27
Array declaration
#include <iostream>
using namespace std;
int main() {
int size;
int size, int size,
cin>>size; OR a[size]; OR a[ ];
int a[size];
cin>>size; cin>>size;
cout<<"Taking input\n";
for (int i=0; i<size; i++)
cin>>a[i];
cout<<"Giving output\n";
for (int i=0; i<size; i++)
cout<<a[i]<<endl;
return 0;
}
28
Arrays elements as variables
29
Code to find highest and offset scores
#include <iostream> max = score[0];
using namespace std; for (int i = 0; i < num; i++)
{if (score[i] > max)
int main() { max = score[i];
int num, max ; }
cout<<"Enter class size cout << “Highest score: " << max;
";
cin>>num; cout << “\n Offset from highest:\
int score[num]; n";
for (int i = 0; i < num; i++)
cout<<"\n Enter scores\ cout << "Score "<< i << " off by
n"; "
for (int i=0; i<num; i++) << (max-score[i]) << endl;
cin>>score[i]; return 0;
}
cout<<"Scores entered\
n:";
for (int i=0; i<num; i++)
30
Arrays-Examples
Bubble Sorting
31
Arrays-Examples
Bubble Sorting
if (a[j]>a[j+1])
{
int temp = a[j];
a[j] =
a[j+1];
a[j+1] = temp;
}
32
Arrays-Examples
Selection Sorting
33
Arrays-Examples
Palindromes
Record keeping
Matrix operations
34
Functions
35
Functions
36
Functions: Basics
pow (x,y);
sqrt (x);
floor (x); ceil (x);
37
Functions: Pre-defined functions
38
Functions: Syntax
void MyFunction()
{
//Function body
}
39
Functions: Declaration and Calling
• Functions are declared before main()
• Declaration is done by defining the ‘type’ and the ‘name’ of the function
followed by a pair of parenthesis ()
• Function declaration explains how to call it
void fun() {
cout << “This is a trial function\
n”;
} What happens if you
declare fun() after main()?
int main () {
fun(); //Calling the function
fun(); //Calling multiple times
fun(); //Calling multiple times
return 0;
}
40
Functions: Declaration and Calling
• Functions are declared before main(), but can be defined after main()
• Helps in better organization of the code
• Functions are called inside the main function
• Calling is done by following the syntax of function declaration
Compilation sequence Run sequence
// Function declaration
1 void fun(); 2
// main function
2 int main() {
fun(); // calling function 1
return 0;
}
// Function definition
3 void fun() {
cout << “My first function.\n”;
3
}
41
Functions: Declaration and Calling
•Functions are declared before main(), but can be defined after main()
•Helps in better organization of the code
•Function declaration explains how to call it
42
Functions: Parameters and passing
arguments
• Functions communicate with one another through parameters
• Parameter details (numbers and types) are given during function declaration
void myFun(parameter1, parameter2, parameter
3) {
// code to be executed
}
#include <iostream>
using namespace std;
int main(){
myfunc("Anjaneya");
myfunc("Dixit", "USA");
return 0; 43
Functions: Variables as Parameters
• Functions communicate with one another through parameters
• Parameter details (numbers and types) are given during function declaration
• Variables can be passed as parameters
#include <iostream>
using namespace std;
int main(){
string s; cin>>s;
myfunc(s);
myfunc(s, "USA");
return 0;
}
44
Functions: Variables as Parameters
• Functions communicate with one another through parameters
• Parameter details (numbers and types) are given during function declaration
• Parameter mismatch (type or number) will result in compilation error
#include <iostream>
using namespace std;
int main(){
string s; cin>>s;
myfunc(s); //Invalid due to Parameter
mismatch
myfunc(s, "USA"); //Valid
return 0;
}
45
Functions: Returning a value
• Functions communicate with one another through parameters
• Parameter details (numbers and types) are given during function declaration
• For a two-way communication, functions can return values to talk back
#include <iostream>
using namespace std;
//Function declaration and receiving the argument sent from main()
int FACT(int x) //Function type set to return value type
{
int fact=1;
for (int i=1; i<=x; i++)
fact*=i; //Calculating the factorial
return fact; //Returning the factorial
}
int main(){
int x; cin>>x; //Taking input
int y = FACT(x); //Calling function and passing the
argument
cout << y; //Displaying the output
return 0;
} 46
Functions: A small program inside the program
• Instead of input using cin, arguments are received as formal parameters
• The output may be the conventional type (cout) or return of a value
• Variables are declared independently in main() and user-defined function()
#include <iostream>
using namespace std;
//Function declaration and receiving the argument sent from main()
int FACT(int x) //Function type set to return value type
{
int fact=1;
for (int i=1; i<=x; i++)
fact*=i; //Calculating the factorial
return fact; //Returning the factorial
}
int main(){
int x; cin>>x; //Taking input
int y = FACT(x); //Calling function and passing the
argument
cout << y; //Displaying the output
return 0;
} 47
Functions: Overloading
• Overloading is declaring multiple functions with same name
• Made distinct by changing the number and/or type of the arguments
• Compiler identifies relevant function by checking the argument number and type
• Only changing the return type will not allow function overloading
48
Functions: Overloading
#include <iostream>
using namespace std;
int main(){
int p,q; float r; cin>>p>>q>>r;
int a = FACT(p); //Calling function
Type-1
int b = FACT(p,q); //Calling function
Type-2
int c = FACT(p,r); //Calling function
Type-3
cout << a << b << c;
return 0; 49