Author Topic: Need help with a program  (Read 622 times)

0 Members and 1 Guest are viewing this topic.

Offline excruciator

  • Veteran
  • *****
  • Posts: 1216
  • Asshole by Nature
Need help with a program
« on: May 28, 2008, 12:49:41 pm »
So I'm doing a programming assignment and I'm stuck on one question

Code: [Select]
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void largest_recursive(int ar[], int size);

int main () {

int ar[101]={0};  //the array

srand(time(0));   

for (int a=0; a<=100; a++)     //counter
{
ar[a]=rand()%25+1;
}


return 0;

So thats is the main, I need a function to find the largest number appeared in this array, In recursive, which I have no idea how to do.

HELP?
Always remember the succubus...

Offline mar77a

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1295
  • mad
    • random stuffs
Re: Need help with a program
« Reply #1 on: May 28, 2008, 01:00:53 pm »
your array is something like this:
a, a+1, a+2, a+3

you can compare like this, calling the same function from last to first:

(((a+3 > a+2) > a+1) > a)

that's what i thought or how i've done it in haskell. not sure how it translates to c++ and i refuse to do your homework.

Offline excruciator

  • Veteran
  • *****
  • Posts: 1216
  • Asshole by Nature
Re: Need help with a program
« Reply #2 on: May 28, 2008, 01:53:54 pm »
I know the format of recursion. But I just dont know exactly how THIS can be done.

Date Posted: May 28, 2008, 02:53:07 pm
i refuse to do your homework.

:/
I said too much didn't I..
Always remember the succubus...

Offline Amblin

  • Major(1)
  • Posts: 23
  • Uwe Boll, a master and friend
    • Homepage
Re: Need help with a program
« Reply #3 on: May 28, 2008, 02:13:34 pm »
Code: [Select]
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//void largest_recursive(int ar[], int size);
int largest_recursive(int ar[], int size) {
int tmp;
int ret;
if (size > 1) {
tmp = largest_recursive(ar, size-1);
}
if (ar[size] > ar[size-1]) {
ret = ar[size];
} else {
ret = ar[size-1];
}
if (tmp > ret) {
ret = tmp;
}
return ret;
}

int main () {

int ar[101]={0};  //the array

srand(time(0));   

for (int a=0; a<=100; a++)     //counter
{
ar[a]=rand()%25+1;
}


cout << largest_recursive(ar, 100);
system("pause");
return 0;
}
Works, though I just noticed that half of the comparisons are pointless. I won't comment on it, just figure it out yourself.