Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reverse array elements

array element

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1 alexie

alexie

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 03 January 2013 - 06:18 AM

Hello.

I have some question i don't understand well, so i hope someone can explain clearly to me :)

Reverse Array

Program to reverse array elements. To invert/reverse array elements.
Read from opposite direction/right-to-left.
E.g
Input : "ar[0]=a, ar[1]=b, ar[2]=c", Output : "ar[0]=c, ar[1]=b, ar[2]=a",
Program should be able to read in/accept any input array size of alphanumeric elements.

Many Thanks!

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 03 January 2013 - 06:28 AM

Suppose, the array is [0, 2, 5, 6, 8, 3, 2]. It will be [2, 3, 8, 6, 5, 2, 0] after reversing. You got it right. So, I'm not sure what is/are your question(s)?

#3 alexie

alexie

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 03 January 2013 - 06:45 AM

i don't understand the meaning for ar[0]=a. is it input number or word?

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 03 January 2013 - 06:50 AM

i don't understand the meaning for ar[0]=a. is it input number or word?

In the example in your post, ar is the array where 'a' is the content in the first cell/room of the array, 'b' is the content of the second cell/room of the array and 'c' is the content in the third cell/room of the array. They are 0, 2 & 5 in my example respectively.

#5 alexie

alexie

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 03 January 2013 - 07:06 AM

so if i need to input 5 number i need to set up like this : ar[0]=a, ar[1]=b, ar[2]=c, ar[3]=d, ar[4]=e
or is there any short cut?

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 03 January 2013 - 07:11 AM

so if i need to input 5 number i need to set up like this : ar[0]=a, ar[1]=b, ar[2]=c, ar[3]=d, ar[4]=e
or is there any short cut?

Well, you can take the input and set the array in a loop.

#7 alexie

alexie

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 03 January 2013 - 07:34 AM

Oh. can you show me a example? thanks again :)

#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 03 January 2013 - 07:56 AM

Oh. can you show me a example? thanks again :)

Here is a sample particularly specific to your example --
#include <stdio.h>

int main(int argc, char *argv[]){
int i;
char ar[5];

for(i = 0; i < 5; ++i) {
printf("\nInput character %d: ", i + 1);
scanf(" %c", &ar[i]);
}

return 0;
}


#9 object

object

    CC Regular

  • Member
  • PipPipPip
  • 35 posts

Posted 03 January 2013 - 01:31 PM

Reversing arrays is easy. Start at the beginning and end of your array, swapping the two elements and working your way inwards until the two indexes come together or overlap.

kernelcoder: What is the significance of the return value of scanf?




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download