Pointer function:
Feb 6, 2014 at 5:03pm UTC
I am trying to make a information to pointer function but the problem is how/what number am i putting in for *a...
and can i get that information back to main? and is that right way to split the array in to ? because i have will 200 random number and I need to find the smallest and second smallest element in array.
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
two_smallest (print, size3,size3,size3,size3);
}
void two_smallest (int find_array[] , int size3, int *a, int *a2, int *b,int *b2)
{
for (int index = 0; index <= 100; index ++)
{
if (find_array[index] < find_array [0])
{
find_array[0] = find_array [index];
*a = find_array[0];
}
}
cout <<"The first lowest number in this call is " << *a << endl;
for (int index_2=101 ; index_2 < 200; index_2 ++)
{
if (find_array [index_2] < find_array [0])
{
find_array [0] = find_array [index_2];
*b = find_array[0];
}
}
cout <<"The second lowest number is " << *a << endl;
}
size3= 200
Last edited on Feb 6, 2014 at 5:05pm UTC
Feb 6, 2014 at 6:40pm UTC
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 37 38 39
include <algorithm> // std::swap
#include <iterator> // std::end, std::begin
#include <iostream>
void display_two_smallest(const int * arr, unsigned size)
{
using std::swap;
if (size < 2)
return ;
int smallest = arr[0];
int next_smallest = arr[1];
if (next_smallest < smallest)
swap(smallest, next_smallest);
for (unsigned i = 2; i < size; ++i)
{
if (arr[i] < smallest)
{
next_smallest = smallest;
smallest = arr[i];
}
else if (arr[i] < next_smallest)
next_smallest = arr[i];
}
std::cout << "smallest: " << smallest << '\n' ;
std::cout << "next smallest: " << next_smallest << '\n' ;
}
int main()
{
int data [] = { 10, 9, -1, 3, 19, 0, -2 };
const unsigned data_size = std::end(data) - std::begin(data);
display_two_smallest(data, data_size);
}
Topic archived. No new replies allowed.