+ 2
Array issues: Backward scanning[SOLVED]
So I’m still working on trying to get these arrays down and I’m having trouble. I attached the following code. What I am trying to do is scan integer values, then reprint the values in the opposite order in which they were entered. https://code.sololearn.com/ctLIyhMVtjG8/?ref=app
12 odpowiedzi
+ 4
This would work:
#include <stdio.h>
int main(void) {
int A[20];
int len,x;
int i=0;
scanf("%i",&len);
for (i=0; i < len; i++)
{
scanf("%i",&A[i]);
}
int temp;
x=len-1;
for (i=0; i < len / 2; i++,x--)
{
temp=A[i];
A[i]=A[x];
A[x]=temp;
}
for (i = 0; i < len; i++) {
printf("%i ",A[i]);
}
return 0;
}
Some changes are:
- fixed swap.
Replaced:
temp=A[i];
A[x]=temp;
with:
temp=A[i];
A[i]=A[x];
A[x]=temp;
The original 2 lines didn't swap. It erased the old A[x] value and just copied A[i] to both. It is the same as A[x] = A[i].
- Iterated i over only half of the array. Iterating i the full length reverses the array twice. Reverse of reverse is the original array. That accomplishes nothing.
+ 2
Alejandro wrote, "I have a question, if you dont mind me asking. Would you say there is a more efficient way of writing this code or is this appropriate then?"
Response:
It depends what you mean by efficiency.
If you mean time complexity, no. You need O(len) time to scan in len numbers. Everything after that is already no worse than O(len) so it has an optimal time complexity.
If you want shorter and simpler code to print the same result, yes. The array could be printed in reverse order instead of reversing its content and then printing it like below.
#include <stdio.h>
int main(void) {
int A[20];
int len;
int i=0;
scanf("%i",&len);
for (i=0; i < len; i++)
scanf("%i",&A[i]);
for (i=len - 1; i >= 0; i--)
printf("%i ",A[i]);
return 0;
}
This is another solution using recursion instead of the array and for-loops. Some people probably find recursion a little more readable even if it is roughly the same length:
#include <stdio.h>
void printReverse(int len) {
if (len > 0) {
int n;
scanf("%i", &n);
printReverse(len - 1);
printf("%i ", n);
}
}
int main(void) {
int len;
scanf("%i",&len);
printReverse(len);
return 0;
}
+ 1
Alejandro wrote, "Hey Josh,
I tried it out, and that didn’t seem to work. I did something similar to that before I got to what is my original code on the post.
Below is your running code.
https://code.sololearn.com/c6uMCj829kw6/?ref=app"
Response:
Alejandro, I tested my code( like you pasted in https://code.sololearn.com/c6uMCj829kw6/?ref=app ) with these inputs:
5
1
2
3
4
5
The code prints:
5 4 3 2 1
Without the reverse loop, it would print:
1 2 3 4 5
That's reverse order, right? How did you test it?
+ 1
My bad Josh, this is 100% correct. Running a little low on sleep so I made a numskull mistake. Thanks a ton for your help!
+ 1
Alejandro wrote, "My bad Josh, this is 100% correct. Running a little low on sleep so I made a numskull mistake. Thanks a ton for your help!"
Response:
Cool.
+ 1
Thank you, this was very helpful !
0
Hey Josh,
I tried it out, and that didn’t seem to work. I did something similar to that before I got to what is my original code on the post.
Below is your running code.
https://code.sololearn.com/c6uMCj829kw6/?ref=app
0
I have a question, if you dont mind me asking. Would you say there is a more efficient way of writing this code or is this appropriate then?
0
lpang wrote, "Instead of swapping elements after reading and saving them into the array, you can read and save the elements into the array in reverse index positioning."
Response:
Yes. There are many ways to solve this.
I mentioned the following code which prints in the opposite order to the order it reads with. As long as the print is opposite order to the reading indexes, it'll work:
#include <stdio.h>
int main(void) {
int A[20];
int len;
int i=0;
scanf("%i",&len);
for (i=0; i < len; i++)
scanf("%i",&A[i]);
for (i=len - 1; i >= 0; i--)
printf("%i ",A[i]);
return 0;
}
0
Josh,
Is there any problem?
My response was for Alejandro the OP, not you.
0
Josh,
I have decided to move my response as a comment in the OP's code. Hope you feel more secure by that.
0
Here's another way to solve this,:
#include <stdio.h>
int main() {
int a, b[30], i;
scanf("%d", &a);
for (i=0;i<a;i++) scanf("%d", b+i);
for (i--;i>=0;i--) printf("%d\n", b[i]);
return 0;
}
// Hope this helps