Home > Blockchain >  Printing a line changes the return value of my function
Printing a line changes the return value of my function

Time:08-17

I've just begun learning C today and have some experience with other languages (Ruby). I'm understanding most of the basics, but I can't for the life of me figure out why this function (which returns the sum of the digits of an integer) only works correctly when I print the sum before returning the sum.

The code in question returns 32766 instead of 8:

#include <iostream>
#include <cmath>
using namespace std;
#include <string>

int sumOfDigits(int x) {
    int i = 1;
    int inc = 1;
    while(i < x){
        i *= 10;
        inc  = 1;
    }
    int size = inc;
    int digs[inc];
    inc = 0;
    int z = x;
    i /= 10;
    while(z > 0) {
        digs[inc] = (z/i);
        z -= i * digs[inc];
        i /= 10;
        inc  = 1;
    }
    int sum = 0;
    for (inc = 0; inc < size; inc  = 1) {
        sum  = digs[inc];
    }
    return sum;
}

int main(){
    cout << sumOfDigits(125); // prints 32766
    return 0;
}

but this piece of code that prints "hello world" before the return statement outputs 8 as expected.

#include <iostream>
#include <cmath>
using namespace std;
#include <string>


int sumOfDigits(int x) {
    int i = 1;
    int inc = 1;
    while(i < x){
        i *= 10;
        inc  = 1;
    }
    int size = inc;
    int digs[inc];
    inc = 0;
    int z = x;
    i /= 10;
    while(z > 0) {
        digs[inc] = (z/i);
        z -= i * digs[inc];
        i /= 10;
        inc  = 1;
    }
    int sum = 0;
    for (inc = 0; inc < size; inc  = 1) {
        sum  = digs[inc];
    }
    cout << "hello world";
    return sum;
}

int main(){
    cout << sumOfDigits(125);
    return 0;
}

CodePudding user response:

You read digs in places you did not write. You can easily verify this by printing the accesses:

while(z > 0) {
    std::cout <<"Writing digs[" <<inc <<"] = "<<(z/i) <<"\n";
    //...
}
//...
for (inc = 0; inc < size; inc  = 1) {
    std::cout <<"Reading digs[" <<inc <<"] => " <<digs[inc] <<"\n";
    //...
}

Result:

Writing digs[0] = 1
Writing digs[1] = 2
Writing digs[2] = 5
Reading digs[0] => 1
Reading digs[1] => 2
Reading digs[2] => 5
Reading digs[3] => 32125

Note that last read is reading an element that was never initialized, and thus can contain anything. Formally, reading uninitialized memory is undefined behavior.

In practice, this will result in reading garbage that depends on what happens to be there.

CodePudding user response:

#include<iostream>

using namespace std;

void sum_of_digits(int);

int main(){

  int num;

  cin>>num;

   sum_of_digits(num);
}

void sum_of_digits(int x){
   static int num=x;

   int sum=0;

   while(x>0){
      int last_digit=x;

      sum =last_digit;

      x/=10;
   }

   cout<<"sum of digits of "<<num<<" is "<<sum<<endl;
}

in your program u have made multiple assigments,re-assignment, i think problem lies there. purpose of first while loop is also not clear..

CodePudding user response:

I tried with online complier, I got following output for your codeOutput Screenshot

You can wind up the answer by using one while loop.

#include <iostream>
using namespace std;

int sumOfDigits(int x) {
    int sum = 0;
    while(x > 0) {
        sum  = x;
        x /= 10;
    }
    return sum;
}

int main(){
    cout << sumOfDigits(125);
    return 0;
}
  •  Tags:  
  • c
  • Related