943,634 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 502
  • C RSS
Feb 18th, 2009
0

something recursive...

Expand Post »
uhm, i really need help on recursion problems, and the first problem before my assignment is me because i really dont know how to do it, or how to construct it successfully... this is my assignment:

a program that will read 10 integers,display them as inputted and in reverse order,
display the minimum and the maximum imput using the following functions:

void readA(int A[])
-a function that reads an array of integers

int minimum(const int A[],int low, int high)
-a recursive function that returns the minimum element in the array A

int maximum(const int A[], int low, int high)
-a recursive function that returns the maximum element in the Array A.

void displayA(const int A[], int low, int high)
-a recursive function that displays the element of array A

void displayrev(const int A[],int low,int high)
-a recursive function that displays the elements of the array A in reverse order

*uhm, and this is my nonsense code that i've done, and even me didnt know what it is doing, but for me, at least i've tried..

  1. #include<stdio.h>
  2. #define inte 10
  3.  
  4. void readA(int A[]){
  5. int A[inte];
  6. printf("\nenter an integer : ");
  7. scanf("%d",A[inte]);
  8. }
  9. void displayA(const int A[],int low, int high){
  10. if(low<=high)
  11. {
  12. printf("%d",A[low]);
  13. displayA(A,low+1,high);
  14. }
  15. }
  16. void displayrev(const int A[], int low, int high){
  17. if (a>=0)
  18. printf("\nA[displayrev(a-1)]");
  19. }
  20. main(){
  21. int A[inte];
  22.  
  23. printf("Enter the numbers AGAIN : ");
  24. scanf("%d",A[inte]);
  25.  
  26. }

there are many things that i didn't have there, i just dont know how, that's why i need help, please help me...super thanks guys...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daniellamae is offline Offline
11 posts
since Sep 2008
Feb 18th, 2009
0

Re: something recursive...

Well, we are not supposed to do your homework, but here is some code you can use as starting point. So, the code for the recursive input:
  1. void readA(int A[]){
  2. if (nr>0) {
  3. printf("\nenter an integer : ");
  4. scanf("%d",&A[--nr]);
  5. readA(A);
  6. }
And also put this in the main function:
  1. int main(){
  2. int A[inte];
  3.  
  4. printf("How many numbers : ");
  5. scanf("%d",&nr);
  6. readA(A);
  7. }
  8. }
where nr is a variable of type int.

P.S. Sorry, I've just noticed that readA shouldn't even be recursive... Well I think its still a good example, and U can use it..
Last edited by Chaster; Feb 18th, 2009 at 10:38 am.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Feb 22nd, 2009
0

Re: something recursive...

Yes you have really messed up the code at some places

Well I wont give you the codes but will help you out in pointing your errors and what you need to add to your codes.Here it goes:
  1. void readA(int A[]){
  2. int A[inte];
  3. printf("\nenter an integer : ");
  4. scanf("%d",A[inte]);
  5. }
This code reads entire array so run a for loop to take in all the inputs you are just taking one input.And ya you are passing array A[] from main and taking it here so why are you re-declaring that array within this function ??? There is no need for int A[inte] within.

  1. void displayA(const int A[],int low, int high){
  2. if(low<=high)
  3. {
  4. printf("%d",A[low]);
  5. displayA(A,low+1,high);
  6. }
  7. }
This code is ok and will fulfill your requisite but don't use "const" at all instances because it may cause big problems if used without caution.

  1. void displayrev(const int A[], int low, int high){
  2. if (a>=0)
  3. printf("\nA[displayrev(a-1)]");
  4. }
This code is nothing. It doesn't even print anything.Your approach to displayA is correct so use the same here in a different way as:
  1. void displayrev(const int A[],int low, int high){
  2. if(low<=high)
  3. {
  4. printf("%d",A[high]);
  5. displayrev(A,low,high-1);
  6. }
  7. }

Now the main() .This is completely useless.
  1. main(){
  2. int A[inte];
  3.  
  4. printf("Enter the numbers AGAIN : ");
  5. scanf("%d",A[inte]);
  6.  
  7. }
After the declaration of array A[inte]
call the read array A function written just for that purpose. Once this is done you can move on with your other parts of coding.

And ya try your might on a bit difficult things like finding min and max through recursion also.I don't see any code pertaining to it here.Try...
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Another Question
Next Thread in C Forum Timeline: Pipe() and Execve() Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC