Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C Programming Loop Help Please!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hnparmar




PostPosted: Sat Mar 09, 2013 10:45 am   Post subject: C Programming Loop Help Please!

3. Print out the current values in a table for voltages ranging from 0 to 50 in steps of 10 and resistor values ranging from 100 to 1000 in steps of 200 ohms. The table should look similar to the one in question 2.

Remember, V = I * R
c:

#include<stdio.h>
main()
{
  int i,j,total;
  for(i=0;i<=50;i=i+10)
  {
    for(j=100;j<=1000;j=j+200)
    {
      total = i/j;
      printf("%4i",total);
    }
  }
  getch();
}


When i run the program i get 0000000000.

Please help me.

Mod Edit:

Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code:

[syntax="c"] ... code ... [/syntax]

[code] ... code ... [/code ]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 09, 2013 11:30 am   Post subject: RE:C Programming Loop Help Please!

Integers don't much like division. Maybe try a different kind of variable.
nullptr




PostPosted: Sat Mar 09, 2013 11:42 am   Post subject: Re: C Programming Loop Help Please!

The reason you're getting a bunch of zeroes is because there's no newline at the end. Your printf call should look like this: printf("%4i\n",total);

The reason that total is always zero is because j is always greater than i. In C, when you divide two integers, the result is always rounded closer to zero so that it is also an integer. Try printing out the results of 3/4 or 13/2. Any remainder is cut off. To get around this, you'll have to cast one of the integers to a double, which can hold fractional values. Of course, for this to work you'll also have to change the type of total into a double. And remember that %i only works for integers, not doubles, so you'll have to take a look at printf too.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: