In: Electrical Engineering
hi,I have this C++ program,can someone add few comments with explanation what is the logic and what is what?thank you I m total beginner
#include <iostream> using namespace std; int ArraySum(int MyArray[], int size){ int* p = MyArray; int sum = 0; while(p<MyArray+size){ sum += *p; p++; } return sum; } int main() { int MyArray[10] = {4, 0, 453, 1029, 44, 67, 111, 887, 4003, 1002}; cout<<ArraySum(MyArray,10); return 0; }
#include <iostream>// Including this header file defines standard input/output stream //objects. using namespace std;// If functiones (eg:- cout) is used, but not defined in the current //scope, computer needs to know where to check. The code will be written outside of standard //name space int ArraySum(int MyArray[], int size)// The above defined function ArraySum returns the sum //of numbers present in the array called "MyArray" and thereby integer a value....that's why //its "int Array" { int* p = MyArray;//pointer variable int sum = 0;//the variable "sum" is initialized as zero while(p<MyArray+size)// Elements in "MyArray" are added one by one. After the last //element of the array is also added to the preceding elements, the while loop exits returing the //"sum" { sum += *p; p++; } return sum; } int main()//This is the main //function (global function) of the program. function "int //main()" can be called with any numbe of arguments. { int MyArray[10] = {4, 0, 453, 1029, 44, 67, 111, 887, 4003, 1002};// integer Array of //size 10 with elements {4, 0, 453, 1029, 44, 67, 111, 887, 4003, 1002} is defined here. cout<<ArraySum(MyArray,10);//calls the function "ArraySum". Input to the function is the //defined integer array "MyArray". As mentioned before, the function "ArraySum" returns the //sum of elements in the array "ArraySum". return 0;//This is to explicitly return a value. the function return value is the exit //code of the program. Exit code zero is the conventional one to indicate that "the program //execution was successful" }