loop problem!!

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 :(
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?
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 ,


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
Idon't understand any thing
If that's the case you should be talking with your professor for sure.
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++

}
the first one it pause for long time and thin show some big number in negative !!
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
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
the program told me that i did not initialzed d !! but i put d=1!!why?
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".
:( 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 !!
. . . .

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
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 !!
#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
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.