GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2006, 22:23
jack999 jack999 is offline
New Member
 
Join Date: May 2006
Posts: 8
jack999 is on a distinguished road

1-D array


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)
  #2  
Old 11-May-2006, 22:40
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: 1-D array


Ok, thats your assignment and what do you want from us? Free code? Umm...no. Do some code, start the project and then we'll help you out where you get stuck. Please don't just post an assignment and expect us to do it for you.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #3  
Old 11-May-2006, 22:43
jack999 jack999 is offline
New Member
 
Join Date: May 2006
Posts: 8
jack999 is on a distinguished road

Re: 1-D array


my question is how can i swap the elements of the array????
  #4  
Old 11-May-2006, 22:55
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: 1-D array


Have you given it any thought at all? From your example your just reversing the array. You'll need two variables. Both will hold the values for the positions your swapping (i.e. array[1] and array[endofarray]). Now just put them back into the array in there new positions. You'll also need something that will let you know when you've reached the middle of the array. If you get stuck coding this, post the code and we'll help you.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #5  
Old 11-May-2006, 23:09
jack999 jack999 is offline
New Member
 
Join Date: May 2006
Posts: 8
jack999 is on a distinguished road

Re: 1-D array


Quote:
You'll need two variables. Both will hold the values for the positions your swapping (i.e. array[1] and array[endofarray]). Now just put them back into the array in there new positions. You'll also need something that will let you know when you've reached the middle of the array. If you get stuck coding this, post the code and we'll help you


i got stuck when i read that
  #6  
Old 11-May-2006, 23:44
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: 1-D array


here's some sudeo code:
Code:
temp1 = first array value temp2 = last array value first array value = temp2 last array value = temp1

all you have to do is put that into c code. I can't really explain it any better without doing it for you.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #7  
Old 12-May-2006, 09:30
jack999 jack999 is offline
New Member
 
Join Date: May 2006
Posts: 8
jack999 is on a distinguished road

Re: 1-D array


in question he says (The swap operation should be done on one array)

if i used

Code:
temp1 = first array value temp2 = last array value first array value = temp2 last array value = temp1

will it be correct
  #8  
Old 12-May-2006, 11:05
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: 1-D array


Yes it will 'cuz you're not using an array for the swap and thus not "violating" the question.

A better algo would be this:

Step 1: decalre 2 int variables, say i & j
Step 2: set i to start of array and j to end of array
Step 3: take a third int variable say temp
Step 4: now assign array[i] to temp
Step 5: then assign array[j] to array of array[i]
Step 6: now copy temp to array[i]
Step 7: increment i by 1 and decrement j by 1
Step 8: repeat steps 3 onwrds untill j becomes less than or equal to i.
Step 9: Celerate you have a new weapon in your C++ arsenel.


You're using 1 less variable this way. Do u get it... I've tried my best to explain you what should be done without the code... now go to your drawing board and try to convert it into c/c++ code (don't write a code for step 9 though )... then come back if you're stuck anywhere.

Regards,
Netnut.
  #9  
Old 12-May-2006, 12:02
jack999 jack999 is offline
New Member
 
Join Date: May 2006
Posts: 8
jack999 is on a distinguished road

Re: 1-D array


i solved it but the problem is that the first element of new array is always 0
CPP / C++ / C Code:
#include<stdio.h>
main( )
{
int	i,n,j ;
float	arr[5], arr_new[5]   ;
int temp;


for (i=0 ; i<5 ; ++i) 
      {
printf ("Enter the %dth element of array :", i) ;
scanf ( "%f", &arr[i] ) ;

       }

for (i=0 ; i<5 ; ++i) 
      {
printf ("the value of element #%d is %f \n", i,arr[i]) ;

       }
for( i = 0, j = 5; i<5; i++,j--)
{
temp = arr[i];
arr[i] = arr_new[j];
arr_new[j] = temp;
}



 for (i=0 ; i<5 ; ++i) 
      {
printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;

       }
}

Last edited by LuciWiz : 15-May-2006 at 01:52. Reason: Please insert your C code between [c] & [/c] tags
  #10  
Old 12-May-2006, 12:40
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: 1-D array


A few things. When I compile this with gcc using gcc -Wall array_swap.c -o ArraySwap I get the following warnings.

Code:
array_swap.c:3: warning: return type defaults to `int' array_swap.c: In function `main': array_swap.c:4: warning: unused variable `n' array_swap.c:35: warning: control reaches end of non-void function

The ones to be concerned with first is line 1, line 2, and line4. You can deal with line 3 later. The first one is telling you that since there is no return type for main, it will default to int. Why, well you can search here in the forums and find many good explanations for this. Suffice it to say though, main() should return an int. It is why line 4 says you have reached the end of a non-void ( int main() ) function.

Granted this may all be different based on your error reporting level (do yourself a favor and crank it up to extra loud) and compiler. You should get something similar though. Line two just lets you know where in the program it is and each line pointing to something specific has a line number.

Now, when I run your program, this is the output I get.
Code:
$ ArraySwap Enter the 0th element of array :3 Enter the 1th element of array :9 Enter the 2th element of array :1 Enter the 3th element of array :8 Enter the 4th element of array :3 the value of element #0 is 3.000000 the value of element #1 is 9.000000 the value of element #2 is 1.000000 the value of element #3 is 8.000000 the value of element #4 is 3.000000 the new array value of element #0 is 165964683143020544000.000000 the new array value of element #1 is 3.000000 the new array value of element #2 is 8.000000 the new array value of element #3 is 1.000000 the new array value of element #4 is 9.000000

Seems to be a smidge of a problem.

The input is getting stored but the new array seems to have a problem. Take a shot and post your further questions.

BTW, in your requirements you have,
Quote:
Originally Posted by project requirements
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

You have two arrays, (I assume you are using only 5 for testing) not just the one your requirements say to have. It sounds like this is to be done in place, so using the temp variable will be the holding spot as previously mentioned.

HTH,
Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 12:21
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 02:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.