How can I bring the output that is required for this for loop assignment

Mar 1, 2012 at 6:39pm
;;;;
Last edited on Apr 30, 2012 at 8:18pm
Mar 1, 2012 at 6:50pm
You're confusing me. Are you trying to get a fibonacci sequence, or are you trying to sum the total of iterative quantities?
Mar 1, 2012 at 6:53pm
the output is suppose to be

1+2+3+4+5=15
Last edited on Mar 1, 2012 at 7:05pm
Mar 1, 2012 at 6:59pm
Well, I can see you having a need for three ints, say i (iteration), sum and count. You would want to initialize sum at 0.
Read in an int from the user to count.
Make a for loop that starts i at 1, continue until i = count, and increment i.
Inside the loop, you want to display i, then add i to sum.
After the loop terminates, display sum.
Mar 1, 2012 at 7:01pm
Mar 1, 2012 at 7:12pm
closed account (3wqkoG1T)
Hi,
Try this code:
(I have made some changes to your code.I've used an array to save the loop iteration values.


#include<iomanip>
#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
long int num;

cout << "Give a positive number: ";
cin >> num;
const int size=num;
int array[size];
while (num<0)
{
cout << "Invalid NUM Please give a positive num: ";
cin >> num;
}

{
int n, sum=0;

for(n=1;n<num;n++)
{
sum+=n;
array[n]=n;
}
cout << "You have ran the loop " << num << " times"<< "\n";
for(int i=1;i<size;i++)
cout<<array[i]<<"+";
cout<<size;
cout<<" = "<<sum+size<<endl;
}

}
Mar 1, 2012 at 7:14pm
Why are you giving away answers to homework problems instead of encouraging them to learn on their own?
Mar 1, 2012 at 7:18pm
the answer is false anyways.. it is overcomplicated for no reason (I am quite sure he didn't yet learn of arrays so he can't put that in the assignment); you don't need arrays anyways to output (1+2+3+4+5=15) and your last 'for' loop would only output (1+6=7; 2+6 = 8; 3+6 =9) which isn't what he asked for.
Mar 1, 2012 at 7:19pm
That's cruel. I approve.
Mar 1, 2012 at 7:29pm
don't take it wrong Isena, i wasn't trying to be cruel..
on the other hand good job for johhny for creating 2 identical threads ;)
Mar 1, 2012 at 7:31pm
Oh, I didn't think that you were meaning to be cruel, timmyyyyy, I'm just saying that I sincerely doubt that the OP will be back to look at this forum again until potentially after he gets a low 'C' on his grade for trying a copy/paste from someone else's code without really verifying it.
Mar 1, 2012 at 7:36pm
closed account (3wqkoG1T)
I agree with you as far as far as giving away answers(my mistake.I'll be more careful in giving help the next time).
But, please , check again the code... The last loop does not output (1+6=7; 2+6 = 8; 3+6 =9),but what he asked for...
Mar 1, 2012 at 7:37pm
oh i see what you did there..
yeah that would be quite evil but who in the world wouldn't even run his program before sending it in ;)
edit : ah yes Isena my mistake, i thought you had a { before the 'for'
Last edited on Mar 1, 2012 at 7:40pm
Mar 1, 2012 at 7:40pm
Trust me. I've seen the way some people operate...
Topic archived. No new replies allowed.