Henry
Henry

Reputation: 721

One dimensional array being passed into the main() function

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];
}

Upvotes: 1

Views: 32

Answers (2)

Henry
Henry

Reputation: 721

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];
}

Upvotes: 0

Hitokiri
Hitokiri

Reputation: 3689

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));

Upvotes: 1

Related Questions