6 Replies - 1409 Views - Last Post: 25 November 2011 - 07:51 AM Rate Topic: -----

#1 saljanjan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-December 10

getting the sum in array in c++

Posted 24 November 2011 - 09:10 PM

[When I compile the program compiler just sit there and does not do anything; not giving me the sum of the elements in the array. How can get the sum of this array?

#include <iostream>
using namespace std;

int main() {
    int a[1000];       // Declare an array of 1000 ints
    int n = 0;         // Number of values in a.

    while (cin >> a[n]) {
        n++;
    }

    int sum = 0;       // Start the total sum at 0.
    for (int i=0; i<n; i++) {
        sum = sum + a[i];  // Add the next element to the total
    }
    
    cout << sum << endl;
    
    return 0;
} 



MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 November 2011 - 04:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: getting the sum in array in c++

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: getting the sum in array in c++

Posted 24 November 2011 - 09:49 PM

Please use code tags.

When is cin >> a[n] going to be false? Is the loop going to continue while it's true?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5615
  • View blog
  • Posts: 14,693
  • Joined: 18-April 07

Re: getting the sum in array in c++

Posted 24 November 2011 - 10:48 PM

And what makes you think this loop...

while (cin >> a[n]) {
   n++;
}



is ever going to end? Just because "n" is higher than the number of spots in the array? Nothing here says that. This will just keep on writing right into memory.

Translation to you, the program will continue to accept input until you get tired of entering values or you run out of memory to write to.

:)

P.S. It is dangerous to be running code you "found on the Internet" without having a clue as to what it could possibly be doing.

This post has been edited by Martyr2: 24 November 2011 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: getting the sum in array in c++

Posted 25 November 2011 - 02:29 AM

Also ... if you were to enter 1001 entries into your array ... do you want to prevent that?
Also ... best practice is not to use 'magic numbers' ...

Instead code like this ...

const int MAX_SIZE = 1000; // max num of array elements

// ...

int ary[ MAX_SIZE ];
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: getting the sum in array in c++

Posted 25 November 2011 - 04:59 AM

Quote

When I compile the program compiler just sit there and does not do anything; not giving me the sum of the elements in the array.


Compiling only builds the executable, it does not actually run the program! Unless, of course, you chose an option like Compile and Run, which I think is an option in some IDEs.

If you did actually run the program, I'm guessing you might need to read this.
Was This Post Helpful? 0
  • +
  • -

#6 newuser007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 11

Re: getting the sum in array in c++

Posted 25 November 2011 - 07:35 AM

#include <iostream>
using namespace std;

int main() 
{
	const int s=3;     //Declare s=999 for array of 1000 ints
    int a[s];        
    int n = 0;         // Number of values in a.


	do
	{
		cout<<"\nENTER ELEMENT "<<n+1<<":";
		cin>>a[n];
		n++;
	}while(n<4);		//replace it with n<1000
    //while (cin >> a[n]) {
    //    n++;
   // }

    int sum = 0;       // Start the total sum at 0.
    for (int i=0; i<n; i++) 
	{
        sum = sum + a[i];  // Add the next element to the total
    }
    
    cout <<"\nSUM:"<< sum << endl;
    
    return 0;
} 





Quote

I've re-edited your code...n its working fine..

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: getting the sum in array in c++

Posted 25 November 2011 - 07:51 AM

newuser007, it's not very helpful to hand somebody working code. They're not going to learn anything from it. Even worse, it encourages others to post "gimme teh codez" questions. We get plenty of those as it is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1