helpppppppppppppppppppp....function

i m doin this code......here shows all the even numbers from random nubers bt i want to find first 3 even nuber....any solution?
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

void sum_even(int , int ,int );

void sum_even(int a[] , int size ,int sum ){
sum=0;
for(int i=0;i<size;i++){
if(a[i]%2==0){
cout<<"Even Found position at "<<i<<" Number is "<<a[i]<<endl;
sum=sum+a[i];

}
}
cout<<endl<<"Sum "<<sum;
}


int main(){
int size ,sum=0 ;
cout<<"Enter Size ";
cin>>size;

int a[size];
srand(time(0));
cout<<"Randomly number generate "<<endl;
for(int i=0;i<size;i++){
a[i]=rand()%50;
}

for(int i=0;i<size;i++){
cout<<a[i]<<" ";
}
cout<<endl;
sum_even(a,size,sum);



}
1
2
3
4
cout << "Enter Size ";
	cin >> size;

	int a[size];


You cant do this.

You can use vectors instead. If you wanna stick to arrays, you will have to allocate memory for that array.

int* a = new int[size];
If I am not wrong, this is what you want to do:
Getting the sum of first 3 random numbers:

So, you have to modify your function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void sum_even(int a[] , int size ,int sum ){
sum=0;
int a1=0;
for(int i=0;i<size;i++){
if(a[i]%2==0&&a<3){
cout<<"Even Found position at "<<i<<" Number is "<<a[i]<<endl;
sum=sum+a[i];
a1++;
}
if(a1==3){
break;
}
}
cout<<endl<<"Sum "<<sum;
}
Last edited on
You dont undestand what i m sayin???there is nothing error in my code.......i am tring to find first three even number
I can see that. And the modified function is capable to do that.
1
2
3
4
cout<<"Enter Size ";
cin>>size;

int a[size];


This is an error. You cannot do this in c++.
txxx a lot
Topic archived. No new replies allowed.