Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 21st, 2005, 3:19 PM   #1
ViZioN
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 ViZioN is on a distinguished road
Returning An Array From a Function

Hey all,

I'm currently writing a calculator program for Uni which takes in 2 Roman Numeral expressions and displays the answer Roman Numerals. The answer is then displayed in Roman Numerals

i.e IX+XV = XXIV

The problem i'm having is trying to return an array from a function. The function takes in the integer answer worked out earlier in the program, and then converts this to roman numerals, saving each character in an array, which is created by the function. I wish to return the array back into the main program but i'm having problems


Sorry if this has been asked before, I tried searching the forums but couldn't find anything that similar .
The code for my sub-routine is as follows:

char convert(int answer)
{
int j=0;
char numeral[20];

	// Calculate M's
   while((answer-1000)>=0)
   {
	answer=answer-1000;
	numeral[j++]='M';
   }

   if(answer>=900)
   {
   numeral[j++]='C';
   numeral[j++]='M';
   answer=answer-900;
   }

   // Calculate D's

	while((answer-500)>=0)
	{
   answer=answer-500;
	numeral[j]='D';
   j++;
   }

   if(answer>=400)
   	{
   	numeral[j++]='C';
   	numeral[j++]='D';
   	answer=answer-400;
   	}

   // Calculate C's

	while((answer-100)>=0)
   {
	answer=answer-100;
	numeral[j++]='C';
   }

   if(answer>=90)
   {
   numeral[j++]='X';
   numeral[j++]='C';
   answer=answer-90;
   }

   //Calcalute L's

while((answer-50)>=0)
   	{
	answer=answer-50;
	numeral[j++]='L';
   }

   if(answer>=40)
   {
   numeral[j++]='X';
   numeral[j++]='L';
   answer=answer-40;
   }

   // Calculate X's

   while((answer-10)>=0)
	{
	answer=answer-10;
	numeral[j++]='X';
   }

   if (answer==9)
   {
   numeral[j++]='I';
   numeral[j++]='X';
   answer=answer-9;
   }

   // Calculate V's

   while((answer-5)>=0)
	{
	answer=answer-5;
	numeral[j++]='V';
   }

   if(answer==4)
   {
   numeral[j++]='I';
   numeral[j++]='V';
   answer=answer-4;
   }

   // Calculate j's

while((answer-1)>=0)
   {
	answer=answer-1;
	numeral[j++]='I';
   }

   numeral[j]  = '\0';
   printf("\n");
   return numeral;
}

I want to return the array numeral so that it can be displayed on screen and the code can be used to convert other numbers.

These are the errors that the compiler is displaying:

Warn : roman.c(207,19)uspicious pointer conversion
Error: roman.c(207,19):Nonportable pointer conversion

Can someone help me out so that i can get my program working properly?

Thanks in advance,

Dave
ViZioN is offline   Reply With Quote
Old Feb 21st, 2005, 3:37 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
char *convert(int answer)
{
int j=0;
char numeral[20];

	// Calculate M's
   while((answer-1000)>=0)
   {
	answer=answer-1000;
	numeral[j++]='M';
   }

   if(answer>=900)
   {
   numeral[j++]='C';
   numeral[j++]='M';
   answer=answer-900;
   }

   // Calculate D's

	while((answer-500)>=0)
	{
   answer=answer-500;
	numeral[j]='D';
   j++;
   }

   if(answer>=400)
   	{
   	numeral[j++]='C';
   	numeral[j++]='D';
   	answer=answer-400;
   	}

   // Calculate C's

	while((answer-100)>=0)
   {
	answer=answer-100;
	numeral[j++]='C';
   }

   if(answer>=90)
   {
   numeral[j++]='X';
   numeral[j++]='C';
   answer=answer-90;
   }

   //Calcalute L's

while((answer-50)>=0)
   	{
	answer=answer-50;
	numeral[j++]='L';
   }

   if(answer>=40)
   {
   numeral[j++]='X';
   numeral[j++]='L';
   answer=answer-40;
   }

   // Calculate X's

   while((answer-10)>=0)
	{
	answer=answer-10;
	numeral[j++]='X';
   }

   if (answer==9)
   {
   numeral[j++]='I';
   numeral[j++]='X';
   answer=answer-9;
   }

   // Calculate V's

   while((answer-5)>=0)
	{
	answer=answer-5;
	numeral[j++]='V';
   }

   if(answer==4)
   {
   numeral[j++]='I';
   numeral[j++]='V';
   answer=answer-4;
   }

   // Calculate j's

while((answer-1)>=0)
   {
	answer=answer-1;
	numeral[j++]='I';
   }

   numeral[j]  = '\0';
   printf("\n");
   return numeral;
}

Example:

char retFromFunc[20];
strcpy(retFromFunc, convert(3));
__________________

tempest is offline   Reply With Quote
Old Feb 21st, 2005, 3:53 PM   #3
ViZioN
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 ViZioN is on a distinguished road
I'm not completly sure where to add in youe example code

The code that calls the function at the moment is,
answer2=convert(answer);
  	printf("\n");
   printf("%s", answer2);

and my function is declared at the top of my program as

char convert(int)

I'm new to C and pointers (only been learning C 4 weeks). Can you explain how to alter the other lines of my code so that the program works correctlly.

Thanks in advance,

Dave
ViZioN is offline   Reply With Quote
Old Feb 21st, 2005, 5:17 PM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
you can't return an array in C you'll have to return a pointer to the array.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 21st, 2005, 6:01 PM   #5
ViZioN
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 ViZioN is on a distinguished road
Yeah Dizzutch i've been told that already today but i'm not too sure how to do that

I've only been learning C for 4 weeks

Are you able to help me out?
ViZioN is offline   Reply With Quote
Old Feb 21st, 2005, 6:45 PM   #6
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4 Xero is on a distinguished road
Edit: Actually Im not sure if this applies for C. Works for C++.

Umm Arrays automatically pass by reference (theres another term used for arrays, cant remember). Theres no need for a pointer.
__________________
...

Last edited by Xero; Feb 21st, 2005 at 6:48 PM.
Xero is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC