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

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...
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...
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...
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...
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...
Can someone please explain the following program to me? I have ran it, but do not...
Can someone please explain the following program to me? I have ran it, but do not understand how the answers are being derived. #include<iostream> using namespace std; int num1 = 1; int quiz(int num) { static int n1 = 10; int n2 = 20; n1--; n2++; num1++; if (num == 1) return num1; else if (num < 3) return n1; else return n2; } // quiz main() { cout << quiz(num1) << endl; cout << quiz(num1) << endl; cout <<...
Add logic to tell me to switch to unlimited if I go over $75. Use C++...
Add logic to tell me to switch to unlimited if I go over $75. Use C++ /******************************************************************************* ** Program Name: Verizons Monthly Bill Calculator. ** File Name: file a03.cpp. ** Author: Natassha Quiroz ** Date: October 18, 2020 (updated 10/20/20) ** Assignment: 03 ** Description: The purpose of this program is to calculate the consumers monthly cell ** phone bill based on data usage. ** Sources: Lecture slides, lecture videos and practice programs from lecture video. *******************************************************************************/ #include <iostream> #include...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT