Array help
Apr 28, 2017 at 3:43pm Apr 28, 2017 at 3:43pm UTC
I'm working on an array program, it's doing what I need it to so far but, I'm having trouble getting it to do two things. I'm not sure how to get it to output Your array is: (numbers entered by user) and how to get it to output the minimum and maximum numbers. This is being done solely using functions/modules per instructor direction. The following is what I have so far. Any help would be greatly appreciated.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <string>
using namespace std;
void userEntryArray(int numArray[10]);
void printArray(int [10]);
bool checkUserEntry(int entry);
int main()
{
//Declaration Block
int numArray[10];
int userEntry;
userEntryArray(numArray);
printArray(numArray);
system("pause" );
return 0;
}
void printArray(int numArray[10])
{
for (int i = 0; i < 10; i++)
{
cout << numArray[i] << " " ;
}
cout << endl;
}
void userEntryArray(int numArray[10])
{
int userEntry = 0;
for (int i = 0; i < 10; i++)
{
cout << "Enter a number: " ;
cin >> userEntry;
numArray[i] = userEntry;
}
cout << endl;
}
bool checkUserEntry(int entry)
{
if (entry > -100 && entry < 1000)
{
return true ;
}
else
{
return false ;
}
}
Apr 28, 2017 at 6:02pm Apr 28, 2017 at 6:02pm UTC
I'm not sure how to get it to output Your array is: (numbers entered by user)
Your printArray function does that. The only thing it is missing is:
26 27
// After line 26
cout << "Your array is: " ;
and how to get it to output the minimum and maximum numbers.
I would suggest you write two more functions:
find_lowest_number()
find_largest_number()
BTW, you never call checkUserEntry()
Apr 28, 2017 at 10:25pm Apr 28, 2017 at 10:25pm UTC
I'm not sure on writing the function for finding the largest and smallest, any suggestions? Also not sure where to call checkUserEntry
Apr 28, 2017 at 11:10pm Apr 28, 2017 at 11:10pm UTC
I did get it printing out, thank you on that by the way.
Apr 29, 2017 at 6:25am Apr 29, 2017 at 6:25am UTC
Bump
Apr 29, 2017 at 11:28am Apr 29, 2017 at 11:28am UTC
changing the return type of checkUserEntry() to void:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
#include <algorithm>
constexpr auto SIZE = 4;
const int lowNum = -100;
const int hiNum = 1000;
void userEntryArray(int numArray[SIZE]);
void printArray(int [SIZE]);
void checkUserEntry(int & entry);//pass by reference
int main()
{
//Declaration Block
int numArray[SIZE];
//int userEntry;
userEntryArray(numArray);
printArray(numArray);
//system("pause");
//for (auto i = 0; i < SIZE; ++i)std::cout << numArray[i] << " "; std::cout << "\n";
auto mn = std::minmax_element(numArray, numArray + SIZE);
//http://en.cppreference.com/w/cpp/algorithm/minmax_element
std::cout << "Minimum array element: " << *(mn.first) << "\n" ;
std::cout << "Maximum array element: " << *(mn.second) << "\n" ;
return 0;
}
void printArray(int numArray[SIZE])
{
for (int i = 0; i < SIZE; i++)
{
std::cout << numArray[i] << " " ;
}
std::cout << "\n" ;
}
void userEntryArray(int numArray[SIZE])
{
for (int i = 0; i < SIZE; i++)
{
std::cout << "Enter array element# " << i + 1 << ": \n" ;
checkUserEntry(numArray[i]);
}
}
void checkUserEntry(int & entry)
{
bool match{false };
while (!match)
{
std::cin >> entry;
if (entry < lowNum || entry > hiNum)
{
std::cout << "Incorrect entry, try again \n" ;
}
else
{
match = true ;
}
}
}
Last edited on Apr 29, 2017 at 11:29am Apr 29, 2017 at 11:29am UTC
Topic archived. No new replies allowed.