Hello everyone, I'm having trouble with a program that allows a user to enter the elements in an array and then outputs them in order from smallest to greatest.
I tried for about a week before I looked up the answer and I would be very grateful if someone could explain the parts I don't understand, and why what I was trying is incorrect.
Here is the correct program:
#include <iostream>
#include <string>
usingnamespace std;
#define space cout << endl;
int main()
{
//Create empty array
int myArr[10];
int smallest;
int pos;
int temp;
int length = sizeof(myArr) / sizeof(myArr[0]);
//Assigns user input to the integers in the array
cout << "Please enter the unsorted numbers one at a time: " << endl;
for (int x = 0; x < 10; ++x)
{
cout << "[" << x << "] = ";
cin >> myArr[x];
}
space
//Shows the list of integers in the array unsorted
cout << "Unsorted list = ";
for (int x = 0; x < 10; ++x)
{
cout << myArr[x] << ", ";
}
space
space
cout << "Sorting......" << endl;
space
cout << "Sorted list = ";
for (int y = 0; y < length - 1; ++y)
{
smallest = myArr[y];
for (int j = y; j < length; ++j)
{
if (myArr[j] < smallest)
{
smallest = myArr[j];
pos = j;
}
}
if (smallest != myArr[y])
{
temp = myArr[pos];
myArr[pos] = myArr[y];
myArr[y] = temp;
}
cout << myArr[y] << ", ";
}
cout << myArr[length - 1];
space
space
}
The part I'm not understanding is why the length variable is using sizeof. I know how sizeof works for finding how many bytes a type is on a system but have no idea how it's working here. And I'm not understanding all the variable reassignments in the last if statement. Also I'm not sure why the pos variable is necessary.
Here is the program I wrote and to me it seems like it should work but when it should output the sorted list it just outputs the same as the unsorted. So, could someone explain why this is incorrect.
#include <iostream>
#include <string>
usingnamespace std;
#define space cout << endl;
int main()
{
//Create empty array
int myArr[10];
int smallest = myArr[0];
//Assigns user input to the integers in the array
cout << "Please enter the unsorted numbers one at a time: " << endl;
for (int x = 0; x < 10; ++x)
{
cout << "[" << x << "] = ";
cin >> myArr[x];
}
space
//Shows the list of integers in the array unsorted
cout << "Unsorted list = ";
for (int x = 0; x < 10; ++x)
{
cout << myArr[x] << ", ";
}
space
space
cout << "Sorting......" << endl;
space
cout << "Sorted list = ";
for (int y = 1; y < 10; ++y)
{
if (myArr[y] < smallest)
{
smallest = myArr[y];
}
}
for (int i = 0; i < 10; ++i)
{
cout << myArr[i] << ", ";
}
space
space
}
Both programs are poorly written, but that's not what this is about. In specific, you want to understand two things, the first being this:
int length = sizeof(myArr) / sizeof(myArr[0]);
The size of the entire array is the number of bytes in memory is occupies (e.g. 40), whereas the size of an individual element in the array is how many bytes each element takes up (e.g. 4), so when you divide them (40/4) you get the number of elements in the array.
Generally speaking though, arrays like this are deprecated.
The second thing you want to know is why your implementation does not sort the numbers - could you tell me where you make any attempt to rearrange the array? I don't see any line other than line 19 that changes the values in the array.
The second thing you want to know is why your implementation does not sort the numbers - could you tell me where you make any attempt to rearrange the array? I don't see any line other than line 19 that changes the values in the array.
Now that you mention it, I just noticed I only changed the smallest variable and not the order of the array elements at all....I'm an idiot.
Well at least the second question is solved.
The size of the entire array is the number of bytes in memory is occupies (e.g. 40), whereas the size of an individual element in the array is how many bytes each element takes up (e.g. 4), so when you divide them (40/4) you get the number of elements in the array.
Here's something that may help: take three cups and put air in one, water in another, and milk (or some other liquid) in the last. Mark "temp" on the one with air, "A" on the one with water, and "B" on the one with milk.