1

The below code works fine except for a one dimensional array I am trying to pass to main() from the function order_coef. I believe I am doing something wrong in the line where I specify *ordered_array=*array;, but not really sure what it is. Some help would be very much appreciated. Thank you!

#include <stdio.h>
#define COEF 5

int save_coef (int i, int *array);
int order_coef (int i, int *array, int *ordered_array);
int save_x (int x);
int resolve_polinomyal (int *array, int x, int i);

int main()
{
  int array[COEF], x = 1, i = 0, *ordered_array=0;

  printf("Please, enter 5 coefficients:\n");
  save_coef (i, array);

  order_coef (i, array, ordered_array);

  for (i = 0; i < COEF; ++i)
    printf("\n%d\n", ordered_array[i]);

  printf("Please, enter the value of x:\n");
  save_x (x);

  printf("%d", resolve_polinomyal (array, x, i));

  return 0;
}



int save_coef (int i, int *array)
{
  for (i = 0; i < COEF; ++i)
  {
    scanf("%d", &array[i]);
  }
return 0;
}

int order_coef (int i, int *array, int *ordered_array)
{
  int j, a;
  for (i = 0; i < COEF; ++i) 
  {
      for (j = i + 1; j < COEF; ++j)
      {
          if (array[i] > array[j]) 
          {
              a =  array[i];
              array[i] = array[j];
              array[j] = a;
          }
      }
  }

  *ordered_array=*array;

  return 0;
}

int save_x (int x)
{
    scanf("%d", &x);

  return 0;
}

int resolve_polinomyal (int *array, int x, int i)
{
  for (i = 5-1; i > 0; --i)
  {
    array[i-1] = array[i] * x + array[i-1];
  }
  return array[0];
}
3
  • What's the expected output and what are you getting? Commented May 12, 2020 at 20:45
  • Sorry, this code does not make sense. the array parameter in order_coef will be ordered. why do you need ordered_array ? and yes, all that statement does is assign whatever is in array[0] to ordered_array[0], and ordered_array has no backing memory. Commented May 12, 2020 at 20:48
  • hi @OldProgrammer exactly, it will be ordered. The only thing that is pending is how to pass the ordered array from the function order_coef into main() so that I can print it from there. Thank you! Commented May 12, 2020 at 20:50

2 Answers 2

1

The first problem is you do not allocate for ordered_array.

When you declare

int array[COEF], x = 1, i = 0, *ordered_array=0;

it means you initialized ordered_array = NULL.

You can use the static or dynamic allocation:

int ordered_array[COEF];
//
int *ordered_array = malloc(COEF*sizeof(int));

The second problem is *ordered_array=*array not copy array to array. It assigns first value of array to first value of ordered_array.

If you want to copy the value from an array to another one, you should use memcpy or a loop to copy value by value.

For example:

// copy the values of array to ordered_array
memcpy(ordered_array, array, COEF * sizeof(int));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Hitokiri!!! I guess I am still getting some brain meltdowns with the pointer concept! But again, thanks very much for your clear explanations, they are perfect!! :-)
0

Based on @Hitokiri comment I am pasting here the final solution with my code updated:

#include <stdio.h>
#define COEF 5

int save_coef (int i, int *array);
int order_coef (int i, int *array, int *ordered_array);
int save_x (int x);
int resolve_polinomyal (int *array, int x, int i);

int main()
{
  int array[COEF], x = 1, i = 0, ordered_array[COEF];

  printf("Please, enter 5 coefficients:\n");
  save_coef (i, array);

  order_coef (i, array, ordered_array);

  for (i = 0; i < COEF; ++i)
    printf("\n%d", ordered_array[i]);

  printf("\nPlease, enter the value of x:\n");
  save_x (x);

  printf("%d", resolve_polinomyal (array, x, i));

  return 0;
}



int save_coef (int i, int *array)
{
  for (i = 0; i < COEF; ++i)
  {
    scanf("%d", &array[i]);
  }
return 0;
}

int order_coef (int i, int *array, int *ordered_array)
{
  int j, a;
  for (i = 0; i < COEF; ++i) 
  {
      for (j = i + 1; j < COEF; ++j)
      {
          if (array[i] > array[j]) 
          {
              a =  array[i];
              array[i] = array[j];
              array[j] = a;
          }
      }
  }

   for (i = 0; i < COEF; ++i)
        ordered_array[i] = array[i];

  return 0;
}

int save_x (int x)
{
    scanf("%d", &x);

  return 0;
}

int resolve_polinomyal (int *array, int x, int i)
{
  for (i = 5-1; i > 0; --i)
  {
    array[i-1] = array[i] * x + array[i-1];
  }
  return array[0];
}

1 Comment

With int order_coef (int i, int *array, int *ordered_array) { ... for (i = 0; ..., code ignores the values of i passed into order_coef(). int i not needed as a function parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.