Hello parinaz mellatdoust,
First off you made changes to your original program. It would help if you post the new code so I can see what you have done and if you missed anything.
When I got the program to compile and run I was faced with a blank window and a flashing cursor having no idea what to enter. As a start here is a suggestion. You can change the cout statements to whatever you like:
1 2 3 4 5 6 7 8 9
|
std::cout << "\n How many numbers do you want to use? 1 to 100: ";
cin >> n;
std::cout << std::endl; // <--- Just adds a blank line to the output.
for (i = 0; i < n; i++)
{
std::cout << " Enter number " << i + 1 << " of " << n << ": ";
cin >> a[i];
}
| |
This same concept can work for the second input of "m" and the for loop that follows.
A thought for the future and now if you want. Above main define something like
1 2 3
|
constexpr int MAXSIZEA{ 100 };
constexpr int MAXSIZEB{ 100 };
constexpr int MAXSIZEX{ 200 };
| |
This is one rare time I suggest using a global variable only because its value can not be changed anywhere in the program. If "constexpr" should be a problem for you than you can just use "const". BTW the capital letters just help to let you know that the variable is defined as a const. Now you can use these variables anywhere in the program and only have to change the value in one place if you need to.
The last part of main is
cout<< *merge(*a,*b);
. First this will not print the entire array only the dereferenced value of that address returned by "merge". What you will need to do is define an int pointer in main and set the return value of "merge" to this pointer. Then you can use a for loop to print the array.
Now for the "merge" function:
The first thing that you do is define "int i". This is unnecessary because you can easily difine this in the for loop as
for (int i = 0; i < 200; i++)
. The only reason to define outside the for loop is to use this variable before the function ends and outside the for loop.
Defining the array "x" as "static" is necessary to retain the array after the function ends.
The if statements do not work as you have them. I am thinking they should be if/else if statements, but even this may not work. Not sure as I have not tried this yet.
It may be better to sort arrays "a" and "b" separately then add each individual array to "x". The next problem here is that you have no idea how much of "a" and "b" have been used. The variables "n" and "m" are in main and the function can not see these values. This is another rare case where a global variable would be useful. Otherwise you could easily exceed the upper boundary of the arrays. A good example of the afore mentioned "MAXSIZEA" and "MAXSIZEB" being useful.
Given the output that you want it would still be better to sort each array first then merge the two arrays together with your if/else if statements. As it is you are assuming that arrays "a" and "b" are in sorted order. This may not always be the case and you should as a programmer account for this.
This should give you something to think about and work with while I work more on the program.
Hope that helps,
Andy
Edit: