I have an algorithm, which should show if two arrays are similar or not. It works but I don't know what size should be of the array.
For example:
int a[10], i = 0, r = 0, n = 0;
printf("Enter the amount of numbers in arrays: ";
scanf("%d", &n);
printf("Enter the numbers of array: ";
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
If I enter for "n" variable n = 11, program stops at the end. My question is: What number should I put to the array a[THAT_PLACE] to be sure that this program will be compatible with most hardware ( I heard that this is also depending from memory. )
@UPDATE1:
I chose an alk's solution. But it still doesn't work. Here is my code:
int main()
{
int i = 0, temp_a = 0, switch_a = 0, temp_b = 0, switch_b = 0, n = 0;
printf("Enter the amount of numbers in arrays: ");
scanf("%d", &n);
{
int a[n], b[n];
printf("Enter elements of first array: ");
for (i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
printf("Enter elements of second array: ");
for (i = 0; i < n; ++i)
{
scanf("%d", &b[i]);
}
do
{
switch_a = 0;
for (i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
switch_a = switch_a + 1;
temp_a = a[i];
a[i] = a[i + 1];
a[i + 1] = temp_a;
}
}
} while (switch_a != 0);
//bubble sort
do
{
switch_b = 0;
for (i = 0; i < n - 1; i++)
{
if (b[i] > b[i + 1])
{
switch_b = switch_b + 1;
temp_b = b[i];
b[i] = b[i + 1];
b[i + 1] = temp_b;
}
}
} while (switch_b != 0);
//Cheks, if an arrays are the same.
for (i = 0; i < n; i++)
{
if (a[i] != b[i])
{
printf("This two arrays don't have the same elements.\n\n\n");
return 0;
}
}
printf("This two arrays have the same elements.\n\n\n");
}
return 0;
}
Could you check it for me? I can't find what is wrong...