5 Replies - 4199 Views - Last Post: 16 April 2012 - 04:46 PM Rate Topic: -----

#1 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:17 PM

I'm trying to write a program which sums the squares of the first 333 positive odd integers.
Here's what i have, but its not working. Please help
int main() 
{
    //declare variables
    int square=2;
    int sum;
    int total=0;
    int count=0;
    
 
    
    while (total <= 333)
    {
        if (square % 2 == 1)
        {
            count = count + 1;
            total = square * square;
            count ++;
            square ++;
        }
        else
        {
            cout<<"Use only odd integers! ";
        }
    }
    
    sum = total + (square * square);
    

    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Finding the sum of squared odd integers till 333

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:24 PM

What's wrong? Does it output incorrectly?
You set total to only square * square every time. Is this what you want? Wouldn't you be adding that value to total instead?
total += (square * square)

Was This Post Helpful? 0
  • +
  • -

#3 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:38 PM

Wouldn't you want your while loop to check count, not total? If I read the problem correctly, you want to add the squares of the first 333 positive odd integers. i.e. 1^2 + 3^2 .... You could have a temp variable that is always incremented each loop. In this way, if an odd is found, increment count, and sum = (temp*temp)

int main() 
{
    //declare variables;
    int sum = 0; // keeps track of our total
    int temp = 0; // this is our incremental variable
    int count = 0; // this will keep track of how many odd numbers we've found
    
    while (count < 333) //while we haven't found 333 odd numbers
    {
        temp++; 
        if (temp % 2 == 1) //if num is odd
        {
            count++; //inc. total odd nums found
            sum += (temp*temp); //add the square of curr number to sum
        }
    }
    return 0;
}



This post has been edited by raspinudo: 16 April 2012 - 02:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:26 PM

View Postraspinudo, on 16 April 2012 - 02:38 PM, said:

Wouldn't you want your while loop to check count, not total? If I read the problem correctly, you want to add the squares of the first 333 positive odd integers. i.e. 1^2 + 3^2 .... You could have a temp variable that is always incremented each loop. In this way, if an odd is found, increment count, and sum = (temp*temp)

int main() 
{
    //declare variables;
    int sum = 0; // keeps track of our total
    int temp = 0; // this is our incremental variable
    int count = 0; // this will keep track of how many odd numbers we've found
    
    while (count < 333) //while we haven't found 333 odd numbers
    {
        temp++; 
        if (temp % 2 == 1) //if num is odd
        {
            count++; //inc. total odd nums found
            sum += (temp*temp); //add the square of curr number to sum
        }
    }
    return 0;
}




Thank you for your help. I think that's the basic structure of the program except i had to use a cout << sum; at the end, but no big deal. The problem is worded funny and i think my teacher is looking for 333 odd integers squared and then sum up all of them which is how this program works. I'm very new to this and i've never seen some of the stuff you used for example, sum += (temp * temp). I do have one more question though...is there a way to check if it's only summing the squares of odd integers?
I thought about setting up my temp=1 and then in my while statement using temp+2;
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:34 PM

I'm imagining that that could work: checking the value that % returns on it should be enough.
Was This Post Helpful? 0
  • +
  • -

#6 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:46 PM

View PostDimitriV, on 16 April 2012 - 04:34 PM, said:

I'm imagining that that could work: checking the value that % returns on it should be enough.

Well i tried it that way, but doesn't produce the same number. I think this way is much better and works because the if statement makes sure that the number has to be odd and then adds to it and continues to add until 333 odd integers have been squared and added together.
#include <iostream>
#include <string>

using namespace std;

/*
 * 
 */
int main() 
{
    //declare variables
    int sum = 0;
    int temp = 0;
    int count = 0;
    
    while (count <= 333)
    {
        temp++;
        if (temp % 2 ==1)
        {
            count++;
            sum += (temp * temp);
        }
        
    }
    
    cout << "The sum is: "<< sum<<endl;
    

    return 0;
}


This post has been edited by jimblumberg: 17 April 2012 - 05:44 AM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1