Tuesday, July 28, 2009

How to write a function in C that takes three parameters?

Write a function in C that takes 3 parameters: the address of a 2-dimensional array of type int, the number of rows in the array, and the number of columns in the array. Have the function calculate the sum of the squares of the elements. For example, for the array nums that is pictured as:


23 12 14 3


31 25 41 17


the call to the function might be


sumsquares ( nums, 2, 4 ) ;


and the value returned would be 4434. Write a short program to test the function.

How to write a function in C that takes three parameters?
You are seriously going to be the worst programmer ever, if you even pass the class your in. According to your profile, you haven't asked a question yet, where you needed someone to do your assignment for you, and your not even willing to pay us for our time.
Reply:In C, when you pass an array as an argument, only the last dimension can be unknown.... but a two-dimensional array can be flattened:





int sumsquares(const int* array, int rows, int cols)


{


int elements = rows * cols;


int sum = 0;


while (elements-- %26gt; 0)


sum += array[elements] * array[elements];


return sum;


}


No comments:

Post a Comment