![]() |
|
#1
|
|||
|
|||
1-D arrayWrite 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
|
||||
|
||||
Re: 1-D arrayOk, 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
|
|||
|
|||
Re: 1-D arraymy question is how can i swap the elements of the array????
|
|
#4
|
||||
|
||||
Re: 1-D arrayHave 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
|
|||
|
|||
Re: 1-D arrayQuote:
i got stuck when i read that |
|
#6
|
||||
|
||||
Re: 1-D arrayhere's some sudeo code:
Code:
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
|
|||
|
|||
Re: 1-D arrayin question he says (The swap operation should be done on one array)
if i used Code:
will it be correct |
|
#8
|
|||
|
|||
Re: 1-D arrayYes 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 Regards, Netnut. |
|
#9
|
|||
|
|||
Re: 1-D arrayi solved it but the problem is that the first element of new array is always 0
CPP / C++ / C Code:
Last edited by LuciWiz : 15-May-2006 at 01:52.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#10
|
||||
|
||||
Re: 1-D arrayA few things. When I compile this with gcc using gcc -Wall array_swap.c -o ArraySwap I get the following warnings.
Code:
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:
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:
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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