so I am learning C, and for one of the excercises the website I am learning on, I am supposed to make a program with a recursive function that calculates the sum of the digits of an integer. When using VS code, my program works perfectly every time (to my knowledge, which is slight), but when I paste it into the website's editor, it says that its calculations are wrong. Here is my code:

Code:

#include <stdio.h>
#include <stdlib.h>

int sumOfDigits(int);
int getLastDigit(int);
int popLastDigit(int);

int main() {
   int number;
   int sum;
   scanf("%d", &number);
   sum = sumOfDigits(number);
   printf("%d", sum);
   return 0;
}

int sumOfDigits(int num) {
   int curDigit;
   int result = 0;
   
   if (num>9) {
      curDigit = getLastDigit(num);
      num = popLastDigit(num);
      result += curDigit + ( sumOfDigits(num) );
      return result;
   }  else {
         return num;
   }
}

int getLastDigit(int num) {
   int result;
   result = num%10;
   return result;
}

int popLastDigit(int num) {
   int result;
   int clippedDigit;
   if (num>9) {
      clippedDigit = getLastDigit(num);
      result = (num - clippedDigit) / 10;
      return result;
   }  else {
      return 0;
   }
}

Thank you in advance! Very Happy
Do you have an example of your ideal output? Also was wondering what 'website editor' you are using?

Like an online C compiler?
well, the website does not tell me what number it inputs, but it says that the correct output should be 21, and the actual output is 18.
Edit: I am taking the C Programming: Modular Programming and Memory Management class on Edx.org
the ppl in the code casts seem to think that their editor is great, but it is the stupidest, slowest, most incompetent editor I have ever used!
Could you replace it with something like:


Code:
int sumOfDigits(int num) {
     if (num != 0) {
        return num%10+sumOfDigits(num/10);
    } else {
        return 0;
    }
}


Does that still give you different answers in the course compiler?
Thank you! It worked!

Code:

#include <stdio.h>
#include <stdlib.h>

int sumOfDigits(int);
int getLastDigit(int);
int popLastDigit(int);

int main() {
   int number;
   int sum;
   scanf("%d", &number);
   sum = sumOfDigits(number);
   printf("%d", sum);
   return 0;
}

int sumOfDigits(int num) {
   int curDigit;
   int result = 0;
   if (num != 0) {
      return num%10+sumOfDigits(num/10);
   } else {
      return 0;
   }
}

int getLastDigit(int num) {
   int result;
   result = num%10;
   return result;
}

int popLastDigit(int num) {
   int result;
   int clippedDigit;
   if (num>9) {
      clippedDigit = getLastDigit(num);
      result = (num - clippedDigit) / 10;
      return result;
   }  else {
      return 0;
   }
}
It looks like you won't need those other supporting functions, unless you want to practice.

Also you can do things like "return num%10;" instead of having to explicitly declare a result variable, if that helps.
So here am I again, wondering how I am supposed to build a C project with this: https://marketplace.visualstudio.com/items?itemName=danielpinto8zz6.c-cpp-project-generator I already know how to use gcc and or clang with single files, but I want to be able to build multiple file projects into the usual executable file. When I use this extension, it reloads VS Code and nothing happened. How would you use this extension correctly, and / or what other extensions could you use to build C projects in VS Code Mac (NOT C++ PROJECTS, ONLY C PROJECTS!!!).

Edit: that extension actually created c projects, it didn't build them.
Instead of using an extention, just copy the template folder (located in ~/CEdev/examples or opt/CEdev/examples) and paste it wherever you like. Then, open a new terminal window, cd to the template folder and type "make". The compiled .8xp file is in the bin folder.
i know how to make a c project for the ti 84 pce; right now i am trying to make one for my computer.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement