guys i need help ASAP since i need to submit this miniproject and i need 1 more minor questions need asnwered.

the C++ coding is given below, i need to change it to function type but giving out the same output.

#include <iostream>
#define SIZE 7
using namespace std;

int main()

    int A[ SIZE ] = { 2, 4, 8, 16, 32, 64, 128};
    int i, j; 
    int total = 0; 

    cout <<"Value of array: ";
    for ( j = 0; j <SIZE; j++)
        cout<< A[j] <<" ";

    for ( i = 0; i < SIZE; i++ )
        total += A[i];

    cout << "Total of 2 power n from 2 to 128 are: "<<total;
    return 0;


thanks in advance if anyonemanaged to answer it within 45 minutes from now.

Edited by Dani: Formatting fixed

5 Years
Discussion Span
Last Post by Schol-R-LEA

First off, you need to edit your post so that the code is in [CODE] tags - the forum software does not retain indentation, so the code is unreadable unless you use the tags.

Second, just what do you mean you need to 'change it to function type'? Do you need to break the program into more than one function, or what?


well it is kinda complicated (to me)

but heres the actual question

Modify the program in Figure 1 below to include a user-defined function. The output of the modified program must be same.


Try taking the second for() loop out of the main() function, and put it into a new function starting with the line:

int sum(int* A)

and after the end of the for() loop, add a line

return total;

Now, where the for() loop was, put the following line:

total = sum(A);

This will actually call the function you've written.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.