943,918 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1047
  • C++ RSS
Aug 9th, 2006
0

C++

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sum(int[],int);//
  5. int main()
  6. {
  7. int a[]={11,33,55,77};//
  8. int size=sizeof (a)/sizeof(int);//
  9.  
  10. cout << "sum(a,size)="<<sum(a,size)<<endl;
  11. }
  12. int sum(int a[],int n)//
  13. {
  14.  
  15. int sum=0;
  16. for (int i=0;i<n;i++)
  17. sum+=a[i];
  18. int Avg= sum/4;
  19. cout<<"Avg="<<Avg<<endl;
  20. return sum;
  21. }
Last edited by Dave Sinkula; Aug 9th, 2006 at 12:23 am. Reason: Added [code][/code] tags -- learn to use them yourself.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Aug 9th, 2006
1

Re: C++

Quote originally posted by Sashar400 ...
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".
Last edited by Dave Sinkula; Aug 9th, 2006 at 12:31 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 9th, 2006
0

Re: C++

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:
Reputation Points: 11
Solved Threads: 0
Newbie Poster
b2daj is offline Offline
8 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Choice of DataStructure
Next Thread in C++ Forum Timeline: Data From C++ GUI to Excel/Notepad, Application Trouble





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC