Function Problem

This is a discussion on Function Problem within the C Programming forums, part of the General Programming Boards category; Something wrong with this code. Sum result is wrong please help me guys Code: #include<stdio.h> #include<conio.h> void lol(long z) { ...

  1. #1
    Registered User Batbyamba's Avatar
    Join Date
    Apr 2012
    Location
    Ulaanbaatar, Mongolia, Mongolia
    Posts
    1

    Talking Function Problem

    Something wrong with this code. Sum result is wrong please help me guys
    Code:
    #include<stdio.h>
    #include<conio.h>
    void lol(long z)
    {
    long r,k,sum=0;
    for(k=0;k<=10;k++)
    {
    sum=0;
    while(z>0)
    {
    r=z%10;
    z=z/10;
    sum=sum+r;
    }
    printf("Sum of a[%d]=%ld\t",k,sum);
    }
    
    
    }
    long main()
    {
    long a[10],r,i,j;
    for(i=0;i<=10;i++)
    {
    a[i]=rand()%100;
    printf("a[%d]=%ld\n",i,a[i]);
    }
    j=a[i];
    lol(j);
    getch();
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This code has errors on almost every line. What exactly are you attempting to do? And why are you using long instead of int?

    An incomplete list of errors:
    • main returns int, not long, and should have a return 0 at the end.
    • for loop in main should be i<10 not i<=10.
    • Before using rand() you should call srand((unsigned)time(0)) (once and only once).
    • Format for i in printf should be %ld.
    • j=a[i] after the loop is beyond end of array.
    • conio.h is unnecessary; use getchar() instead of getch().
    • etc.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I got this code to compile with no warnings anf function correctly only after changing virtually every line. My understanding is that he wants the sum of the digits in the number so, 95->14. Yah do everything ooga said, including changing all the data types to int. Compile with -Wall to see all warnings. Take a good look at the loop in lol (and consider changing lol's name) I think that you think you are looping through the array here, but really you are iterating and stripping digits off of z. The printf statement within the loop is not in the right place, and k cannot be used to access the array in the way you are thinking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 04:50 PM
  2. Replies: 2
    Last Post: 11-14-2011, 07:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 12:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21