Changing multiple values of integer array passed to a function – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#2737 siteicon   siteicon   siteicon  

Changing multiple values of integer array passed to a function

A program to pass integer array to a function and change many values in it based on a condition. Then reading such values in the calling function.

Learning Objectives

  • Changing multiple values of an array passed to a function.
  • Learning how multiple value can actually be affected in a functions, although it can return just one value.

Source Code

TC++ #2737

 

Source Code

Run Output

-- Array members before change --
19 20 34 55 99 102 12
-- Array members after change --
19 21 35 55 99 103 13

Code Understanding

int even2odd(int [],int); int disp_arr(int [],int);
Two functions prototypes with integer array passed as reference with another variable passed as value of length of array.

int arr[]={19,20,34,55,99,102,12}; //Array initialised
int len=sizeof(arr)/sizeof(arr[0]); //Array length found

cout<<“– Array members before change — “<<endl;
disp_arr(arr,len);
Array display function called which simply displays the function.

even2odd(arr,len);
This function traversed the whole array up to len and converts any value which is even to its higher odd number.

cout<<“– Array members after change — “<<endl;
disp_arr(arr,len);
Array display after change. Since the original array itself is changed, same array is passed to the function.

int even2odd(int array[],int len) {
A function header where array is passed by reference and length is passed by value.

for(int i=0;i<len;i++){ //Array is traversed till the last point as it is passed as len

if(array[i]%2==0) array[i]+=1; 
change passed array members if found even to higher odd number by adding 1 to it. Even number testing is done by divisibility by 2 test using array[i]%2==0

}return 0;}
//If function is successful 0 is returned.

int disp_arr(int array[],int len)
A function header for display purposes where array is passed by reference and length is passed by value.
{
for(int i=0;i<len;i++) { cout<<array[i]<<” “;} //The array display loop
cout<<endl; //Extra line when array is displayed

return 0; //Returned 0 when function is successful
}

Notes

  • While initial development of a function one may feel that ability to return just value by a function is its serious limitation, but now one has seen the demo of affecting multiple values within a function. Actually value return is often used for success condition of the function and affected values can be actually be done through referencing.


Suggested Filename(s): fn-arr-change.cpp, fnarrchg.cpp



Share

sunmitra| Created: 17-Jan-2018 | Updated: 17-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back