I need help with the program

Here is the question

1. Write a function convertArray ( ) that takes an array of integers a, and the size of that array as arguments. Then it converts the negative elements of a, if any exist, to their positive values. convertArray ( ) returns true if at least one of the elements of a has been converted, false otherwise. Write an application program to test the function convertArray ( ).

here is my progtam
#include <iostream>
using namespace std;
bool convertArray ( int a[],int size){
for(int i=0; i<size;i++){
if (a[i]<0){
a[i]=(a[i]+(a[i]*a[i]));
return(true);
}
else{
return(false);
}
}
}
void main(){
int x[10]={0,-1,-2,3,4,5,6,10,20,30};
if( convertArray(x,10)==true){
cout<<"the negitive values in the array has been changed"<<endl<<"the new values are = "<<endl;

for (int i=0;i<10;i++){
cout<<x[i];
}
}
else{
cout<<"the Array doesnot contain any negative values"<<endl;
}
}
but the program always displays:"the Array does not contain any negative values"
although you can see there are negative numbers in the array


Last edited on
You use return before the loop terminates therefore you get a false value since the first number is 0

you should try this instead:
1
2
3
4
5
6
7
bool returnvalue = false;
for (int i = 0; i< size; i++){
  if (a[i] < 0){
    a[i] *= -1
    returnvalue = true;}
}
return returnvalue;
Topic archived. No new replies allowed.