What is the problem ?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the problem ?!

so I wrote this program i#include <stdio.h> int main() { int i,k,j,n,c,h; scanf("%d",&n); int a[n]; for(i=0;i<n;i++) scanf("%d",&a[i]); for (j=0;j<n;j+=2) { h = j+1; c = a[j]; a[j] = a[h]; a[n] = c; } for(k=0;k<n;k++) printf("%d\n",a[k]); return 0; } it's suppose when I enter a group of numbers it will change every number with the one next to it like 👉 {123456}_>{214365} but instead of That it just give me zero or the addresses

29th Oct 2018, 6:57 PM
mo🐼
mo🐼 - avatar
8 Answers
+ 6
can we see the code via the playground as it may help us help you
29th Oct 2018, 7:05 PM
BroFar
BroFar - avatar
+ 2
Inside your second for loop, the for(j = 0; ...) Replace: a[n] = c; With: a[h] = c; Hth, cmiiw
29th Oct 2018, 7:33 PM
Ipang
+ 2
and everytime I get this 👇 0 123456 0 2358772 0 4593840
29th Oct 2018, 8:50 PM
mo🐼
mo🐼 - avatar
+ 2
Taw🐼 that's odd, I am running your code but we have different output, I'll look it up again later tomorrow, now I'm too sleepy. Good luck mate ...
29th Oct 2018, 8:54 PM
Ipang
+ 1
I already tried that but it didn't work the same result without infinite loop
29th Oct 2018, 8:18 PM
mo🐼
mo🐼 - avatar
+ 1
OK the problem was in the way I enter the numbers😅 now I have the right output thanks for your help 👍
29th Oct 2018, 8:58 PM
mo🐼
mo🐼 - avatar
29th Oct 2018, 8:17 PM
mo🐼
mo🐼 - avatar
0
Taw🐼 I run the code with this input in Code Playground. The first 6 is the array size, next I used 1 to 6 to fill array. 6 1 2 3 4 5 6 Output: 2 1 4 3 6 5
29th Oct 2018, 8:23 PM
Ipang