You are here:

C/functions dont work- beginers

Advertisement


Question
-------------------------
Followup To
Question -
-------------------------
Followup To
Question -
-------------------------
Followup To
Question -
I run this code:


#include <stdio.h>
int i=0,n;
int sum=0, result;

int pair(int n);
int uneven(int n);

void main()
{

do
{
  puts ("print positve integers");
  scanf ("%d", &n);
  ++i;
  if (n==-1)
     break;
  else {
  if (n<0)
     {
     puts ("numbers has to be positve integers");
     i=0;
     n=0;
     }
       }
  if (i==1||i==3||i==5)
     {
      int pair(int n);
     }
  else
     {
  if (i==2||i==4||i==6)
     {
     int uneven(int n);
     }
     }
}
  while (n!=-1);
  result=10-sum%10;
  if (result==10)
  result=0;
  printf ("sum is %d\n", sum);
  printf ("bikoret is %d\n", result);

}

int pair(int n)
{
n*=2;
sum+=n;
return sum;
}
int uneven(int n)
{
n*=1;
sum+=n;
return sum;
}

1. once I have added the functions I get result=0. when the caluclation (n*=1;sum+=n; ) was in main() it worked OK. Why is that?!
2. in line 26 I check if it is pair or uneven in a primitive way. how would I do it better?
Answer -
It will be easier for me to debug this code, if I know what it is doing!
So, please tell me what was the problem statement (for which you wrote code).
And also input and expected output?

-ssnkumar

The original task is to take as an input unlimited quantity of positive integers. The delimeter is -1, which means after I write -1, the program quits. The goal is to calculate sort of inspection digit, based on the following algorithm:
first digit multiple by 1
second digit multiple by 2
third digit multiple by 1
forth digit multiple by 2
etc...
then sum the results of the above, let's say it is 24. the inspection digit has to complete to the closest round number: 10/20/30/40/50/60 etc... so the inspection digit of this example would be 6.
Hope I made myself clear inough now.
Answer -
>then sum the results of the above, let's say it is 24. the >inspection digit has to complete to the closest round >number: 10/20/30/40/50/60 etc... so the inspection digit >of this example would be 6.

How did you get 6?
First digit is 2 and second is 4.
Multiply first digit by 1 = 2 * 1 = 2
Multiply 2nd digit by 2 = 4 * 2 = 8.
Total is 10. But, how did you get 6?

-ssnkumar

again, from start: my example was:
input: 31348.
8+4*2+3+1*2+3= 24 this is the sum of the everything.
then to comlete to 30 you need 6.
sorry for confusion.

Ronen
Answer -
I have modified your code and have added indentation (it helps in easy understanding). Here is the code:

#include <stdio.h>

int sum = 0, result;

int pair(int *n);
int uneven(int *n);

main()
{
       int i = 0, n;

       do
       {
         puts ("print positve integers");
         scanf ("%d", &n);
         ++i;
         if (n == -1) break;
         else
         {
         if (n < 0)
         {
         puts ("numbers has to be positve integers");
         i = 0;
         n = 0;
         }
         }
         if (i == 1 || i == 3 || i == 5)
         {
         uneven(&n);
         }
         else
         {
         if (i == 2 || i == 4 || i == 6)
         {
         pair(&n);
         }
         }
       }

       while (n != -1);

       result = 10 - sum % 10;
       if (result == 10) result = 0;
       printf ("sum is %d\n", sum);
       printf ("bikoret is %d\n", result);

}

pair(int *n)
{
       (*n) *= 2;
       sum += (*n);
}

uneven(int *n)
{
       (*n) *= 1;
       sum += (*n);
}


ssnkumar, Thank you!
what I dont understand is:
1. the use of syntax: *n. in lines: uneven(int *n), (*n) *= 1
2. In my first message I have asked you, how can I write better the if in line 26. There I check if i is uneven, in a primitive way (later for pair, also). how would I do it in more elegant way?

Thanx again.


Answer
1. In C, there is no call by reference feature. That means, when you pass a parameter to a function, it will have a copy of the parameter and will change the copy. It will not change the copy. So, when you pass n to uneven() and change it inside, it will not be changed when it comes out, since the change was made on the copy of the variable, but not the original variable itself. To overcome this, we can pass the address of the variable and to catch this address, you will have to have pointer as argument. So, now you will be changing the contents of the memory and this change will remain even after leaving that function. That's what I am doing there.
2. Elegent way of doing is to check if it is even or not. And this can be done by % operator, which returns the remainder. As you know when a number is divided by 2, it will have a non-zero remainder if it is odd and 0 if it is even. So, in the if condition you could find the remainder and then do processing accordingly. Or you can do this inside function itself. So, your program will become:
#include <stdio.h>

int sum = 0, result;

int pair(int *n);
int uneven(int *n);

main()
{
     int i = 0, n;

     do
     {
         puts ("print positve integers");
         scanf ("%d", &n);
         ++i;
         if (n == -1) break;
         else
         {
         if (n < 0)
         {
         puts ("numbers has to be positve integers");
         i = 0;
         n = 0;
         }
         }
         pair(&n);
     }
     while (n != -1);

     result = 10 - sum % 10;
     if (result == 10) result = 0;
     printf ("sum is %d\n", sum);
     printf ("bikoret is %d\n", result);
}

pair(int *n)
{
     if (*n % 2) (*n) *= 2;
     else (*n) *= 1;

     sum += (*n);
}  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.