Question

In: Electrical Engineering

hi,I have this C++ program,can someone add few comments with explanation what is the logic and...

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;
}

Solutions

Expert Solution

#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" 
}

Related Solutions

i need C++ program with possible comments what is going on,(a) Write a program that reads...
i need C++ program with possible comments what is going on,(a) Write a program that reads a char as input, and determines if it is a lowercase letter, uppercase letter, a digit or something else (call the last one a special character).
Can you add more comments explaining what this code does? i commented what I know so...
Can you add more comments explaining what this code does? i commented what I know so far #include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; int sleepLength = 2; // used to represent work void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); // decrement by 1 if rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); //shows current reader and data sleep(sleepLength); // 1 second of "work" is...
Hello I need this written in C# with a few comments thanks Modify the Patient class...
Hello I need this written in C# with a few comments thanks Modify the Patient class to reference a demographic object that contains the patient phone number, email and next of kin. Create a demographic class to store this information. Make sure to create a constructor in the class for this information. Modify the Patient constructor to pass in this additional information at the time of instantiation. Modify the display method to display all the data HERE IS THE CODE...
Here is a program that computes Fibonacci series. Can you add comments to variables and functions...
Here is a program that computes Fibonacci series. Can you add comments to variables and functions of every functions? For example, what are these variables meaning? What "for" function do on this program? Describe important section of code to text file. For example, which section of this code important? Why? Thank you all of help. #include<iostream> using namespace std; int main() { int arr[100]; int n1, n2; cin>>n1; arr[0] = 0; arr[1] = 1; for(int i = 2;i<n1;i++){ arr[i] =...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include<iostream> #include<string> #include<exception> #include <cstdlib> #include<stdio.h> #include<map> #include <cctype> #include<Windows.h> #define INPUT_SIZE 8 using namespace std;...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it. The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms: Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
If answer can be shown using a c++ program and leave comments it will be very...
If answer can be shown using a c++ program and leave comments it will be very appreciated!!! A bank charges $10 per month plus the following check fees for a commercial checking account: $0.10 each for fewer than 20 checks $0.08 each for 20-39 checks $0.06 each for 40-59 checks $0.04 each for 60 or more checks The bank also charges an extra $15.00 if the balance of the account falls below $400 (before any check fees are applied). Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT