1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
/*
Use pointers to write a function that finds the smallest element in an array of integers.
Use [1,2,4,5,10,100,2,-22] to test the function.
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int *search(int *list, int size) /*If I don't put the * next to the "list", I won't be able to access the values
listed on the "main". That's what I think*/
{
int x = 0;
for (int i = 0; i < size ; i++) //just to find the smallest element from the array
{
if (x > list[i]) //If condition probably works here
{
x = list[i];
}
}
cout << "Smallest element is: " << x << endl;
return 0; /*not really sure what to put here, I keep getting error unless I put a return value so I guess
it's just zero... UGH!!! It works though.*/
}
int main()
{
int list[] = { 1,2,4,5,10,100,2,-22 };
search(list, 8);
system("pause");
return 0;
}
|