/**
* A. This uses common array functionality and symbolism
*/
#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int ages[], int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
ages[i] = newAge[i];
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(age, 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int ages[], int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
ages[i] = newAge[i];
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(age, 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
/**
* B. Or by using the power of pointer, we can do also exactly like
* the above
*/
#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int *ref, int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
*( ref + i) = newAge[i] ;
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(age, 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int *ref, int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
*( ref + i) = newAge[i] ;
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(age, 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
/**
* C. In this form, the calling parameter argument is used as the
* first array index
*/
#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int *ref, int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
*( ref + i) = newAge[i] ;
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(&age[0], 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
using namespace std;
#include <ctime>
#include <cstdlib>
void setAges(int *ref, int len){
int newAge[] = {10,20,30,40};
int i;
for(i=0; i< len; i++){
*( ref + i) = newAge[i] ;
}
}
int main()
{
int age[] = {1,2,3,5};
setAges(&age[0], 4);
int i;
for(i=0;i<4;i++){
cout << age[i] << endl;
}
return 0;
}
No comments:
Post a Comment