| | |
Newbie need help on HW
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 3
Reputation:
Solved Threads: 0
My assignment is to write a program that produces a diamond when the user inputs an integer, if the integer is even it should be increased to the next odd number.
if the number is 7, it should print:
___*
__***
_*****
*******
_*****
__***
___*
(Without the underscores.)
So far I am able to produce the following output with the code I have written:
Enter number: 7
3
*
**
***
Press any key to continue
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int n;
cout<<"Enter number: ";
cin>>n;
cout<<n/2<<endl<<endl;
for(int i=0; i<=n/2; i++){
cout<<" ";
for(int st=1; st<=i; st++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
This is an assignment on nested loops, so we're supposed to use 4 for loops. I suspect that there's going to be 2 pairs of nested for loops. Can anyone help me out?
if the number is 7, it should print:
___*
__***
_*****
*******
_*****
__***
___*
(Without the underscores.)
So far I am able to produce the following output with the code I have written:
Enter number: 7
3
*
**
***
Press any key to continue
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int n;
cout<<"Enter number: ";
cin>>n;
cout<<n/2<<endl<<endl;
for(int i=0; i<=n/2; i++){
cout<<" ";
for(int st=1; st<=i; st++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
This is an assignment on nested loops, so we're supposed to use 4 for loops. I suspect that there's going to be 2 pairs of nested for loops. Can anyone help me out?
Greetings.
Sorry, I didn't have time to really sit down and think.
Can't get 4 for loops, but I've got a while nested with 3 for loops.
Take it as a reference if you like.
I'll spend some more time to come out with one with 4-For loops. :cheesy:
Hope this helps.
Sorry, I didn't have time to really sit down and think.
Can't get 4 for loops, but I've got a while nested with 3 for loops.
Take it as a reference if you like.
I'll spend some more time to come out with one with 4-For loops. :cheesy:
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int num, odd, increment, space; bool go; num = 0; increment = 1; odd = 1; go = false; do{ cout<<"Enter number: "; cin>>num; }while(num<3); if(num%2==0) num = num + 1; while(odd<=num) { space = num/2 - odd + increment; if(go==false) increment += 1; else increment -= 1; for(int x=0; x<space; x++) cout<<" "; for(x=0; x<odd; x++) cout<<"*"; for(x=0; x<space; x++) cout<<" "; cout<<endl; if(go==false) odd += 2; else odd -= 2; if(odd>num && go==false) { odd = num - 2; increment = num/2; go = true; } if(odd<0) odd = num + 1; } return 0; }
•
•
Join Date: Jul 2004
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Eddie
red_evolve,you really helpful person.
actually my college is having assignment soon and the title is address book system,since i am very fresh in programming,hope can get help from u too...hope meet you soon
•
•
Join Date: Jul 2004
Posts: 7
Reputation:
Solved Threads: 0
hi mrlucky0, heres a similar prog.chk this also out.i feel this is quite simple than that of red_evolves.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,k,no,i,j,a,b,c;
cout<<"\nenter n:";
cin>>n;
if(n%2==0)
n++;
for(i=1;i<=n;i+=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
for(i=n-2;i>=1;i-=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,k,no,i,j,a,b,c;
cout<<"\nenter n:";
cin>>n;
if(n%2==0)
n++;
for(i=1;i<=n;i+=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
for(i=n-2;i>=1;i-=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
getch();
}
•
•
Join Date: Jul 2004
Posts: 3
Reputation:
Solved Threads: 0
Yes, I was attempting to write a program similar to that. Looks like you found it. Eventually I was able to figure it out. Here's my rendition, which is not too different. But I swear, I did it on my own!
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int num, p=1; cout<<"Enter number: "; cin>>num; if(num%2==0) num+=1; int sp=num/2; for(int i=0; i<=num/2; i++) { for(int j=0; j<sp; j++) { cout<<" "; } for(int k=0; k<p; k++) { cout<<"*"; } cout<<endl; p+=2; sp--; } sp=1; p=num-2; for(i=0; i<num/2; i++) { for(int j=0; j<sp; j++) { cout<<" "; } for(int k=0; k<p; k++) { cout<<"*"; } cout<<endl; p-=2; sp++; } return 0; }
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Best free C/C++ compiler for a newbie? (C++)
- help newbie alert needs help with login page (ASP.NET)
- newbie needs help, basic mfc stuff (C++)
- Hello, newbie here... (Geeks' Lounge)
- Book For Newbie (C++)
- Newbie - how do I start C++ programming? (C++)
- PHP newbie, project feasibility (PHP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: storing references to classes
- Next Thread: acceleration and brakes in a car game
Views: 3125 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






hope meet you soon 