Monday, May 24, 2010

Help with c++ functions program?

Hey i have this c++ program i need to make for school with functions to find the sum average highest lowest and find numbers





here is mine so far.





#include %26lt;iostream.h%26gt;


void printAcross (int L[], int size);


void printDown (int L[], int size);


int summation (int L[], int size);


void loadInput (int L[], int size);


float average (int L[], int size);


int largest (int L[], int size);














int main()


{


int A[10] = {0};


cout %26lt;%26lt; "ACROSS: ";


printAcross(A,10);


cout %26lt;%26lt; endl %26lt;%26lt; endl %26lt;%26lt; "DOWN: " %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


printDown (A,10);


loadInput (A,10);


int sum;


sum = summation(A,10);


cout %26lt;%26lt; "Sum = " %26lt;%26lt; sum %26lt;%26lt; endl;








return 0;


}





void printAcross(int L[], int size)


{


for (size = 0; size %26lt; 10; size++)


cout %26lt;%26lt; L[size] %26lt;%26lt; " ";


}





void printDown(int L[], int size)


{


for (size = 0; size %26lt; 10; size++)


cout %26lt;%26lt; L[size] %26lt;%26lt; endl;


}





int summation (int L[], int size);


{


for(size = 0; size %26lt; 10; size++


{


sum = sum + L[size];


return sum;


}


void loadInput(i

Help with c++ functions program?
You haven't copied the whole code, it's either has been truncated by Yahoo Editor or you didn't mark it all. Anyway, I have few notes:





* You passed the argument "size" and still haven't made a logical use of it:


- You used it as a control variable!! You don't need to pass variables that you don't need their values and will use just locally and temporarily, you can create them inside the function or let the for loop create it for you, e.g.,


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





- Usually when you have a parameter for an array, you define another parameter of type int to indicate the array's length, "size" variable would have been a good choice, but then you would have to modify the for loops to:





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





what's the point of using a variable rather than the constant integral 10 ?? Let's say you decided to change the array's length, with your way of doing it you will have to go through all the for loops where you typed 10 then change it to the new value, but when using a variable you would just have to change it once.





My suggestion is to declare you array this way:


cont int size = 10;


int A[size] = {0};





.....


// passing the array along with its length


yourArrayFunction ( L, size );





...





then using the constant size in all of your for loops..








*The summation function


-Did you even try to compile the code? I don't see where you have declared the sum variable, declare it just above the for loop and it will be fine, otherwise you will get "undefined variable" compilation error.





- You don't have to declare a variable to store the return value then output it, you can directly use it with cout like this:


cout %26lt;%26lt; "Sum = " %26lt;%26lt; summation(A,10) %26lt;%26lt; endl;








*If your code doesn't fit here, upload it to http://pastebin.com then paste the link you will get here so we can help you with the other functions.
Reply:Try www.amandalasha.blogspot.com there are many different types of programing examples. This may just help. Report It

Reply:You have a question?
Reply:The average is just the sum divided by the number of elements in your array.





For highest and lowest just use a for loop. Go through each element and find the max or min.





Based on your code, you're on a good start and should have the necessary knowledge to finish the other functions.


No comments:

Post a Comment