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 18-Nov-2008, 00:17
mimi mimi is offline
New Member
 
Join Date: Nov 2008
Posts: 1
mimi is on a distinguished road

Reverse of 2 arrays


Hi I am having trouble getting the exact code for this problem." Write the definition of a function, isReverse , whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.) "

This is what I have right now:

CPP / C++ / C Code:
bool isReverse(int a[],int b[],int c) 
{
    
   for(int i=0;i<=c;i++)
  {
   if (a[i]==b[c-i])
   {
     return 1;
   }
   else 
   {
     return 0;
   }    
  }
   
   
}



Can someone tell me what I am doing wrong???
Last edited by LuciWiz : 18-Nov-2008 at 02:14. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 18-Nov-2008, 06:04
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: reverse of 2 arrays


You are only examining the first element of the for loop and then exiting.
It is more likely you would want to return false on failure within the loop otherwise return true as all things matched. For example
CPP / C++ / C Code:
bool isReverse(int a[],int b[],int c) 
{
   for(int i=0;i<=c;i++)
      if (a[i] != b[c-i])
         return false;
   return true;
}
As a side note, you were returning integer values from a boolean function. Boolean values may or may not equate to integer values but it is usually better practice to return the value declared by the function.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 18-Nov-2008, 06:56
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: reverse of 2 arrays


If c is the size of the arrays, it should be

CPP / C++ / C Code:
bool isReverse(int a[],int b[],int c) 
{
   for(int i=0;i<c;i++)
      if (a[i] != b[c-i-1])
         return false;
   return true;
}
__________________
www.blake-foster.com
  #4  
Old 18-Nov-2008, 12:28
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Reverse of 2 arrays


Yes. My apologies. Indexing should always end at size - 1 if the array is zero based.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

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
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Reverse DNS entries admin Web Hosting Forum 1 21-Apr-2006 11:39
Reverse functions and Reverse checking functions jenmaz C Programming Language 4 24-Nov-2004 09:48
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26

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

All times are GMT -6. The time now is 23:48.


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