Thread: Help!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    11

    Help!!

    I need 2 Write a program with a for or while loop that will calculate the sum of intergers between 1 & 500 that are evenly divisible by either 7 or 11. then calculate an print to the screen how many intergers there are between 1 & 500 that are evenly divisible by either 7 or 11 and calculate the average number from the sum divided by the count. format it with teo numbers after the decimal point. The average should come out to be 251.45. I need any help i can get thank you

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What do you have so far?

    Start with an empty main() and make sure it compiles. Then write the loop that just outputs all the integers between 1 and 500. Then change it to output only those divisible by 7. Then output those divisible by 7 or 11. Keep compiling and testing as you go. Then add code to keep a sum and a count of those integers. Then add the output of the average. Then you are done. If you have a specific problem or question, post it here with your current code in [code][/code] tags and you will get some help.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    First, include the header files you will need to do what you want. This will probably only be iostream.

    Then, declare the variables you will need in the program. You will probably need a variable for the total, the total number of integers divisible by 7 or 11, and nother for average.

    After that step, you are going to have to set up the int main(){ (the body of the code)

    Right away, you should set up your "for" loop.
    HINT: You will probably want to start at 0, want it to go to 500, and you will probably want to add 1 each time (1, 2, 3 to 500). Don't forget to put a { to show where the loop starts.

    Now, to see if a number is divisible by 11 you can do a math function with the % sign.
    If you do 5/2, you get 2.5. In C++ if you do 5%2, it will tell you that decimal place, which in this case is 5. So, lets say you had 9/3, you get 3. If you do 9%3, you get 0 as the decimal place (it was 3.0) so you know it is divisible:
    Make an if statement that checks to see if the number is divisible by 11, and in the if part, you can put "||" to represent OR, so pseudo: "if x%11==0 OR if x%7==0, then do this:"

    Now, you need to find what the total of numbers is, so you should have the variable declared for the total. Use that, and if the number (x) is divisible by 7 or 11 evenly, then add that number to the total.
    It should be something like adding x (from the for loop) to the total variable. Each time the for loop loops, it will add the x if it is divisible, and, at the end, it will have a total.

    Also, you are going to have to find the average, so you will need to know how many numbers there were (average is the total divided by the total number of things added), so you will need another variable to work as a counter. The 2nd variable you declared should be used for this.
    Still inside the if-statement, have that total integer variable add 1 to itself each time a number is divisible by 7 or 11.

    Then, include two }s to show the end of the if loop and the end of the for loop.

    Now, before you output anything, you can do the code to find the average.
    The average would be the total divided by the totalnums (a / is used for division if you didn't know)

    The rest of the code would be just displaying what you want:
    The first would be the total
    You can use the cout<< command and then type in the variable's name to show the total of all the integers added up.

    Then, output the total number of numbers:
    Once again, use the cout<< command and then put in your variable's name for total integers divisible by 7 or 11. Don't forget to put a semicolon at the end of all these lines.

    And finally, output the average. Use cout<< and then type in the average's variable name and put a semicolon.

    You will then need to close your int main() function with a } and a sort-necessary return 0; before that.


    And that's it!
    Last edited by JDrake; 11-18-2006 at 12:56 PM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    11

    this is what i got so far

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    
    void main()
    
    {
    
    int i,j,sum,count;
    sum=count=0;
    float averege;
    for (i=1;i<=500;i++)
    {
    if (i%7 == 0)
    sum=sum+i;
    count=count+1;
    }
    for (j=1;j<=500;i++)
    {
    if (j%11 == 0)
    sum=sum+j;
    count=count+1;
    }
    
    cout.precision(5);
    cout<<"The sum is "<<sum<<" and the averege is "<<averege<<endl;
    cout<<"The total count is "<<count<<endl;
    
    system("pause");
    
    return 0;

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1)
    Code:
    void main()
    Should be
    Code:
    int main (void)
    2)
    Code:
    sum=count=0;
    ? Maybe
    Code:
    sum = 0;
    count = 0;
    3)
    Code:
    sum=sum+i;
    Could be
    Code:
    sum += i;
    (same for j)
    4)
    Code:
    count=count+1;
    Could be
    Code:
    count++;
    (same for j)
    5) Average is never given a value before you cout it.
    6)
    Code:
    system("pause");
    Non-portable. See the FAQ.
    7) Main has no closing brace.
    8) You have no indentation.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> this is what i got so far
    Is it working? Do you have specific problems?

    What I see is that you have a separate loop for 7 and 11, but from my understanding of the original instructions, a single loop would be better. Otherwise you will count numbers that are divisible by both 7 and 11 twice (like 77). Also, you have a typo in your second loop that should go away when you combine them into one.

    You also need to put braces around your if blocks. If you are missing the braces after the if, only a single statement will be included in the if block. You want both the sum and the count to be changed only if the if is true.

    Finally, as manutd mentioned you need to set a value for average before outputting it.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You may also find that indenting your code can improve program readability and makes it far easier to debug
    Double Helix STL

Popular pages Recent additions subscribe to a feed