problems with divison

This is a discussion on problems with divison within the C Programming forums, part of the General Programming Boards category; Hi pals I am new in C programm but I have some problems with division in my simple code I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    problems with divison

    Hi pals I am new in C programm but I have some problems with division in my simple code
    I have problems when I define h=xmax/nstep. I dont know why please help me
    thx
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    # define max 60
    int main ()
    {
    	float t[max],k,h,tmax;
    	int j,alpha,xmax,nstep,m;
    	printf("xmax\n");scanf("%d",&xmax);
    	printf("nstep\n");scanf("%d",&nstep);
    	tmax=0.5;
    	alpha=1;
    	/*xmax=1;*/
    	/*nstep=10;*/
    	h=xmax/nstep;
    	/*k=0.5*h*h/alpha;*/
    	k=0.5*h*h/alpha;
    	m=50;
    	for (j=0;j<=m;j++)
    	    { t[j]=j*k;    
        printf ("%d\t%1.3f\n",j,t[j]);
    	    }
    	    exit(0);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since xmax and nstep are integers, you are going to get an integer result. If nstep is greater than xmax, it is going to be zero, but for example nstep being 5 and xmax being 7 will give you 1 as a result, not 1.4.

    The solutions available include (there are others, but that'll be more convoluted):
    1. Cast xmax or nstep to float, e.g.
    Code:
       h = (float)xmax/nstep;
    2. Make xmax and/or nstep a floating point value rather than an integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Thanks Mats it works. I really appreciate your help. I am learning C because my supervisor asked me to learn it
    Regards

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by vikingcarioca View Post
    I am learning C because my supervisor asked me to learn it
    Regards
    and believe me you'll love learning C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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