Hello thorpedo,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
With your code properly indented it would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
int main()
{
int i, arr[10], x[5], y[5];
cout << "Enter 10 Numbers:";
for (i = 0; i < 10; ++i) {
cin >> arr[i];
}
cout << "\n 1st" << endl;
for (i = 0; i < 5; i++) {
x[5] = arr[i];
cout << x[5];
}
cout << "\n 2nd" << endl;
for (i = 5; i < 10; i++) {
y[5] = arr[i];
cout << y[5];
}
return 0;
}
|
With a few blank lines it would make the code easier to read.
You are using two different styles for your {}s. It is better to pick one style,
https://en.wikipedia.org/wiki/Indentation_style , and stick with it. Mixing styles is a bit confusing and interrupts the flow when reading. It is a great benefit to you and others to make the code as easy to read as you can. Adding comments also helps.
For a program that is small like this on using a single letter for your variable names may be something that you can keep track of, but when your program becomes 200 or 300 lines of code it is very easy to loose track of the single letter variable and what it is for. Variables should have a proper name, a noun or sometimes a verb that describes what it is or what it does, so that there is no guess work of what to use. The following code will show an example of this.
Your first for loop works, but could be done a little better.
For the second two for loops and as
lastchance
has pointed out are a problem.
It took me a few times of looking at those loops to figure out what you are doing.
For the code
x[5] = arr[i];
not only is "x[5]" beyond the end of the array that you defined you are putting the five elements of "arr" into one single element of "x". Is there a point to doing this?
The same is true for the last for loop.
I have worked with your code and came up with this:
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
|
#include <iostream>
#include <limits>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
constexpr size_t L_MAXSIZE{ 10 }, S_MAXSIZE{ 5 };
int arr[L_MAXSIZE]{ 25, 56, 99, 42, 66, 73, 13, 9, 85, 5 };
int arr1[S_MAXSIZE]{}, arr2[S_MAXSIZE]{}; // <--- Added.
std::cout << "Enter 10 Numbers:\n\n"; // <--- Changed.
//for (int i = 0; i < L_MAXSIZE; ++i) // <--- Changed.
//{
// std::cout << " Enter number " << i + 1 << ": "; // <--- Added.
// cin >> arr[i];
//}
std::cout << "\n 1st\n ";
for (int i = 0, index = 0; i < S_MAXSIZE; i++, index++) // <--- Changed.
{
arr1[index] = arr[i]; // <--- Why is "arr[i]" being stored into the same element each time? "x[5]"
std::cout << arr1[index] << ' ';
}
//cout << "\n 2nd" << endl;
//for (int i = 5; i < L_MAXSIZE; i++) // <--- Changed.
//{
// arr2[5] = arr[i]; // <--- Why is "arr[i]" being stored into the same element each time?
// std::cout << arr2[5];
//}
// A fair C++ replacement for "system("pause")" or a good way to put a pause in the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
The first line of "main" defines two constants. You will find this makes the program much easier to use. Not only now, but more so in the future. This way if something changes you have only one place to make the change and it will be seen in the whole program.
By defining "arr" and initializing it with ten numbers you can comment out the for loop used for input and concentrate on the other two for loops. For the next line the {}s initialize the entire array to (0)s zeros.
The second for loop is one possible idea of what you can do.
I left the third loop for you to practice with.
The comments in the program should help or explain what I did. Any questions let me know.
Hope that helps,
Andy