0

I'm trying to write a loop function that returns the result of a computation given a positive integer nVal.

Given nVal, the function computes for 1 + 2 - 3 + 4 - 5 + ... + nVal. So for example if nVal = 4, the function returns the result of 1 + 2 - 3 + 4. The solution I made isn't looping nVal properly and I don't know how to fix it.

Any fixes that I can try?

Here's my code so far (Btw I'm using C language):

#include <stdio.h>

int getValue (nVal)
{
  int i, nResult = 0;
    
  for (i = nVal; i <= nVal; i++) 
  {
    if (i % 2 == 0) 
    {
      nResult += i;
    } 
    else if (i % 2 == 1)
    {
      nResult -= i;
    }
  }
  if (nVal == 1)
    nResult +=i + 1;
      
  return nResult;
}
    
int main ()
{
  int nVal, nResult; 
    
  printf ("Enter n value: ");
  scanf ("%d", &nVal);
    
  nResult = getValue(nVal);
  printf ("Result: %d", nResult);
    
  return 0;
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • 1
    Think about the loop `for (i = nVal; i <= nVal; i++)` for a while... And please learn how to use a *debugger* to be able to solve (or at least find) such problems in a fraction of the time it takes to post questions here. – Some programmer dude Jan 06 '21 at 09:58
  • What does a debugger do? I'm still pretty new to C programming – Marty Mcfly Jan 06 '21 at 10:08
  • `int getValue (nVal) {...}`: never write code like this. It should be `int getValue (int nVal) {...}`. The compiler should give a warning such as `warning: type of 'nVal' defaults to 'int'`. – Jabberwocky Jan 06 '21 at 10:12
  • this loop for (i = nVal; i <= nVal; i++) excute only 1 time ,but in your exercise ,you should summed number from 1 to nVal with changing the sign each time – MED LDN Jan 06 '21 at 10:12
  • Go to your favorite search engine and search for `debugger for ` (where you of course replace `` with the actual environment you're using, like Visual Studio, Linux console, or similar). If you want to be more than a "beginner" forever, knowing how to use debuggers is crucial. With a debugger you can step through your code statement by statement while monitoring variables and see how their values change. – Some programmer dude Jan 06 '21 at 10:20
  • okay ill take note, thanks for the tips, it really helps – Marty Mcfly Jan 06 '21 at 10:28
  • https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jan 06 '21 at 11:50

1 Answers1

1

Because the numbers are consecutives,I removed the if elseif statement ,I use the variable k to reverse '+' to '-',my loop start from 2 to nVal

Your loop

for (i = nVal; i <= nVal; i++)

runs just 1 time

so ,you should changed it to

for (i = 2; i <= nVal; i++)

and (nResult=1)because in your exercise the sign changed from the third number, not from the second number

here:

if (nVal == 1)
nResult +=i + 1;

If I write as 1 input ,the output is 2 ,I remove this line

my code:

#include <stdio.h>

int getValue (nVal)
{
int i, nResult = 1;
int k=1;
for (i = 2; i <= nVal; i++)
{
     nResult+=i*k;
     k*=-1;
}
return nResult;

}

int main ()
{
int nVal, nResult;
printf ("Enter n value: ");
scanf ("%d", &nVal);

nResult = getValue (nVal);

printf ("Result: %d", nResult);

return 0;

}
MED LDN
  • 684
  • 1
  • 5
  • 10