Sum of prime numbers of an array

I'm trying to figure out where the problem is but im kinda lost.I'm not familiar with functions or vectors yet so i dont know?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 int main(){
	
	int A[10], i, count = 0,sum=0;
	for (i = 0; i < 10; i++)
	{
		cout << "A[" << i << "]=";
		cin >> A[i];
	}
	for (i = 2; i<= A[i]/2; ++i)
	{
		if (A[i] % i == 0)
		{
			count = 1;
			break;
		}
		if (count == 0){
			sum = sum + A[i];
		}
	}
	cout << "sum =" << sum << endl;
}
Lines 4-8: A[] is uninitialized. i.e. contains garbage.

line 9: You're trying to use A[i] as part of the termination condition of the loop, but you've never initialized A[i]. Since you don't know how large i will get to find the first 10 primes, you really want the termination condition to be count < 10.

Line 11: Again, you're trying to use an uninitialized array location.

Line 14: You're breaking out of the for loop on the first non-prime number.

Lines 13,16: You're using count as a flag to indicate if a number is prime or not. It would be better to used count as the number of prime numbers you've inserted in the array.

Line 17: Again, you trying to add an uninitialized array location.

What you're missing here is that when you find a prime number, you want to add it to the array and to the sum.


AbstractionAnon
that was a great explanation. thank you for sharing your time :).
Last edited on
Topic archived. No new replies allowed.