Beginner problem help

This is a discussion on Beginner problem help within the C Programming forums, part of the General Programming Boards category; Hello everybody, pleased to be here. I'm relatively new to programming, just took up learning C a few weeks ago ...

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    East from heaven
    Posts
    3

    Red face Beginner problem help

    Hello everybody, pleased to be here. I'm relatively new to programming, just took up learning C a few weeks ago (and not that actively either).
    I've run into an assignment that my mentor said "could" be appropriate, but that there's a simpler solution (which she did not say of course -_-), but when I wrote the code in Microsoft Visual Studio C++, the debugger returned no errors, but I can't seem to understand why I get nothing as a result (actually, instead of a number result, I get "-858993460").

    The code SHOULD be doing the following: The user types the values of N and M, where N is the number of natural numbers (is it "natural" in English? All whole numbers above 0 that is :P) and M is the paramater. The program is supposed to find the biggest number from 0 to N whose summary of digits is equal to M.

    Here's the code I wrote:

    Code:
    #include <stdio.h>
    #include <math.h>
    main ()
    {
    	int n,m,i,j,k,p,l,sum,x,max;
    	printf("Type in the amount of numbers \n");
    	scanf("%d",&n);
    	printf("Type in the M paramater \n");
    	scanf("%d",&m);
    	for (i=0;i<n;i++);
    	{
    		k=1;
    		for (j=1;j<=k;j++);
    		{
    			if (i/pow(10,j)==0)
    				p=j;
    			else k++;
    		}
    		sum=0;
    		for (l=p;l>=1;l--)
    		{
    			x=(i%(int)pow(10,l))/(pow(10,l-1));
    			sum=sum+x;
    		}
    		if (sum==m)
    			max=i;
    	}
    	printf ("The greatest number whose sumary of digits is equal to M is %d \n",max);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you intend to put your if (sum==m) statement inside your loop? As it is, it only happens once, once the loop is done.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    East from heaven
    Posts
    3
    The if (sum==m) statement is inside the first loop on purpose, but not the second. How come it only happens once if it's inside the loop, shouldn't it happen every time the first "for (i=0;i<n;i++)" loop cycles?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yeah, I miscounted the braces. Sorry; that should be fine.

    Looking at your first bit:
    Code:
    i/pow(10,j)==0
    You seem to be wanting to use integer division there, but pow(10,j) is not an integer value and so you'll basically never actually equal 0. To add up the digits, you should just start at the right, use %10 to peel off the last digit and /10 to move everything over to the right until you get to 0.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    East from heaven
    Posts
    3
    I see... Good to know for the future :P
    I'll rewrite the code tomorrow, or in a day or two, and report back the result here, right now I have other stuff to study :P

    Thank you for your time tabstop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's problem with C
    By scheng924 in forum C Programming
    Replies: 1
    Last Post: 05-19-2009, 04:44 PM
  2. Beginner Problem
    By knoxmaddog in forum C++ Programming
    Replies: 13
    Last Post: 11-21-2005, 11:16 PM
  3. Beginner's problem.. please help
    By jstevanus in forum C++ Programming
    Replies: 10
    Last Post: 12-08-2004, 04:08 PM
  4. beginner problem
    By laasunde in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 08:24 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

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