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