Hi
I am new to c++ and I have a simple program that I am trying to understand .Here is the program If anyone can put some comments to what the codes mean I would be grateful.//Explain Please what these lines mean.

#include <iostream>
using namespace std;
 
int sum(int[],int);//
int main()
{
 int  a[]={11,33,55,77};//
 int size=sizeof (a)/sizeof(int);//
 
 cout << "sum(a,size)="<<sum(a,size)<<endl;
}
int sum(int a[],int n)//
{ 
 
   int sum=0;
   for (int i=0;i<n;i++)
   sum+=a[i];
   int Avg= sum/4;
   cout<<"Avg="<<Avg<<endl;
 return sum;
}

Recommended Answers

All 2 Replies

Explain Please what these lines mean.

Better still would be to take an initial guess to help others help you by at least having a frame of reference.

But...

#include <iostream>
using namespace std;

int sum(int[],int);// (1) function declaration or prototype
int main()
{
   int  a[]={11,33,55,77};// (2) initialization of an array
   int size=sizeof (a)/sizeof(int);// (3) initialize size with number of elements in a

   cout << "sum(a,size)="<<sum(a,size)<<endl;
}
int sum(int a[],int n)// (4) function definition
{

   int sum=0;
   for ( int i=0;i<n;i++ )
      sum+=a[i];
   int Avg= sum/4;
   cout<<"Avg="<<Avg<<endl;
   return sum;
}

More details:

  1. This says, this function will exist and this is how it should be used.
  2. The size of the array is left to the compiler to figure out based on the number of elements initializing it.
  3. The number of elements in an array is equal to the total size divided by the size of the first member.
  4. The definition of the function: "the meat".

they simple dont have any meaning while they dont provide us some meaningful comments but itd be useful the see declarations or some major steps of program by trying to get attention :mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.