GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Nov-2005, 13:24
azncrazycooler azncrazycooler is offline
New Member
 
Join Date: Nov 2005
Location: Toronto
Posts: 3
azncrazycooler is on a distinguished road

making table of LCM using arrays


I am trying to make a table of LCM values using an array. I know that to find the LCM , you need the GCM. Im stuck in my program. Is it possible to call a function in a function? This is what I have so far. The table is set to 10 x 10. I currently have user input because it waits for the user to enter the row number and the column number for that specific LCM value. I.E 2x6, LCM is 6. Its currently got 4 errors, and im stuck and unsure hwo to fix them. Can someone help?

Code:
CPP / C++ / C Code:
 #include <iostream>
using namespace std;
int findLCM(double, double);
int gcd(int, int);


void main()
{
	const int rowsize = 10;
	const int colsize = 10;
	
	int rowcount, colcount;
	int data[rowsize][colsize];
	
	// make muilt table
	for (int r = 0; r < rowsize; ++r)
	{
		for (int c = 0; c < colsize; ++c)
		{
			data[r][c]= findLCM(r,c);
		}
	}
	
	cout << " Enter number of rows to display: ";
	cin >> rowcount;
	cout << " Enter number of columns to display: ";
	cin >> colcount;
	

	// output table
	for (r = 0; r < rowcount; ++r)
	{
		for (int c = 0; c < colcount; ++c)
		{ 
			cout << data[r][c] << " ";
		}
		cout << endl;
	}

  
	double findLCM(double num, double num2)
	{
		double lcm;
		lcm = (num*num2)/gcd;
		return c;

	}

	
	
	int gcd(int n1, int n2)
	{
		while( 1 )
		{
			n1 = n1 % n2;
			if( n1 == 0 )
			return n2;
			n2 = n2 % n1;
			if( n2 == 0 )
			return n1;
		}
	}




}
 
  #2  
Old 30-Nov-2005, 17:30
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: making table of LCM using arrays


Hi azn,

Welcome to the GIDForums™.

First, dont confuse with parameters and return values.
For example, you have declared the findLCM statement like this:
CPP / C++ / C Code:
int findLCM(double, double);
But you are never passing a double to it.
Rather, the return value is double!

So, change that to
CPP / C++ / C Code:
double findLCM(int, int);
int all places.

Also, you have to declare the data array as double, since you are returning a double value.

Next, the error which comes here is this:
'r' : undeclared identifier in line 31
So, whats the error?
Because the compiler doesnt know where the variable r has been declared.
You have to declare it as int r, because of the scope problems.

Then, you have to define your functions findLCM, and gcd outside the main function.

The next error is in this line:
CPP / C++ / C Code:
		lcm = (num*num2)/gcd;
Because you should not pass the variables num1 and num2 to the gcd function.
It should be like this:
CPP / C++ / C Code:
		lcm = (num*num2)/gcd(num1, num2);
Can you understand why?

Again, in this function:
CPP / C++ / C Code:

	double findLCM(int num, int num2)
	{
		double lcm;
		lcm = (num*num2)/gcd;
		return c;
	}
What is the other error? Can you find that?

Now that all the syntax errors are gone, here comes the logical error:
Division by zero!!!
That is because you are passing 0 and 0 as the values to gcd function in the loop:
CPP / C++ / C Code:
for (int r = 0; r < rowsize; ++r)
	{
		for (int c = 0; c < colsize; ++c)
		{
			data[r][c]= findLCM(r,c);
		}
	}
You have to pass the values from 1 to <=colsize or rowsize.

Last but not the least, you have to define the main function as int main(), rather that void main. And you should return 0 to indicate successful execution:
CPP / C++ / C Code:
int main()
{
    // code.....

    return 0;
}

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 30-Nov-2005, 21:05
azncrazycooler azncrazycooler is offline
New Member
 
Join Date: Nov 2005
Location: Toronto
Posts: 3
azncrazycooler is on a distinguished road

Re: making table of LCM using arrays


Hello Paramesh.


Thank You for taking the time to take a look at my program. I've managed with your help to fix up all the errors from it. But one thing that confuses me is , some things that you suggested didint work. Is it because I am using visual basics?

Here is a copy of my code that works for me at least .

[edit] ohhhhh i just noticed theres a visual basics c++ section!!

CPP / C++ / C Code:
 #include <iostream>
using namespace std;
double findLCM (int, int);
int gcd(int, int);



