0% found this document useful (0 votes)
2 views8 pages

Arrays, Functions, Recursive Functions Examples

The document contains exercises demonstrating C++ programming concepts such as arrays, functions, and recursion. Each exercise includes code snippets with expected outputs, illustrating how to manipulate data and perform calculations. The final exercise shows how to calculate powers using a recursive function.

Uploaded by

name name2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Arrays, Functions, Recursive Functions Examples

The document contains exercises demonstrating C++ programming concepts such as arrays, functions, and recursion. Each exercise includes code snippets with expected outputs, illustrating how to manipulate data and perform calculations. The final exercise shows how to calculate powers using a recursive function.

Uploaded by

name name2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

1

CMP 132
Computer Programming

Arrays, Functions, Recursive Functions Examples

 2003 Prentice Hall, Inc. All rights reserved.


2

What does this C++


programs output on screen?

 2003 Prentice Hall, Inc. All rights reserved.


3

Exercise (1)

#include <iostream> Main


using namespace std; i 0 1 2 3 4 5
int sum(int pt[], int n) Pt[i] 0 2 4 6 8 stop
{
int i, temp = 60;
sum i Pt[i] Temp=60
for ( i = 0; i < n; ++i)
{ 0 0 60
temp += pt[i];
}
1 2 62
return temp; 2 4 66
}
int main()
3 6 72
{ 4 8 80
int i, total;
int pt[10]; 5 -- stop
for ( i = 0; i < 5; i++)
pt[i] = i*2;
total = sum(pt, 5); Output:
cout << "total= "<<total Total= 80
<< " \n i= " << i+10 << endl; i= 15
return 0;
}
 2003 Prentice Hall, Inc. All rights reserved.
4

Exercise (2)

#include <iostream>
using namespace std; j n Fun(j) m=5
int Fun(int n)
0 0 0 5
{
n*3; 1 1 1 6
return n; 2 2 2 8
} 3 3 3 11
int main()
4 4 4 15
{
int j, m = 5; 5 stop stop stop
for( j = 0; j < 5; j++)
m = m + Fun(j); Output:
cout << m << "\n";
15
return 0;
}

 2003 Prentice Hall, Inc. All rights reserved.


5

Exercise (3)

#include <iostream>
using namespace std; j n Fun(j) m=5
int Fun(int n)
0 0 0 5
{
n= n*3; 1 3 3 8
return n; 2 6 6 14
} 3 9 9 23
int main()
4 12 12 35
{
int j, m = 5; 5 stop stop stop
for( j = 0; j < 5; j++)
m = m + Fun(j); Output:
cout << m << "\n";
35
return 0;
}

 2003 Prentice Hall, Inc. All rights reserved.


6

Exercise (4)

#include <iostream> j n Sub1(j) m=5


using namespace std; 0 -1 -1 6
int sub1(int n)
1 0 0 6
{
n--; 2 1 1 5
return n; 3 2 2 3
} 4 3 3 0
int main()
5 4 4 -4
{
int m = 5; 6 5 5 -9
for(int j = 0; j < 10; j++) 7 6 6 -15
m - = sub1(j); 8 7 7 -22
cout << m << "\n"; 9 8 8 -30
return 0;
10 Stop
}
Output:
-30

 2003 Prentice Hall, Inc. All rights reserved.


7

Exercise (5)
#include <iostream>
using namespace std;
int calculatePower(int, int);
int main() {
int base, powerRaised, result;
cout << "Enter base number: ";
cin >> base;
cout << "Enter power number(positive integer): ";
cin >> powerRaised;
result = calculatePower(base, powerRaised);
cout << base << "^" << powerRaised << " = " << result;
return 0;
}
int calculatePower(int base, int powerRaised) {
if (powerRaised != 0)
return (base*calculatePower(base, powerRaised-1));
else
return 1;
}
 2003 Prentice Hall, Inc. All rights reserved.
8

• Using Base= 2, Power=3

Base powerRaised Result= calculatePower(base, powerRaised != 0


powerRaised-1);
2 3 2* calculatePower(2, 2) T
2*2*calculatePower(2,1) T
2*2*2*calculatePower(2,0) F
Rusult = 2*2*2*1 = 8

Output:
Enter base number:2
Enter power number(positive integer):3
2^3= 8

 2003 Prentice Hall, Inc. All rights reserved.

You might also like