Computer Programming- Lecture 10
-
Upload
dr-md-shohel-sayeed -
Category
Education
-
view
5.638 -
download
5
description
Transcript of Computer Programming- Lecture 10
![Page 1: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/1.jpg)
1TCP1231 Computer Programming I
Lecture 10Arrays,
Functions & Structures
![Page 2: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/2.jpg)
2TCP1231 Computer Programming I
Objectives
• To Learn about functions and arrays• Explore how to declare and manipulate arrays
with functions• Explore how to declare and manipulate structures
with functions• Become familiar with functions, arrays, and
structures.
![Page 3: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/3.jpg)
3TCP1231 Computer Programming I
Functions – Revisited(User Defined Function)
• Two components of a function definition
• Function declaration (or function prototype)– Shows how the function is called– Must appear in the code before the function can be
called
– Syntax:
Type_returned Function_Name(Parameter_List);//Comment describing what function does
![Page 4: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/4.jpg)
4TCP1231 Computer Programming I
Functions – Revisited(User Defined Function)
• Function definition– Describes how the function does its task– Can appear before or after the function is called– Syntax:
Type_returned Function_Name(Parameter_List){ //code to make the function work}
![Page 5: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/5.jpg)
5TCP1231 Computer Programming I
#include <iostream>using namespace std;
int p(int x, int n);int f(int x);
int main() { int no, x, n; cout << "Enter a number==> "; cin >> no; cout << "factorial = "<<f(no); cout << "\n\nEnter number 1==> "; cin >> x; cout << "Enter number 2==> "; cin >> n; cout << "power = "<<p(x,n); system(“PAUSE”); return 0;}
Function declaration
int p(int x, int n) { int temp=1; for (int i=1; i <= n; i++) temp= temp * x; return temp;}
int f(int x) { int temp=1; for (int i=1; i <= x; i++) temp= temp * i; return temp; }
Function definition
Function call
![Page 6: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/6.jpg)
6TCP1231 Computer Programming I
Structure and Function Calls
• Structure definition is generally placed outsideany function definition– This makes the structure type available to all code
that follows the structure definition
• To declare two variables of type CDAccount:
CDAccount my_account, your_account;
– My_account and your_account contain distinct member variables balance, interest_rate, and term
![Page 7: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/7.jpg)
7TCP1231 Computer Programming I
Structures as Arguments
• Structures can be arguments in function calls– The formal parameter can be call-by-value– The formal parameter can be call-by-reference
• Example:void get_data(CDAccount& the_account);
– Uses the structure type CDAccount as the type for a call-by-reference parameter
![Page 8: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/8.jpg)
8TCP1231 Computer Programming I
//Program to demonstrate the CDAccount structure type.#include <iostream>using namespace std;
//Structure for a bank certificate of deposit:struct CDAccount { double balance; double interest_rate; int term; //months until maturity};
void get_data(CDAccount& the_account);//Postcondition: the_account.balance and the_account.interest_rate//have been given values that the user entered at the keyboard.
A Structure Definition
Function declaration:To receive struct CDAccount as argument
![Page 9: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/9.jpg)
9TCP1231 Computer Programming I
int main( ){ CDAccount account; get_data(account);
double rate_fraction, interest; rate_fraction = account.interest_rate / 100.0; interest = account.balance * rate_fraction * (account.term / 12.0); account.balance = account.balance + interest;
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "When your CD matures in " << account.term << " months,\n" << "it will have a balance of $" << account.balance << endl; return 0;}
Function calling :Send account as get_data argument
![Page 10: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/10.jpg)
10TCP1231 Computer Programming I
void get_data(CDAccount& the_account){ cout << "Enter account balance: $"; cin >> the_account.balance; cout << "Enter account interest rate: "; cin >> the_account.interest_rate; cout << "Enter the number of months until
maturity\n“ << "(must be 12 or fewer months): "; cin >> the_account.term;}
Function definition :Manipulating the_account inside get_data implementation
Enter account balance: $100.00
Enter account interest rate: 10.0
Enter the number of months until maturity(must be 12 or fewer months): 6
When your CD matures in 6 months, it will have a balance of $105.00
Output:
Sample Output:
![Page 11: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/11.jpg)
11TCP1231 Computer Programming I
Structures as Return Types
• Structures can be the type of a value returned bya function
• Example:students reading(){
students rec; cout << "\nEnter the student name ==> "; getline(cin, rec.name); cout << "Enter the student ID ==> "; cin >> rec.id; cout << "Enter the student Mark ==> "; cin >> rec.mark; cin.ignore(); return rec;}
![Page 12: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/12.jpg)
12TCP1231 Computer Programming I
#include <iostream>using namespace std;
struct students{ string name; int id; double mark;};students reading(){ students rec; cout << "\nEnter the student name ==> "; getline(cin, rec.name); cout << "Enter the student ID ==> "; cin >> rec.id; cout << "Enter the student Mark ==> "; cin >> rec.mark; cin.ignore(); return rec;}
void printing(students rec){ cout << "\nThe student name: "; cout << rec.name; cout << "\nThe student ID: "; cout << rec.id; cout << "\nThe student mark: "; cout << rec.mark;}int main() { int i; students info[3]; for (i=0; i<=2; i++) info[i]=reading() ; for (i=0; i<=2; i++) printing(info[i]); return 0;}
To read info about three students
![Page 13: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/13.jpg)
13TCP1231 Computer Programming I
Arrays in Function
• Indexed variables can be arguments to functions– Example: If a program contains these declarations:
int i, n, a[10]; void my_function(int n);
Variables a[0] through a[9] are of type int, making these calls legal: my_function( a[ 0 ] ); my_function( a[ 3 ] ); my_function( a[ i ] );
![Page 14: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/14.jpg)
14TCP1231 Computer Programming I
Array as Function Arguments
• A formal parameter can be for an entire array– Such a parameter is called an array parameter
• It is not a call-by-value parameter• It is not a call-by-reference parameter
• Array parameters behave much like call-by-reference parameters
![Page 15: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/15.jpg)
15TCP1231 Computer Programming I
Array Parameter Declaration
• An array parameter is indicated using emptybrackets in the parameter list such as
void fill_up(int a[ ], int size);
![Page 16: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/16.jpg)
16TCP1231 Computer Programming I
Function with an Array Parameter
#include<iostream>using namespace std;
void fill_up(int a[], int size);//Precondition: size is the declared size of the array a.// The user will type in size integers.//Postcondition: The array a is filled with size integers// from the keyboard.
void fill_up(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; size--; cout << "The last array index used is " << size << endl;}
Function declaration:To receive an array of int, a[] as argument
Function definition :Manipulating a[] inside fill_up implementation
![Page 17: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/17.jpg)
17TCP1231 Computer Programming I
Function calls with array
• If function fill_up is declared in this way: void fill_up(int a[ ], int
size);
and array score is declared this way: int score[5], number_of_scores;
fill_up is called in this way: fill_up(score, number_of_scores);
![Page 18: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/18.jpg)
18TCP1231 Computer Programming I
Function call details
• A formal parameter is identified as an array parameter by the [ ]'s with no index expression
void fill_up(int a[ ], int size);
• An array argument does not use the [ ]'s
fill_up(score, number_of_scores);
![Page 19: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/19.jpg)
19TCP1231 Computer Programming I
Array Formal Parameters
• An array formal parameter is a placeholder forthe argument
– When an array is an argument in a function call,
an action performed on the array parameter is performed on the array argument
– The values of the indexed variables can be changed by the function
![Page 20: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/20.jpg)
20TCP1231 Computer Programming I
Array Parameter Considerations
• Because a function does not know the size of an array argument…– The programmer should include a formal parameter
that specifies the size of the array– The function can process arrays of various sizes
• Function fill_up can be used to fill an array of any size:
fill_up(score, 5); fill_up(time, 10);
![Page 21: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/21.jpg)
21TCP1231 Computer Programming I
Returning An Array
• Recall that functions can return a value of type int, double, char, …, or a class type
• Functions cannot return arrays
• We learn later how to return a pointer to an array
![Page 22: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/22.jpg)
22TCP1231 Computer Programming I
#include <iostream>using namespace std;
void ReadArray(int arr[] ) { int i; for (i=0; i < 9; i++) { cout << "a["<<i<<"]="; cin >> arr[i]; }}
int main() { int i; int a[9]; ReadArray(a); for (i=0; i < 9; i++) cout << a[i] << '\t'; system(“pause”); return 0;}
To read 9 numbers
![Page 23: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/23.jpg)
23TCP1231 Computer Programming I
#include <iostream>using namespace std;
void ReadArray(int arr[] ) { int i; for (i=0; i < 9; i++) { cout << "a["<<i<<"]="; cin >> arr[i]; }}// Bubble sortvoid sorting(int arr[] ) { int i, j, temp; for (i=0; i < 9; i++) for (j=0; j < 8; j++) if (arr[j] > arr[j+1]) { temp= arr[j]; arr[j]= arr[j+1]; arr[j+1]= temp; }}
int main() { int i; int a[9]; ReadArray(a);
for (i=0; i < 9; i++) cout << a[i] << '\t'; cout << endl; sorting(a);
for (i=0; i < 9; i++) cout << a[i] << '\t';
system(“pause”); return 0;}
To read 9 numbers, then sort them in ascending order
![Page 24: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/24.jpg)
24TCP1231 Computer Programming I
Functions and Multidimensional Array
• When a one-dimensional array is defined as a formal parameter, the size of the array may be omitted
void Fun(float list[], int size) { . . . }
![Page 25: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/25.jpg)
25TCP1231 Computer Programming I
Multi Dimensional Array as Parameter
• With two-dimensional arrays, the first dimension (number of rows) may be omitted, but not the second dimension (number of columns).
void Fun(float table[ ][5], int rows, int cols){ . . . }
• You can specify both dimensions if you choose to.
void Fun(float table[2][5], int rows, int cols){ . . . }
![Page 26: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/26.jpg)
26TCP1231 Computer Programming I
#include <iostream>using namespace std;
const int row=3;const int col=4;
void Read2Array(int arr[][col] ) { int i,j; for (i=0; i<row; i++) for ( j=0; j<col; j++) cin >> arr[i][j];}
void writing(int arr[][col]) { int i,j; for (i=0; i<row; i++) { for ( j=0; j<col; j++) cout << arr[i][j] << '\t'; cout << endl; } }
int main () { int a[row][col]; Read2Array(a); writing(a); system(“pause”); return 0;}
To two dimensional array then display its elements
![Page 27: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/27.jpg)
27TCP1231 Computer Programming I
#include <iostream>using namespace std;
void multi(int a[][3], int b[][4], int c[][4]) { int i, j, k; for (i=0; i<3; i++) for ( j=0; j<4; j++) { c[i][j]=0; for ( k=0; k<3; k++) c[i][j]= c[i][j] + a[i][k] * b[k][j]; }}void writing(int arr[][4]) { for (int i=0; i<3; i++) { for ( int j=0; j<4; j++) cout << arr[i][j] << '\t'; cout << endl; } }
int main () { int x[3][3]= {{12, 4, 9}, { -5, 3, 1}, { 9, 2, -2}};
int y[3][4]= {{11, 1, 12, 1},{ 2, 24, 32, 4}, {63, -3, 3, 4}} ;
int z[3][4];
multi(x, y, z); writing(z); system(“pause”); return 0;}
To multiply two arrays
![Page 28: Computer Programming- Lecture 10](https://reader033.fdocuments.in/reader033/viewer/2022061203/547e3a26b4af9f602d8b46ab/html5/thumbnails/28.jpg)
28TCP1231 Computer Programming I
The End