Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




Help w/ a simple "for" program

 
Reply to this topicStart new topic

Help w/ a simple "for" program

isuckatprograming
12 Mar, 2007 - 06:16 PM
Post #1

New D.I.C Head
*

Joined: 12 Mar, 2007
Posts: 4


My Contributions
CODE
#include <stdio.h>
int main()       // entering the main
{
float height, base;
float size, cross, moment, section;

printf("Lumber Size\tCross-section\tMoment of\tSection/n");     //listing
printf("Base*Height\tArea         \tInertia  \tModulus/n");     //titles

for (base = 2; base <= 12; base + 2)
{
for (height = 2; height <= 12; height + 2)
{
cross = base * height;                                  // calculating
moment = (base * height * height * height) /12;
section = (base * height * height)/6;
}
printf("%f*%f\t%f\t%f\t%f\n", base, height, cross, moment, section);
}
}


is what i got so far, and for some reason, im getting a repeating loop only printing out the vaules of it with 2

the objective is just to print a table,
2x2
2x4
2x6
up to 12, and then
4x2
excetra, and follow out those calculations

im pretty sure my format is right??? any help would be great
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help W/ A Simple "for" Program
12 Mar, 2007 - 07:27 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,984



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You are trying to increment like this base + 2, when it should be like this base += 2.

Same problem with the height increment, should be height += 2.
User is online!Profile CardPM
+Quote Post

ajwsurfer
RE: Help W/ A Simple "for" Program
12 Mar, 2007 - 09:50 PM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Here is a version of your program that compiles and runs correctly.
Changes where:

Line 4 and 5 changed float to int.
If you don't want to use integers you should format the numbers, in printf using, %8.0f or something like that.

Line 7 and 8 changed "/n" to "\n"
Line 10 and 12 changed "base + 2" to "base +=2"
Line 18 changed "%f*%f\t%f\t%f\t%f\n" to "%d*%d\t\t%d\t\t%d\t\t%d\n"
Now it's time to get rid of the logic errors.
You have it done, almost wink2.gif

CODE

#include <stdio.h>
int main()       // entering the main
{
  int height, base;
  int size, cross, moment, section;

  printf("Lumber Size\tCross-section\tMoment of\tSection\n");     //listing
  printf("Base*Height\tArea         \tInertia  \tModulus\n");     //titles

  for (base = 2; base <= 12; base += 2)
  {
    for (height = 2; height <= 12; height += 2)
    {
      cross = base * height;                                  // calculating
      moment = (base * height * height * height) / 12;
      section = (base * height * height)/6;
    }
    printf("%d*%d\t\t%d\t\t%d\t\t%d\n", base, height, cross, moment, section);
  }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month