| | |
Help in program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 6
Reputation:
Solved Threads: 0
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:
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:
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int number; srand((unsigned)time(0)); int random_integer; for(int index=0; index<50; index++) { random_integer =(rand()%99)+0; cout << random_integer << endl; } cout<< " Enter a number between 0 & 99: "; cin>>number; }
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 12:56 pm. Reason: Added code tags, learn to use them.
•
•
Join Date: Sep 2004
Posts: 89
Reputation:
Solved Threads: 1
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;
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;
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):
Hope that made sense.
edit: too slow
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):
C++ Syntax (Toggle Plain Text)
find integer (array) copy integer to temp variable for i = (position of node) to 1 copy i node to i-1 node end for
edit: too slow
Last edited by John A; Feb 1st, 2007 at 11:46 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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):
Hope that made sense.C++ Syntax (Toggle Plain Text)
find integer (array) copy integer to temp variable for i = (position of node) to 1 copy i node to i-1 node end for
edit: too slow
I'm in a good mood today, so here's some free code for you:
regards Niek
c Syntax (Toggle Plain Text)
int array[50]; for (int iCount = 0; iCount < 50; iCount++) { array[iCount] = rand()%99; }
regards Niek
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: Sms
- Next Thread: Any help with pointers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






