1-D array

Reply

Join Date: Apr 2006
Posts: 5
Reputation: jack999 is an unknown quantity at this point 
Solved Threads: 0
jack999 jack999 is offline Offline
Newbie Poster

1-D array

 
0
  #1
May 11th, 2006
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????
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: 1-D array

 
0
  #2
May 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 1-D array

 
0
  #3
May 12th, 2006
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: 1-D array

 
0
  #4
May 12th, 2006
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: jack999 is an unknown quantity at this point 
Solved Threads: 0
jack999 jack999 is offline Offline
Newbie Poster

Re: 1-D array

 
0
  #5
May 12th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 34
Reputation: CaliVagabond is an unknown quantity at this point 
Solved Threads: 0
CaliVagabond CaliVagabond is offline Offline
Light Poster

Re: 1-D array

 
0
  #6
May 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 1-D array

 
0
  #7
May 12th, 2006
> 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: jack999 is an unknown quantity at this point 
Solved Threads: 0
jack999 jack999 is offline Offline
Newbie Poster

Re: 1-D array

 
0
  #8
May 12th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: 1-D array

 
0
  #9
May 12th, 2006
you only need a single array as a parameter to swap(), and only use a single array within swap.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC