Загрузка страницы

Arrays as a Parameter || Data structures and Algorithms || Saral Bhasha me

Arrays as a Parameter || Data structures and Algorithms || Saral Bhasha me

#arraysasparameter #arrays #parameter #datastructures #algorithms #aktu #akturesults #ugc

C++ Passing Arrays to Functions
C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index.

If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

Way-1
Formal parameters as a pointer as follows −

void myFunction(int *param) {
.
.
.
}
Way-2
Formal parameters as a sized array as follows −

void myFunction(int param[10]) {
.
.
.
}
Way-3
Formal parameters as an unsized array as follows −

void myFunction(int param[]) {
.
.
.
}
Now, consider the following function, which will take an array as an argument along with another argument and based on the passed arguments, it will return average of the numbers passed through the array as follows −

double getAverage(int arr[], int size) {
int i, sum = 0;
double avg;

for (i = 0; i size; ++i) {
sum += arr[i];
}
avg = double(sum) / size;

return avg;
}
Now, let us call the above function as follows −

#include iostream
using namespace std;

// function declaration:
double getAverage(int arr[], int size);

int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;

// pass pointer to the array as an argument.
avg = getAverage( balance, 5 ) ;

// output the returned value
cout "Average value is: " avg endl;

return 0;
}
When the above code is compiled together and executed, it produces the following result −

Average value is: 214.4
As you can see, the length of the array doesn't matter as far as the function is concerned because C++ performs no bounds checking for the formal parameters.

Видео Arrays as a Parameter || Data structures and Algorithms || Saral Bhasha me канала Codemasters
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
28 августа 2020 г. 16:30:04
00:13:09
Другие видео канала
Cache Mapping Techniques 3 || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques 3 || Computer Organisation and Architecture ||Detailed ExplanationStructures and Functions|| Data Structures and Algorithms || Saral Bhasha meStructures and Functions|| Data Structures and Algorithms || Saral Bhasha meStatic And Dynamic Memory Allocation || Data Structures and Algorithms || Saral Bhasha meStatic And Dynamic Memory Allocation || Data Structures and Algorithms || Saral Bhasha meTime And Space Complexity || Data Structures and Algorithm || Saral Bhasha meTime And Space Complexity || Data Structures and Algorithm || Saral Bhasha meCache Mapping Techniques || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques || Computer Organisation and Architecture ||Detailed ExplanationSandeep Maheshwari Inspires || Powerful Inspirational SpeechSandeep Maheshwari Inspires || Powerful Inspirational SpeechInstruction cycle State Transition Diagram ||Computer Organisation and Architecture||Saral Bhasha meInstruction cycle State Transition Diagram ||Computer Organisation and Architecture||Saral Bhasha meFloating Point Representation || Computer Architecture || HindiFloating Point Representation || Computer Architecture || HindiArrays Basics || Data Structures and Algorithms || Saral Bhasha meArrays Basics || Data Structures and Algorithms || Saral Bhasha meShri Ram Chandra Kripalu Bhajman ll by THE Gayak Brahmin ll Shri Ram Bhumi Pujan SpecialShri Ram Chandra Kripalu Bhajman ll by THE Gayak Brahmin ll Shri Ram Bhumi Pujan SpecialHardwired Control Unit | Computer Organization and Architecture | Saral Bhasha meHardwired Control Unit | Computer Organization and Architecture | Saral Bhasha meIntroduction to Computer Organisation and Architecture || Computer Organisation and ArchitectureIntroduction to Computer Organisation and Architecture || Computer Organisation and ArchitectureFloating point representation ||  Computer Architecture || HindiFloating point representation || Computer Architecture || HindiWhat every Indian Student must do || Don't miss these tips by Sir Walter lewin || Physics wallahWhat every Indian Student must do || Don't miss these tips by Sir Walter lewin || Physics wallahPointers to a Structure || Data Structures and Algorithms || Saral Bhasha mePointers to a Structure || Data Structures and Algorithms || Saral Bhasha meCache Mapping Techniques 2 || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques 2 || Computer Organisation and Architecture ||Detailed ExplanationWho is a Teacher || unke kartavya kya haiWho is a Teacher || unke kartavya kya haiStructures in C || Data Structure and Algorithms || Saral Bhasha meStructures in C || Data Structure and Algorithms || Saral Bhasha meIntroduction to Data Structures || Data Structure and Algorithms || Saral Bhasha me!!!Introduction to Data Structures || Data Structure and Algorithms || Saral Bhasha me!!!Addressing modes | Computer Organisation and architectureAddressing modes | Computer Organisation and architectureYou Didn't come this far to only come this far!!!Ultimate motivation for jee,neet students!You Didn't come this far to only come this far!!!Ultimate motivation for jee,neet students!
Яндекс.Метрика