Learnem Educational
May 18, 2012, 01:34:50 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Some Confusing question to expert....  (Read 1890 times)
nicholas80
Newbie
*
Posts: 2


View Profile
« on: October 02, 2002, 11:03:08 AM »

Hi I'm newbie in programming and having troublesome with some of my homework...anybody can help me?? Here is the question.

Write an iterative  function called OperateFloatArray that take in a float array
floatarray, an integer parameter numrec (which determines the number of float values in the array) and a character parameter op (which stores any one of the 4 characters '+', '-',  '/' and '*'). If floatarray = {1.0,2.0,3.0} and numrec = 3;
OperateFloatArray (floatarray, numrec, '+') returns 6.0 (1.0 + 2.0 + 3.0),
OperateFloatArray (floatarray, numrec, '-')  returns -4.0 (1.0 - 2.0 - 3.0).
OperateFloatArray (floatarray, numrec, '/')  returns 0.16667 ((1.0 / 2.0) / 3.0).
OperateFloatArray (floatarray, numrec, '*') returns 6.0 (1.0 * 2.0 * 3.0).

I got the model function cannot make it works coz i'm useless in programming. Here is it....

float OperateFloatArray (float floatarray[], int numrec, char op)
{

int x;
float sum=0, product=1;
float diff=floatarray[0], quotient=floatarray[0];

if (op == '+')
   {   for (x=0; x < numrec; x++)
         sum = sum + floatarray
  • ;
     return sum;
   }

if (op == '-')
   {   for (x=1; x < numrec; x++)
         diff = diff - floatarray
  • ;
      return diff;
   }



Any helps is really appreciated....Thanks in advance.....
« Last Edit: January 01, 1970, 12:00:00 AM by nicholas80 » Logged

I'm not stupid
MrDeed
Newbie
*
Posts: 8


View Profile
« Reply #1 on: October 02, 2002, 02:44:22 PM »

[code:19eclkhd]
float OperateFloatArray (float floatarray[], int numrec, char op) {
   int x;
   float result = floatarray[0];

   switch (op) {
     case '+':
       for(x = 1; x < numrec; x++)
         result += floatarray[x];
       break;
     case '-':
       for(x = 1; x < numrec; x++)
         result -= floatarray[x];
       break;
     case '/':
       for(x = 1; x < numrec; x++)
         result /= floatarray[x];
       break;
     case '*':
       for(x = 1; x < numrec; x++)
         result *= floatarray[x];
       break;
     default:
       puts("Not a valid input, try again!");
       exit(1);
   }
   return result;
} [/code:19eclkhd]

There you go..=)
« Last Edit: January 01, 1970, 12:00:00 AM by MrDeed » Logged
nicholas80
Newbie
*
Posts: 2


View Profile
« Reply #2 on: October 04, 2002, 03:22:39 AM »

The program seems like too complicated. I'm actually only need the main function of the program which the output I need is fixed to the program code. No user input is required......
« Last Edit: January 01, 1970, 12:00:00 AM by nicholas80 » Logged

I'm not stupid
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!