something recursive...

Reply

Join Date: Sep 2008
Posts: 11
Reputation: daniellamae is an unknown quantity at this point 
Solved Threads: 0
daniellamae daniellamae is offline Offline
Newbie Poster

something recursive...

 
0
  #1
Feb 18th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: something recursive...

 
0
  #2
Feb 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: something recursive...

 
0
  #3
Feb 22nd, 2009
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...
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC