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 :/)