-3

Okay, so here's the deal. I have been working on this problem for a few days now and have tried every resource available to me. Just this morning I sent my last resort email to my Professor, but I want to keep trying to work on it while I wait for his answer. I am supposed to be returning a pointer to the smallest value in an array of random numbers between 0 - 1410 after assigning them through pointer arithmetic(got this part to work just fine). There was a skeleton file included for the assignment, but I like to build things from the ground up(using his parameters of course). Here is my code:

#include <iostream>
#include <cstdlib>
using namespace std;

int* find_smallest_number(int*, int);

int main()

{
    const int size = 10;
    int random_array[size] = {};

    int* smallestNumber = find_smallest_number(random_array, size);

    srand(time(NULL));

    for(int i = 0; i < size; i++)
    {
        *(random_array + i) = rand()%1410;
        cout << random_array[i] << endl;
        cout << &*(random_array + i) << endl;
    }

    cout << "The smallest number is " << *smallestNumber << endl;

    return 0;
}


int* find_smallest_number(int *random_array, int size)

{

/*    
    for(int i = 0; i < size; i++)

    {
       if(*(random_array + i) > *(random_array + 1))
       {
           return *(random_array + i);
       }
       else if(*(random_array + i) < *(random_array + 1))
       {
           return *(random_array+1);
       }

    } */

    return &min_element;
}

I honestly can not tell you how many different ways I've tried. This is just the most recent attempt(all have ended in massive amts of [headdesk]). Any tips or hints you guys can come up with for me to try?? I have tried creating another variable to hold the value/address, I have tried as many different ways of comparison as I currently know(this min_element thing was something I found on the internet). I can occasionally get my code to compile, but when I do it will only return the value/address of the FIRST element in the array(no matter what my code looks like). And if I try to return a pointer it gets angry and tells me "invalid conversion from int to int*" Please help me? (Also, sorry if the format of my code is off a little bit, I tried to follow the instructions for it but may have failed in that too :/)

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
ImmaD0rk
  • 39
  • 1
  • 5
  • So, what problems, exactly are you having? Show erroneous output or error messages. – Marcin Mar 11 '12 at 16:09
  • You seem to be calling `find_smallest_number` **before** you've filled the array with values. That's not going to end well. Also, your code won't compile (what is `min_element`?). – Oliver Charlesworth Mar 11 '12 at 16:10

3 Answers3

2

Well, first off, why are you calling find smallest number before you populate the array? It should be:

srand(time(NULL));

for(int i = 0; i < size; i++)
{
    *(random_array + i) = rand()%1410;
    cout << random_array[i] << endl;
    cout << &*(random_array + i) << endl;
}

//move the call after you populate the array
int* smallestNumber = find_smallest_number(random_array, size);

Second, the code in your function should be something like:

int* find_smallest_number(int *random_array, int size)
{
    int* minPtr = random_array;
    for(int i = 1; i < size; i++)
    {
        if ( *(random_array + i) < *minPtr )
           minPtr = random_array + i;
    }
    return minPtr;
}

This way, you return a pointer to the exact element in the array. Your variant:

 return &min_element;

suggests you were returning the address of a local variable, which is undefined behavior.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Don't reinvent the wheel, use std::min_element as:

#include <algorithm> //must include this

int* minValue = std::min_element(array, array+size);

Also, make sure you fill the array first, then use std::min_element as shown above.

It seems you need really really good C++ introductory book. Get an introductory book from this list.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

Looks like you're trying to find the smallest number in the array before you fill it with random values. You need to initialize the array first, then find the smallest value.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • -___- this is why I should have done this way earlier(i love this website). Moar headdesk for me, let me try to switch it up. – ImmaD0rk Mar 11 '12 at 16:10
  • Grrr, nope! I am still getting that _really annoying_ error "invalid conversion from int to int*" – ImmaD0rk Mar 11 '12 at 16:13