Thursday, July 30, 2009

Well can anyone teach me how to invoke function in c++?

well the assignment is to add 10 points to all the elements in an array and dispaly the array before and after the function is invoked.i tried it but machine came up with kind of error that i couldn't figure out can anyone help me? . plz SE's out there plz tell me that in simple language


i am not that computer geek you see





thanks





tom

Well can anyone teach me how to invoke function in c++?
i'm gonna code down what you need ;) , God i love C++ :P





anyway here it goes:





#include%26lt;iostream%26gt;





void printmyarr( int [ ] , int );





int main ()


{





int myarray[5] = {1,2,3,4,5}; //Define and initialize an array





printmyarr(myarray, 5 );





for(int counter=0; counter %26lt;=4; counter++)


{ myarray[counter] +=10; //Add 10 to each element }





printmyarr(myarray, 5);





return 0;


}





void printmyarr( int thearray [ ] , int arrsize )


{


for (int i=0; i%26lt;=4; i++)


{


cout%26lt;%26lt;thearray[i]%26lt;%26lt;"\n"


}


}
Reply:i is an incrementer, N is the number of items in the array. of course you will need to adjust it to output it the way it should but here is a verysimple start for you:








for (i=0; i%26lt;N; i++)


{


cout %26lt;%26lt; array[i] %26lt;%26lt; endl; //1 line per number


}





for (i=0; i%26lt;N; i++)


{


array[i]+=10;


}





for (i=0; i%26lt;N; i++)


{


cout %26lt;%26lt; array[i] %26lt;%26lt; endl; //1 line per number


}
Reply://


//C++


//








//declare an array of 5 elements


int arr[5];





//initialize it: give each item a value


int i;


for (i = 0; i %26lt; 5; i++)


{


arr[i] = i;


}





//print it out before adding


for (i = 0; i %26lt; 5; i++)


{


cout %26lt;%26lt; arr[i] %26lt;%26lt; endl;


}





//add 10 to each memeber


for (i= 0; i %26lt; 5; i++)


{


arr[i] =+ 10;


}





//print after adding


for (i = 0; i %26lt; 5; i++)


{


cout %26lt;%26lt; a[i] %26lt;%26lt; endl;


}








That's about it.


No comments:

Post a Comment