Cpe102 HW11 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Name___________________________________________ ID#_________________ Date___________

COMPUTER ENGINEERING
CHIANG MAI UNIVERSITY
CPE102 Computer Programming
Homework 11

1. Source Code Function


Function Array
Source Code Function
Source Code ( Function )
#include <iostream>
using namespace std;
int myFunction1(int *x){
return (*x)*(*x);
}
int myFunction2(int x[],int N){
return x[N]*x[N];
}
int myFunction3(int x[],int *N){
return x[*N]*x[*N];
}
int myFunction4(int &x){
return x*x;
}
int main () {
int a[] = {1,2,3,4,5};
cout << "a[0]*a[0] = " << myFunction1(
cout << "\na[1]*a[1] = " << myFunction1(

);
);

cout << "\na[2]*a[2] = " << myFunction2(

);

cout << "\na[3]*a[3] = " << myFunction3(

);

cout << "\na[4]*a[4] = " << myFunction4(

);

return 0;
}

a[0]*a[0]
a[1]*a[1]
a[2]*a[2]
a[3]*a[3]
a[4]*a[4]

=
=
=
=
=

1
4
9
16
25

2. ( Run )
Source Code

#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b[] = {10,12,25,31,15};
int *c = &a;
int *d = b;
int **e = &c;
cout << *(a+d) << ;
cout << *d++ << ;
cout << *d << ;
cout << (*c)++ << ;
cout << *c << ;
cout << **e << ;
return 0;
}

3. ( Run )
Source Code
#include <iostream>
using namespace std;
int main()
{
int a = 2, b = 4, c = 10;
int *d = &a, *e = &b;
int * &f = d;
*d *= 3;
f = &c;
*e = *d + a;
cout << a << " ";
cout << b << " ";
cout << c << " ";
return 0;
}

You might also like