Create an Array of 50 integers. Write a simple function to seach the inputted integer in the array. If the integer is found in the array return the index of that integer in that array. If integer is not found in the array just return -1 from that function.
Now you have got the index of the number. Write another function to swap the elements in the same array from 0 to the index you have found. Supply the array and the index to that function to swap the elemets.
If the element is not found in the array just supply the array and 49 to that function. It will swap all the elements in the array except the first element. And assing the inputted element to
Array[0] = element;
msaqib
Junior Poster in Training
91 posts since Sep 2004
Reputation Points: 9
Solved Threads: 1
You aren't storing the random numbers in a container, which is basically the idea behind this project. How else is the program supposed to know afterwards which numbers were generated?
As for moving the integers around - make a copy of the integer you plan to move once you've found it, and then simply copy all the nodes over one, and then you'll have an empty space at the end at which to insert your original node.
Consider something like this for moving a node to the top(pseudocode):
find integer (array)
copy integer to temp variable
for i = (position of node) to 1
copy i node to i-1 node
end for
Hope that made sense.
edit: too slow
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
How do you store 50 generated numbers in an array?
I'm in a good mood today, so here's some free code for you:
int array[50];
for (int iCount = 0; iCount < 50; iCount++)
{
array[iCount] = rand()%99;
}
regards Niek
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403