loop problem!!

Nov 3, 2009 at 7:41am
hello everyone
I am anew in c++ programming and I missed my class last week it was about loop ,,I have ahomework I just found out and it due after 3 hours can you help to solve it plz?? Idon't understand any thing

these are the three Q ,,,,I will be greatfull to any one who help ,

1. Using for loop, write a program that reads an integer to generate all its divisors.
Example-1: if you read 11 the out put should be 1 11
Example-2: if you read 8 the out put should be 1 2 4 8
2. Using while loop, Write a program to get a triangle shape with a length entered by
the user. For example, if the user entered a length of 10 then the shape is:
*
**
***
****
*****
******
*******
********
*********
**********
3. Using do while loop, write a program that will calculate the following series:
Sum = 1 + x + x2 + x3 + x4 - - - - + xn
Notice that x and n values should be entered by the user.




please help :(
Nov 3, 2009 at 7:50am
It's due in three hours and you didn't even attempt any of the exercises? If you missed a class, why don't you ask your professor for help?
Nov 3, 2009 at 8:18am
his office hours conflictwith my other lecture ,,I tried to solve it but I didn't know how , I am very sorry for bothering you but it marked assig. and I really hate to ask people to do my work that not me and the other reason for ASKING A HELP THAT I just Knew about this homework it was attached to the class note ,


Nov 3, 2009 at 8:21am
Can you post what you have so far?
A good way is to write pseudo-code, like
1
2
while number is bigger than something
do something


It outlines what your program will do and helps with a lot of your thinking.
Last edited on Nov 3, 2009 at 8:23am
Nov 3, 2009 at 8:31am
Idon't understand any thing
If that's the case you should be talking with your professor for sure.
Nov 3, 2009 at 8:45am
look I know the for know I just review it and i tried the first one ,, but after i compile and debug it it it didn't work

this is my work so far ,


for Q1


#include<iostream>

using namespace std;
void main(){
int d,n;
for(n=0;n>=0;n++);
if(n%d==0,d>0)
cout<<"the diviisor of "<<n<<"="<<d;
cout<<d;
}






and for the other Q2


#include<iostream>



using namespace std;
void main()
{
int n,f;

cout<<"enter a number";
cin>>n;

f=0;
i=1;
while(i<n)
f=f+1
cout<<"*";


i++

}
Nov 3, 2009 at 8:47am
the first one it pause for long time and thin show some big number in negative !!
Nov 3, 2009 at 8:55am
Q1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

using namespace std;
void main(){ //void main() - NO! Main() must return an int and therefore only int main() is standard
int d,n;
//put the line where you cout << "The divisors of " << (user_input) << " = "; //end here
for(n=0;n>=0;n++); //infinite loop that's... useless? N = 0, if it's bigger than or EQUAL TO 0 (wtf?)
                                 //change it so that instead of a ; use { } so it becomes a code block
if(n%d==0,d>0) //I assume D is user input (get it before your for loop
cout<<"the diviisor of "<<n<<"="<<d; //finish the previous statement here
                                                              //cout n and print a comma and a space
cout<<d; //unneeded line, remove
//end code block here
}


I have outlined some key mistakes

Post in [co.de][/co.de] tags next time please, not quotes.

Q2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
void main() //int main()
{
int n,f;

cout<<"enter a number";
cin>>n;

f=0;
i=1; //what's i? int? double? char? string?
while(i<n) //should be <= if i is declared as 1, think about it
//you forgot code blocks again.. { ... }
f=f+1
cout<<"*"; // i don't understand this bit. It just prints a long line of asterisks
//think about it... print a newline after you have enough asterisks on a line

i++
//end code block here 

Last edited on Nov 3, 2009 at 8:57am
Nov 3, 2009 at 9:57am
thank you fir your time ,,, for the first Q Iam really confused !! look what I did

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;
int main(){ 
int d,n;
cout<<"enter n";
cin>>n;
if(n%d==0)
cout<<"the diviisor of "<<n<<"="<<d;

for(d=1;d>=10;d++)

                                                            

}
Last edited on Nov 3, 2009 at 9:59am
Nov 3, 2009 at 10:01am
the program told me that i did not initialzed d !! but i put d=1!!why?
Nov 3, 2009 at 10:03am
Well... why don't you look yourself?

d = 1 is 2 lines after where you use it.

And that won't work......

How would YOU do it if you were solving that problem (with your brain) in "human code". Translate the complete "human code" to "machine code".
Nov 3, 2009 at 10:07am
:( it did'nt work either !!

you make me feel bad about myself ,,,
after i modified it it just give me the resultfor 5 only 5=1 !!
Nov 3, 2009 at 10:13am
. . . .

Okay, can you turn this pseudo-code into C++?
1
2
3
4
5
6
7
8
9
let user_input be user input
get user input
print "The factors of " user_input " are: "
for number i = 1, i <= user_input, i++
    see if user_input modulo i gives you remainder 0
    if it does
        print that out followed by comma and a space
    if it doesn't
       go on 


Enter a number: 8
The factor of 8 are: 1, 2, 4, 8, 


Can you see how it goes?
The method I used is very inefficient, but once you get the structure working, you can easily expand + edit the code.

. . . .

EDIT: Instead of me hopelessly trying to explain this, read this site's detailed tutorial. This should fill you on what you missed during the lesson and will help you with your homework.
http://www.cplusplus.com/doc/tutorial/control/
Happy Homeworking . . .
Last edited on Nov 3, 2009 at 10:23am
Nov 3, 2009 at 10:32am
thank iwill go know i have a midtearm exam I will not submit it toady but i will back to this topic on sat.
wait for me ^_^ I will ask your help to get this !!
Nov 4, 2009 at 3:29pm
#include<iostream>
using namespace std;

int main()
{
int size;

cout<<"Enter a size: ";
cin>>size;

for(int i=0; i<size; i++)
{
for(int j=0; j<i; j++)
{
cout<<"*";
}
cout<<endl;
}

return 0;
}

I hope this will help you, for your second question
Nov 8, 2009 at 6:03pm
thank you very much ,, I found the the answer with help and i get the last one by myself
Topic archived. No new replies allowed.