Salida no deseada en el programa C

I have a class project to make an array, declare it's size (add 1) then fill it in numerical, nondecreasing order. After that, I need to declare x, a value to be added to the array in the appropriate spot, so the array is still nondecreasing.

The program builds without error (for once!) but I'm getting some really weird outputs.

#include <stdio.h>

int main (void) {

//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;

printf("Enter the size of the array: ");
scanf("%d", &size);

printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) scanf("%d", &ary[i]);

printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);

while(i <= x && x > ary[i]){
j = size - 1;
    while(j >= i) {
        ary[j++] = ary[j];
for(i = 1; i < size; i++) {
 printf("%d", &ary[i]);

return 0;
} //main

When it runs I get:

Enter the size of the array: 3

Enter digits to fill the array, in numerical order: 1

Input x, the value to add to the array: 4
Process returned 0 (0x0)   execution time : 7.124 s
Press any key to continue.

preguntado el 27 de noviembre de 13 a las 01:11

Your printf statement is wrong - missing some separator (space or newline) and why are you taking the address of &ary[i] ? remove the addressof operator. -

4 Respuestas

I would examine your print function . It looks like you're printing the memory address of that particular array index rather than the value at that spot in the array.

respondido 27 nov., 13:01

Adding on to this, if the compiler gives you a warning, Por favor pay attention to it. If you have a warning and you don't know why, then your program is almost certainly wrong. - dennis meng

@DennisMeng The compiler didn't give me a warning. - Link Noneya

@LinkNoneya, remove the '&' entirely. That should print the correct value - popook88

After doing this I still get the wrong output. Enter the size of the array: 4 Enter digits to fill the array, in numerical order: 1 2 3 4 Input x, the value to add to the array: 5 2341630076519 Process returned 0 (0x0) execution time : 8.337 s Press any key to continue. - Link Noneya

@LinkNoneya Did you turn on all the flags? - dennis meng

Don't increase the size variable before you iterate through. Then walk backwards through the array, and populate as needed.

for (i = size; i >0; --i)
    if (ary[i-1] > x)
            ary[i] = ary[i-1];
            ary[i] = x;
if (i == 0) 
     ary[i] = x; 

Afterwards, you can increase size as you print the output.

I've reproduced your desired output using this array walk.

There are other ways that I would solve this problem in real life, but this works well enough within your existing code.

respondido 27 nov., 13:02

Oh, and be sure you're printing from i = 0, not i = 1. :) - russellm

This has given me the desired output. This logic is much better than the logic my professor gave. Thank you! - Link Noneya

Whoops, it seems to work for all but one test. When inputting values 2,3,4,5,6,7 then declaring x as 1, it returns 2,2,3,4,5,6,7. I expected 1,2,3,4,5,6,7. - Link Noneya

Edited the answer. I added a condition which traps heading all the way through the array to 0. - russellm

First things first: what do you EXPECT to see as the output?

Second things first: you aren't clearing out the memory for the array before use. You have garbage values in any slots you aren't using.

Third things first: using GCC I'm seeing at least two warnings without even trying.

test.c:32:15: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
     printf("%d", &ary[i]);
test.c:27:14: warning: unsequenced modification and access to 'j' [-Wunsequenced]
    ary[j++] = ary[j];

respondido 27 nov., 13:01

If I fill the first 4 values of the array with 1,2,3,4 and I then declare x = 5, I expect the output to be 1,2,3,4,5. But if I were to declare x=3, I would expect the output to be 1,2,3,3,4. - Link Noneya

Another issue I see is that in your output function, you iterate the loop starting at i=1 instead of 0. - russellm

In your printing loop your are not printing the value but your are printing the address of that element. Change the last loop to

 for(i = 1; i < size; i++) {
     printf("%d", ary[i]);

I am not sure why your are printing the array from index 1. But if you want to print whole array you should initialize i to zero in above loop.

respondido 27 nov., 13:02

This was suggested before and has been changed. But it has not solved the problem. - Link Noneya

What exactly you are doing.? Before while loop, value of i is greater than size and hence your are fetching the values from array beyond the size limit (garbage data) and also j will never be grater than i so your condition 'while(j >= i)' will never come true.Consider reseting i before outer while loop. - Patada de Vallabh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.