C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

C++

 
0
  #1
Aug 9th, 2006
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++

 
1
  #2
Aug 9th, 2006
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 8
Reputation: b2daj is an unknown quantity at this point 
Solved Threads: 0
b2daj b2daj is offline Offline
Newbie Poster

Re: C++

 
0
  #3
Aug 9th, 2006
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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC