1 Replies - 658 Views - Last Post: 17 October 2007 - 12:08 PM Rate Topic: -----

#1 sugarflaps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-October 07

creating a aseries of triangles

Post icon  Posted 17 October 2007 - 08:06 AM

hi imtrying to create a series of triangles which looks like this
*
**
***
****
*****
*
**
***
**
*
*
**
***
****
***
**
*
*
**
***
****
*****
****
***
**
*
i have having real trouble getting the triangle shape i know its someting to do with my loops but im completly stuck here is my code so far if anyone can please help or suggest where i am going wrong. cheers very much everyone xx
//Lyndsey Scott
//Tutorial 4
//Simple c++ program using functions
//16th October 2007

#include <iostream> // using the main library
using namespace std;

// Function declarations
char get_char_wanted();
int get_num_rows();
int get_num_colms();
void draw_line(char wanted_char, int rows, int colms);

int main()
{
  char wanted_char = get_char_wanted();
  int num_rows = get_num_rows();
  int num_colms = get_num_colms();

  draw_line (wanted_char, num_rows, num_colms);
}

// Function definitions.
// Function to get character
char get_char_wanted()
{
  char wanted_char;
  cout << "input character wanted" << endl;
  cin >> wanted_char;
  return wanted_char;
}

//Function to specify number of rows.
int get_num_rows()
{
  int num_rows;
  cout << "enter number of rows" << endl;
  cin >> num_rows;
  return num_rows;
}

// Function to specify number of coloumns.
int get_num_colms()
{
  int num_colms;
  cout << "please enter the number of coloumn" << endl;
  cin >> num_colms;
  return num_colms;
}

// Function to draw line of characters.
void draw_line(char wanted_char, int rows, int colms)
{
  for ( int i = 1; i <= rows; i ++ )
  {
	cout << wanted_char;
  }

  for ( int j = 1; j <= colms; j ++ )
  {
	cout << wanted_char;
  }
  cout << endl  << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: creating a aseries of triangles

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: creating a aseries of triangles

Posted 17 October 2007 - 12:08 PM

As with your previous assignment, if I remember correctly, you aren't using nested loops. You need to nest the two loops so that a character is outputted for each of num_colms, in each of the num_rows. Take a look at the solution to your previous assignment and go from there.

Once you've got that sorted, there is a current thread here dealing with how to print out triangles of characters instead of rectangles.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1