void main()
{
	const int rowsize = 10;
	const int colsize = 10;
	
	int rowcount, colcount;
	int data[rowsize][colsize];
	
	// make muilt table
	for (int r = 1; r < rowsize; ++r)
	{
		for (int c = 1; c < colsize; ++c)
		{
			data[r][c]= findLCM(r,c);
		}
	}
	
	cout << " Enter number of rows to display: ";
	cin >> rowcount;
	cout << " Enter number of columns to display: ";
	cin >> colcount;
	

	// output table
	for (r = 1; r < rowcount; ++r)
	{
		for (int c = 1; c < colcount; ++c)
		{ 
			cout << data[r][c] << " ";
		}
		cout << endl;
	}

}
	
	double findLCM(int num, int num2)
	{
		int lcm;
		lcm = (num*num2)/gcd(num, num2);
		return lcm;

	}

	
	
	int gcd(int n1, int n2)
	{
		while( 1 )
		{
			n1 = n1 % n2;
			if( n1 == 0 )
			return n2;
			n2 = n2 % n1;
			if( n2 == 0 )
			return n1;
		}
	}



  #4  
Old 30-Nov-2005, 22:36
azncrazycooler azncrazycooler is offline
New Member
 
Join Date: Nov 2005
Location: Toronto
Posts: 3
azncrazycooler is on a distinguished road

Re: making table of LCM using arrays


I still need some help. My program has logical errors. For example wjen i enter 2 abd 3, the LCM is 6, but it shows the number before it. Cany someon help ?
  #5  
Old 01-Dec-2005, 02:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: making table of LCM using arrays


Quote:
Originally Posted by azncrazycooler
I've managed with your [Paramesh's] help to fix up all the errors from it. But one thing that confuses me is , some things that you suggested didint work.
He pointed out many things. Can you be less vague?

Quote:
Originally Posted by azncrazycooler
Is it because I am using visual basics?
Doubtfully, since you are using (I assume) Visual C, not Visual Basic. VB is an entirely different language.

Quote:
Originally Posted by azncrazycooler
[edit] ohhhhh i just noticed theres a visual basics c++ section!!
No there isn't. We have a Visual C++ section. But that's for question related directly to extensions to C++ you'll find only in MS Visual C/C++ and MFC. Not for basic C++ programming questions like yours.


As for the code you just posted, it wouldn't compile. When I fixed the error it seemed to run perfectly for 10 by 10, although I didn't check all 100 answers. If there's a problem, you'll have to be more specific.

Show us the error, don't just tell us about it.

[edit]Oh, I see. When you enter 4 rows and 5 columns you get rows 1-3 and colums 1-4. Your description wasn't clear at all.

The answer has to do with loops. With that clue you should be able to find the problem.[/edit]
__________________

Age is unimportant -- except in cheese
  #6  
Old 01-Dec-2005, 03:52
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: making table of LCM using arrays


Quote:
Originally Posted by azncrazycooler
But one thing that confuses me is , some things that you suggested didint work. Is it because I am using visual basics?
I too use Visual C++, and it worked for me.

Here we go:

For more simplification, remove all doubles in your code and convert them to int.

Did you read this:
Quote:
Originally Posted by Paramesh
Next, the error which comes here is this:
'r' : undeclared identifier in line 31
So, whats the error?
Because the compiler doesnt know where the variable r has been declared.
You have to declare it as int r, because of the scope problems.
It comes in the line:
CPP / C++ / C Code:
	for (r = 1; r < rowcount; ++r)
	{
So, you have to change r=1 to int r=1.

Again, when printing:
CPP / C++ / C Code:
	for (int r = 1; r < rowcount; ++r)
	{
		for (int c = 1; c < colcount; ++c)
		{ 
			cout << data[r][c] << " ";
		}
		cout << endl;
	}
You have to vary r from 1 to rowcount(i.e r <=rowcount)
Similarly, vary c from 1 to colcount.

At last:
Quote:
Originally Posted by Paramesh
Last but not the least, you have to define the main function as int main(), rather that void main. And you should return 0 to indicate successful execution:
CPP / C++ / C Code:
int main()
{
    // code.....

    return 0;
}



Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a whole column in table lowercase... geedubya15 MySQL / PHP Forum 1 02-Jun-2005 11:50
Hash Table & Graph Kay Chan C++ Forum 7 08-Oct-2004 07:44
Help making a survey in C++ elavalos C++ Forum 2 23-Sep-2003 16:40
[Tutorial] MySQL Basics nniehoff MySQL / PHP Forum 15 23-Mar-2003 19:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.