Hello mpg,
I think this may be simple enough for what you know:
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 <iostream>
//using namespace std; // <--- Best not to use.
int main()
{
constexpr size_t MAXSIZE{ 5 }; // <--- or "const unsigned int MAXSIZE{ 5 };" either will work.
int v1[MAXSIZE]{ 1, 5, 7, 10, 8 };
int v2[MAXSIZE]{ 5, 8, 22, 1, 4 };
int dups[MAXSIZE]{};
// <--- Finds numbers that match.
for (size_t arr1 = 0; arr1 < MAXSIZE; arr1++)
{
for (size_t arr2 = 0; arr2 < MAXSIZE; arr2++)
if (v1[arr1] == v2[arr2])
{
//std::cout << v1[arr1] << " " << v2[arr2] << std::endl; // <--- Used for testing.
dups[arr1] = v1[arr1];
}
}
std::cout << "\n Mismatched numbers:" << std::endl;
for (int lc = 0; lc < MAXSIZE; lc++)
if (v1[lc] != v2[lc])
std::cout << ' ' << (v1[lc] < 10 ? " " : "") << v1[lc] << " " << (v2[lc] < 10 ? " " : "") << v2[lc] << std::endl;
std::cout << std::endl;
for (int lc = 0; lc < MAXSIZE; lc++)
{
if (dups[lc])
std::cout << ' ' << dups[lc] << " is common in both arrays." << std::endl;
}
return 0;
}
|
"constexpr" (from C++11 on) is about the same as "const". I am trying to use the C++11 standards and not the older versions. If "constexpr" does not work for you then your compiler would need to be adjusted to use the C++11 standards. I found this to be needed with the Dev C++ IDE. Not sure about CodeBlocks.
The "size_t" is usually a typedef for "unsigned int", but I have seen setups that give this a type def of "long" or "int_64", ( same as a long).
When defining a constant it is generally accepted to use capital letters for the variable name.
The {}s whether empty or containing a number is called an "uniform initializer" available from C++11 on. Very handy. The "=" are not needed.
You can see in defining the arrays and in the for loops how using "MAXSIZE" makes things much easier. This way you only have one place to make changes and it is used and changes everywhere in the program.
Defining variables as a constant is useful for things like "SALESTAX", some kind of "TAXRATE" or "DISCOUNTRATE" as examples.
I thought someone mentioned this earlier, but I must be thinking about something else.
The first nested for loops are used to check each element of "v1" with every element of "v2" to see if there is a matching number. I used the extra array to store these matches for later use.
The next for loop prints out the mismatched elements of both arrays. The if statement only prints elements of the array elements that do not match.
The last for loop prints out the numbers that match based on the contents of the "dups" array. The if statements works on the principal that what is between the () evaluates to either false (0) or true (1). Actually the (1) can be any number greater than zero.
This may be new to you
(v1[lc] < 10 ? " " : "")
. It uses the ternary operator "?" and ":". you can think of it as a shortened if/else statement equilivant to:
1 2 3 4
|
if (v1[cl] < 10)
std::cout << " "; // <--- prints a space.
else
std::cout << ""; // <--- prints nothing.
|
This may help:
http://www.cplusplus.com/articles/1AUq5Di1/ It is the first link I found when doing a search here on "ternary operator". Worth looking into.
The point is that it will help line up the numbers in the output.
Mismatched numbers:
1 5
5 8
7 22
10 1
8 4
1 is common in both arrays.
5 is common in both arrays.
8 is common in both arrays.
|
And when an element of each array have the same number I get this:
Mismatched numbers:
1 5
5 8
10 1
8 4
1 is common in both arrays.
5 is common in both arrays.
22 is common in both arrays.
8 is common in both arrays.
|
The third element of each array is 22.
At school I didn't study "std::srand(static_cast<unsigned>(std::time(nullptr))) " |
Not a problem.
"srand" as defined
void srand (unsigned int seed);
.
http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand
As you can see the value passed to "srand" needs to be an "unsigned int". The
static_cast<type>
. In this "type" can be replaced with "unsigned", "unsigned int" or "size_t". This is needed because "time()" does not return an "unsigned int" as "srand" needs.
When calling the "time" function what is passed could be zero or "NULL", but as I have recently learned "nullptr" is the best choice.
and also I don't use "std::" |
If you are going to use the line
using namespace std;
then you will need to learn everything that is in the standard namespace so you do not use a variable or function name that is in the standard name space because it will give you an error. Or you could learn to qualify things like
std::cin
std::cout
std::endl
std::string when including the header file "string"
std::vector when including the header file "vector"
std::setw() These when including the header file "iomanip"
std::fixed
std::setprecision
std::left
std::right
|
These, I would say are the most common, you are likely to use for now. The compiler and a good IDE can help you learn others. I know that you have paid a lot of money for someone to teach you the incorrect or easy way to write source code. In the end this is not helping you. And what happens when you finish all your schooling and your first employment says: "you will no use
using namespace std;
in any code you write. If you have not learned slowly over time you will have to learn it all at once.
If you do a search here there ate many posts to cover this topic that are worth finding and reading.
All the examples given earlier are good and worth copying and going back to in the future when you understand more.
Hope that helps,
Andy