Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,230 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,854 people online right now. Registration is fast and FREE... Join Now!




find certain number in List

 
Reply to this topicStart new topic

find certain number in List

kkgaming
18 Mar, 2007 - 12:52 PM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
I have wrote another iterative Function for this huge assignment. This program takes 2 inputs, the user determines the list size, and gives a value. This program searches an array for how many numbers the user enters for the value. If a user enters 10 for the list size, and 5 for the value, it will find all of the 5's in the randomly generated list. We have to do this recursively as well. I am unsure how to randomize the recursive list. Here is iterative code.

CODE

#include <stdio.h>
#include <stdlib.h>

//List unsorted Iteration Function

void Itunsorted(int unsorted, int unsortedvalue)
{
  int i, cntr2 = 0;
  int *unsortedlist = calloc (unsorted, sizeof (int) );
  srand(time(0));
  for(i = unsorted; i >= 1; i--)
  {
  
  unsortedlist[i] = rand() % unsorted + 1;
  if (unsortedlist[i] == unsortedvalue)
  cntr2++;
  printf("%d\n", unsortedlist[i]);
  
   }
  printf("there is %d %d's ", cntr2, unsortedvalue);
}



int main()
{

//Print out iteration unsorted list
   int unsorted, unsortedvalue;
   printf("Give a Number: ");
   scanf("%d", &unsorted);
   printf("Value to search for: ");
   scanf("%d", &unsortedvalue);
   Itunsorted(unsorted, unsortedvalue);
return 0;
}


here is what I have for the recursive thanks to another member, but it it wrong for this. If anyone can help me write this program recursively, would help a lot.

CODE

#include <stdio.h>
#include <stdlib.h>

//List unsorted Recursion Function

  int Reunsorted(int y, int uservalue)
   {
  
   if (y > 0)
   {
    if (y == uservalue)
    {
        
        return  1 + Reunsorted(uservalue-1, y);
    } else
    {
        return Reunsorted(uservalue-1, y);
    }
} else {return 0;}
}




main()
{



//Print out recursion unsorted list
   int usera, userb;
   printf("Give a Number: ");
   scanf("%d", &usera);
   printf("Value to search for: ");
   scanf("%d", &userb);
   Reunsorted(usera, userb);
   ans3 = Reunsorted(usera, userb);
   printf("there is %d %d's ", ans3, userb);
  
        
  
  
return 0;
  }



This post has been edited by kkgaming: 18 Mar, 2007 - 02:18 PM
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Find Certain Number In List
18 Mar, 2007 - 01:12 PM
Post #2

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
How to I randomize the recursive list?

This post has been edited by kkgaming: 18 Mar, 2007 - 02:22 PM
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Find Certain Number In List
20 Mar, 2007 - 09:33 AM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
You must initialize the randomizer first. Then you can use the rand()
function to produce phudo random numers.

http://www.cprogramming.com/tutorial/random.html

Are you using an array or a list? There is a difference. recursion is done by using a function that calls itself.

The function must have 3 posible states
1. an initialized state
2. a standard operation state
3. an exit state

consider a sum function which will produce the sum of
(2 * 1) + (2 * 2) + (2 * 3) + ... + (2 * n)

to call it use:

int n = 100;
int theTotal = sum( n ); // n must be larger than 0

The first condition is met on the function call.


Now for the function description
int sum(int i) {
if ( i < 1) return i; // the third condition, or the exit state
return sum(i - 1) * 2; // the second condition, or the normal state
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month