3

I was asked to code a program that asks for an integer 'n', then scan for 'n' integers, and then sort and print those integers in even numbers, ascending, then the odd numbers, descending.

So I began by having the even numbers in the first column of an array, and then the odd numbers in the second column, and then print them, but at the end I'm getting nothing but huge, similar numbers instead of the values I initially entered.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int array1_size, unfill;
  printf("How many integers do you wish to enter?\n");
  scanf("%d",&array1_size);
  int array1[array1_size][2];

  for (int i = 0; i < array1_size; ++i)
  {
    printf("Enter integer number %d\n", i+1);
    scanf("%d",&array1[i][0]);
  }

  for (int i = 0; i < array1_size; ++i)
  {
   if ( (array1[i][0] % 2) != 0 )
    {
      array1[i][1] = array1[i][0];
      array1[i][0] = unfill;
    }
  }

  printf("Your even numbers are:\n");
  for (int i = 0; i < array1_size; ++i)
  {
    printf("%d\n", array1[array1_size][0]);
  }

  printf("...and your odd numbers are:\n");
  for (int i = 0; i < array1_size; ++i)
  {
    printf("%d\n", array1[array1_size][1]);
  }

  return(0);
}
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • 2
    Huge numbers often are the result of not initialising variables. Please point out for each variable you use where you think it gets written first time, before it gets read. Start with `unfill`. – Yunnosch Jun 01 '20 at 14:15
  • 2
    You ignore the return value of `scanf()` at your own risk. – Yunnosch Jun 01 '20 at 14:16
  • 1
    What is the value of `int unfill` supposed to be? Doesn't the compiler warn you about this? – Weather Vane Jun 01 '20 at 14:18

1 Answers1

1

You declare

int array1[array1_size][2];

The highest-index legally accessable element inside that is

array1[array1_size-1][2-1]

This line is hence highly suspicious

printf("%d\n", array1[array1_size][1]);

Also you do not initialise unfill but copy its content elsewhere here

array1[i][0] = unfill

You possible leave the content of any array1[i][0] non initialised, because your code is vulnerable by a failing scanf(), because here you ignore the usually very helpful return value, which could warn you in case anything went wrong with scanning.

scanf("%d",&array1[i][0])

Any of these issues could be the explanation of your observed huge numbers, which sometimes are a symptom of using non-initialized variables or content of illegally accessed memory.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • I see... I've fixed the array's size problem that you mentioned, but it seems that it didn't do the job, so it seems that it could be related to the non-initialisation of 'unfill' like you said. But my issue is that the function of 'unfill' was to leave blank spaces in case the number in that slot was odd, changing to the other column, so that at the end it didn't print the leftover numbers, I don't know if I'm explaining myself correctly. In that case, how would I have to proceed? – Nicholas Vilches Jun 01 '20 at 15:07
  • First think of a value which will never occur in input. Maybe negative, maybe extremely high. Use that to init `unfill`. That will at least allow to verifiy that what you see is the unwanted content of `unfill`. Also check the return vaue of `scanf()`. Read its spec to learn about it. If it is not helpful enough read this http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html , the title might seem cynical, but it really is a nice and helpful read. – Yunnosch Jun 01 '20 at 15:54
  • Note, in case you consider updating the code in your question. Be careful to keep the code which matches my answer. StackOverflow does not appreciate "moving target" questions. But you can add something like "Based on the answer below I then tried the following code and found out that ...". Strictly speaking you should create a new question after using an answer. But I will be tolerant as long as my answer still makes sense and will let you know when it is time to make a new question. – Yunnosch Jun 01 '20 at 15:59