LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2007, 08:45 AM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
C++ for command help


Hello,
I need to write a program for my C++ class that prints the following diamond shape using the for command and cout.
Code:
        *
       ***
      *****
     *******
    *********
     *******
      *****
       ***
        *
Code:
#include <iostream.h>
 void main(){
	int n;
	double i,j;
	n=5;
	for(i=0;i<=n-1;i++){
		for(j=0;j<=i;j++){
			cout<<"*";
		}//endfor
		cout<<endl;
	}//endfor
}//endmain
This prints:
Code:
*
**
***
****
*****
Thanks a million.
 
Old 02-12-2007, 11:28 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
So finish the program.

I hope you get a good grade on it!
 
Old 02-12-2007, 12:01 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I'd probably start by making a function which takes an integer argument and returns 1,3,5,7,9,7,5,3,1. Then maybe another function to build line number n (returning a string), and have the main program containing a simple loop which calls it.

Splitting up a program in to functions might even get you extra marks (assuming that's been discussed in you class).
 
Old 02-12-2007, 10:20 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Indeed you are getting there, you have:
Code:
*
**
***
****
*****
can you now get:
Code:
*****
****
***
**
*
Combine the two together and you might be able to get:
Code:
*
**
***
*****
****
***
**
*
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM
Command to display whole filestructure hierarchy f/ command line? mjewell Linux - Newbie 10 01-19-2004 10:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration