User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,548 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,919 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 955 | Replies: 4
Reply
Join Date: Feb 2007
Posts: 6
Reputation: samernic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
samernic samernic is offline Offline
Newbie Poster

Help Help in program

  #1  
Feb 1st, 2007
A container that holds 50 distinct integers has two ends: top and bottom. When an input integer matches one of the integers in the container, it is then moved to the top, and all the integers above the matched integer are moved down to fill the gap in the container (keeping the same order). When none of the integers in the container matches the input value, the bottom integer is discarded, the remaining integers are moved down to fill the gap (keeping the same order), and the input integer is inserted on top.
Write a C++ program to implement the above problem. Fill the initial container with distinct random integers in the range 0 to 99. Prompt the user to enter an integer, output the updated container. Repeat until the user enters a (any) negative value.

I got to generating the different numbers and output them but I don't know how to input an integer and compare it to those numbers.
If anyone has an idea let me know thanks.


Here is my code and the ouput:
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int number;
  10. srand((unsigned)time(0));
  11. int random_integer;
  12. for(int index=0; index<50; index++)
  13. {
  14. random_integer =(rand()%99)+0;
  15. cout << random_integer << endl;
  16. }
  17. cout<< " Enter a number between 0 & 99: ";
  18. cin>>number;
  19. }
output:

93
85
63
42
77
70
56
45
69
54
57
39
63
21
47
30
37
9
64
94
73
57
78
36
54
1
50
76
54
43
62
48
27
24
88
5
94
43
50
65
96
7
3
60
26
50
88
64
58
53
Enter a number between 0 & 99:
Last edited by ~s.o.s~ : Feb 2nd, 2007 at 11:56 am. Reason: Added code tags, learn to use them.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Plymouth, UK
Posts: 79
Reputation: msaqib is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

Re: Help in program

  #2  
Feb 1st, 2007
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;
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,500
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Help in program

  #3  
Feb 1st, 2007
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
Last edited by John A : Feb 1st, 2007 at 10:46 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Feb 2007
Posts: 6
Reputation: samernic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
samernic samernic is offline Offline
Newbie Poster

Re: Help in program

  #4  
Feb 2nd, 2007
Originally Posted by joeprogrammer View Post
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

How do you store 50 generated numbers in an array?
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,776
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Help in program

  #5  
Feb 2nd, 2007
Originally Posted by samernic View Post
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:
  1.  
  2. int array[50];
  3. for (int iCount = 0; iCount < 50; iCount++)
  4. {
  5. array[iCount] = rand()%99;
  6. }

regards Niek
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC