3 Replies - 509 Views - Last Post: 19 February 2013 - 04:35 AM Rate Topic: -----

#1 narabo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-February 13

Print out a Circle using a Matrix

Posted 19 February 2013 - 04:15 AM

We have to print out a circle using matrices and I'm having trouble sending the matrix to the circle function. There may be other problems though also...

Thanks!


//This program will accept a radius of a circle and print out said circle using a matrix

#include <iostream>
using namespace std;


//this function fill in a matrix a circle

void circle (char matrix[ ][20], int length, int radius)

{
    int middle=(length/2);
    
    for(int y=0 ; y<length ; y++)
        
    {
        int asquared=(middle-y)*(middle-y);
        
        for(int x=0 ; x<length ; x++)
            
        {
            int bsquared= (middle-x)*(middle-x);
            int radiussquared= radius* radius;
            
            if((bsquared+asquared)<(radiussquared))
                matrix[x][y]='*';
            
            else
                
                matrix[x][y]='-';
            
            cout<<matrix[x][y]<<" ";
            
        }
        
        cout<<endl;
        
    }
    
}


int main()
{
    int radius=8;
    int length=20;
    
    
    char matrix[length][radius];
    circle (matrix, length, radius);
    
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: Print out a Circle using a Matrix

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Print out a Circle using a Matrix

Posted 19 February 2013 - 04:25 AM

Well, you are not sending any value of your matrix to your Circle function.

You need to provide some scanf statements in a for loop for collecting the values that you want to provide in the matrix.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 narabo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-February 13

Re: Print out a Circle using a Matrix

Posted 19 February 2013 - 04:28 AM

sorry, im pretty new at this and didnt really understand what you said...
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Print out a Circle using a Matrix

Posted 19 February 2013 - 04:35 AM

There are several things wrong. In C++ the size of an array must be a compile time constant so the following is illegal.

void circle (char matrix[ ][20], int length, int radius);

int main()
{
    int radius=8;
    int length=20;


    char matrix[length][radius];

If you change the radius and length to const int variables then that part should be okay. However you seem to have your matrix sizes different in main than what you have in your function signature. This is where defining these two constant variables as global is considered acceptable
const int radius=8;
const int length=20;

void circle (char matrix[length][radius], int length, int radius);

int main()
{
    char matrix[length][radius];



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1