944,131 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2774
  • C RSS
May 11th, 2006
0

1-D array

Expand Post »
Write a C-Program that swaps the elements of 1-D array (10 elements) :
Example:
If the given array is:
5 8 9 2 3 1 11 17 43 6
The new array will be:
6 43 17 11 1 3 2 9 8 5
Your program should consist of the following functions:
1. Function READ to read the elements of the array
2. Function Display to print the elements of the new array
3. Function SWAP to swap the elements of the array
4. main function to call the previous functions
Note: in swap function, do not use an intermediate array for swapping, i.e. do not
declare a new array and fill it with the elements of the original array starting from the
last element. The swap operation should be done on one array

The size of array must be N (Entered by user)



my question is how can i swap the elements of the array????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jack999 is offline Offline
5 posts
since Apr 2006
May 12th, 2006
0

Re: 1-D array

hi
looking at ur requierment, u can write in swap funtion like
  1. for(int i=0; i<=4; ++i) {
  2. arr[i] = arr[i] + arr[9-i] ;
  3. arr[9-i] = arr[i] - arr[9-i] ;
  4. arr[i] = arr[i] - arr[9-i]
  5. }
hope this should work for u.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
May 12th, 2006
0

Re: 1-D array

> looking at ur requierment, u can write in swap funtion like
What is wrong with using a temporary variable?

Not to mention the possibility of data corruption due to whatever way you machine handles arithmetic overflow and underflow.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 12th, 2006
0

Re: 1-D array

>arr[i] = arr[i] + arr[9-i] ;
>arr[9-i] = arr[i] - arr[9-i] ;
>arr[i] = arr[i] - arr[9-i]
That's clever and braindead at the same time. Unless of course you didn't discover that solution, in which case it's just braindead.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 12th, 2006
0

Re: 1-D array

i solved it but the problem is that the first element of new array is always 0

  1. #include<stdio.h>
  2. main( )
  3. {
  4. int i,n,j ;
  5. float arr[5], arr_new[5] ;
  6. int temp;
  7.  
  8. for (i=0 ; i<5 ; ++i)
  9. {
  10. printf ("Enter the %dth element of array :", i) ;
  11. scanf ( "%f", &arr[i] ) ;
  12. }
  13. for (i=0 ; i<5 ; ++i)
  14. {
  15. printf ("the value of element #%d is %f \n", i,arr[i]) ;
  16. }
  17. for( i = 0, j = 5; i<5; i++,j--)
  18. {
  19. temp = arr[i];
  20. arr[i] = arr_new[j];
  21. arr_new[j] = temp;
  22. }
  23.  
  24. for (i=0 ; i<5 ; ++i)
  25. {
  26. printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;
  27. }
  28. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jack999 is offline Offline
5 posts
since Apr 2006
May 12th, 2006
0

Re: 1-D array

in the for loop that swaps the values, you need to initialize j as 4 and not 5. otherwise it starts by accessing an array element that has not been initialized.
Reputation Points: 10
Solved Threads: 0
Light Poster
CaliVagabond is offline Offline
34 posts
since Apr 2006
May 12th, 2006
0

Re: 1-D array

> for( i = 0, j = 5; i<5; i++,j--)
The last element of the array is at index 4, not index 5

for ( i = 0 ; i < 5 ; i++ ) // going forwards
for ( i = 4 ; i >= 0 ; i-- ) // going backwards
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 12th, 2006
0

Re: 1-D array

guys i really got confused when u tryed yo solve iy with functions
  1. #include<stdio.h>
  2. void read(float arr[],int n)
  3. {
  4. int i ;
  5. for (i=0 ; i<n ; ++i)
  6. {
  7. printf ("Enter the %dth element of array :", i) ;
  8. scanf ( "%f", &arr[i] ) ;
  9. }
  10. void swp(float arr[],float arr_new[],int n)
  11. {
  12. int i,j, temp;
  13. for( i = 0, j = n-1; i<n; i++,j--)
  14. {
  15. temp = arr[i];
  16. arr[i] = arr_new[j];
  17. arr_new[j] = temp;
  18. }
  19. }
  20. void display(float arr_new[]);
  21. {
  22. int i;
  23. for (i=0 ; i<n ; ++i)
  24. {
  25. printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;
  26. }
  27. }
  28. void main()
  29. {
  30. float a[],b[], c[];
  31. int m;
  32. printf ("Enter the size of array :") ;
  33. scanf("%d",&m);
  34. read(a,m);
  35. swap(a,b,m);
  36. display(c);
  37. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jack999 is offline Offline
5 posts
since Apr 2006
May 12th, 2006
0

Re: 1-D array

you only need a single array as a parameter to swap(), and only use a single array within swap.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: compile error
Next Thread in C Forum Timeline: Menu repeats itself





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC