Thread: C programming help Please!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    C programming help Please!

    Question 2

    5 7 9 11 13 15
    __|___________________________________
    |
    3 | 15 21 27 33 39 45
    4 | 20 28 36 44 52 60
    5 | 25 35 45 55 65 75
    6 | 30 42 54 66 78 90
    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

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

    Please help me.

    Last edited by hnparmar; 03-09-2013 at 03:35 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Well, we can't see question 2.

    So, is the requirement to step through the entire range of resistance for each step in voltage - or to step through both at the same time, given that each is basically 5 steps? This will affect whether you need nested loops or just one loop.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    In the innermost loop you probably meant to have:

    Code:
    total = i/j;

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    Code:
    main(){float i,j,total; for(i=0;i<=50;i=i+10){for(j=100;j<=1000;j=j+200){      total = i/j; printf("%.2f,total); }}  getch();}

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    main()
    {
      float i,j,total; 
      for(i=0;i<=50;i=i+10)
      {
    	  printf("\n\n");
      for(j=100;j<=1000;j=j+200)
      {
          total = i/j; 
          printf("%.2f",total); 
       }
    }
      getch(); 
    }
    is this right format?

    the output i get is this

    0.000.000.000.000.00


    0.100.030.020.010.01


    0.200.070.040.030.02


    0.300.100.060.040.03


    0.400.130.080.060.04


    0.500.170.100.070.06

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, you need to give your numbers a total width of field:

    Code:
    printf("%6.2f ",total);
    Now the numbers won't be running together one column to the next. Adjust the six as needed, but keep the space after the f.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Quote Originally Posted by Adak View Post
    No, you need to give your numbers a total width of field:

    Code:
    printf("%6.2f ",total);
    Now the numbers won't be running together one column to the next. Adjust the six as needed, but keep the space after the f.

    Thank you so much Mr.Adak. Now the programming is running. Thank You.

    Code:
    #include<stdio.h>
    main()
    {
      float i,j,total; 
      float a=0,b=10,c=20,d=30,e=40,f=50;
      printf("%12.2f ",a);
      printf("%6.2f ",b);
      printf("%6.2f ",c);
      printf("%6.2f ",d);
      printf("%6.2f  ",e);
      printf("%6.2f ",f);
      for(i=100;i<=1000;i=i+200)
      {
                  printf("\n\n");
                  printf("%6.2f" , i);
                  for(j=0;j<=50;j=j+10)
          {
          total = j/i; 
          printf("%6.2f ",total); 
          }
      }
      getch(); 
    }

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i,j;
    clrscr();
    for(i=5;i<16;i+=2)
    {
    for(j=3;j<7;j++)
    {
    printf("%d",i*j);
    }
    printf("\n");
    }
    getch();
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jorgedobson View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i,j;
    clrscr();
    for(i=5;i<16;i+=2)
    {
    for(j=3;j<7;j++)
    {
    printf("%d",i*j);
    }
    printf("\n");
    }
    getch();
    }
    Welcome to the forum, Jorge! Thanks for using code tags on your first post!!

    May I make two important recommendations for your coding?

    1) It's never just main(), it's always int main() with a return (normal run time evidenced by a zero), so return 0, at the end of your programs.

    and

    2) Indenting your code - where subservient lines of code are indented 2 to 5 spaces - is important. It makes the logic "pop" out as people read it/study it.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Quote Originally Posted by Adak View Post
    Welcome to the forum, Jorge! Thanks for using code tags on your first post!!

    May I make two important recommendations for your coding?

    1) It's never just main(), it's always int main() with a return (normal run time evidenced by a zero), so return 0, at the end of your programs.

    and

    2) Indenting your code - where subservient lines of code are indented 2 to 5 spaces - is important. It makes the logic "pop" out as people read it/study it.
    Dear return type doesn't meter we just follow output...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM