0

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...

user7374044
  • 157
  • 1
  • 1
  • 10
  • You want a variable length array, which luckily C99 onwards has. Read in how many elements you need with the scanf line before declaring `int a[n];` – Colin Jan 04 '17 at 12:59
  • "... this program will be compatible with most hardware" - Shouldn't the program be written such that it is appropriate for the **problem** it is meant to solve? – too honest for this site Jan 04 '17 at 13:20
  • The updated code appears to work if you add the `#include `. What is the problem? Further, this sounds like a new question, unrelated to the old one. If you can identify a problem, maybe post a new question? – ad absurdum Jan 04 '17 at 15:14
  • It works! Thank You all soooo much! I'm sooo happy. :) Stackoverflow - the place for me :) – user7374044 Jan 04 '17 at 16:43

3 Answers3

2

Your array a can contain only 10 int's. So, if you input n as 11, then you have undefined behaviour due to out-of-bounds access.

You can use a C99 VLA (varaible length array) or use dynamic memory allocation using malloc() family functions.

Note: VLAs are optional in C11. So, you may want to check if your implementation doesn't support it by using the macro __STDC_NO_VLA__.

A potential problem with VLAs (which have automatic storage duration -- commonly known as "stack") is that it's hard to figure out if the allocation is successful for large arrays.

P.P
  • 117,907
  • 20
  • 175
  • 238
2

If you cannot (or you do not want to) use VLA, use malloc: man malloc

Example:

int *buffer;
int dim;

printf("Enter the amount of numbers in arrays: ");
scanf("%d", &dim);
buffer = (int*) malloc(sizeof(int) * dim);
if (buffer == NULL) {
   perror("Malloc error");
   exit(-1);
}

More on malloc here: Stack Overflow: How do malloc() and free() work?

Community
  • 1
  • 1
mpampana
  • 166
  • 6
1

If your C implementation in use supports Variable Length Arrays, then just do:

int i = 0, r = 0, n = 0;
printf("Enter the amount of numbers in arrays: ";
scanf("%d", &n);

{
  int a[n];
  printf("Enter the numbers of array: ";
  for (i = 0; i < n; i++)
  {
    scanf("%d", &a[i]);
    ...
alk
  • 69,737
  • 10
  • 105
  • 255