Make numbers in sequence

Hi,

I have a school project on basic C++ where the user will input numbers between 1 to 1000 and the code should make those numbers in sequence.

Like if the list is 1, 10, 20, 25 and the user enters 15 then it should be added between 10 and 20.

Thanks in advance.
So what have you tried?
A possible:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <set>
#include <iostream>

int main()
{
	std::multiset<int> msi;

	int no {};

	do {
		std::cout << "Enter a number between 1 - 1000 (0 to exit): ";
		std::cin >> no;
	} while (no > 0 && *msi.insert(no));

	for (const auto& n : msi)
		std::cout << n << ' ';

	std::cout << '\n';
}

Thanks a lot for the code <3 ..... I can't blv someone can go through that trouble for free. Good people still exist .... :) https://www.theengineeringprojects.com/2021/05/how-to-increase-ef-core-performance-for-saving-multiple-entities.html
Last edited on
Be careful if you turn that in. Most schools want you to struggle and fight to build your own solution to things the language can do for you in a couple of lines. This is a combined effect of outdated material, outdated professors (not always, but often), a desire for you to understand how to solve problems (what if you did not have a tool that did it for you, not all languages will have rich tools) and other such things. Even if your professor accepts this code, you will be called on to explain how it works and what a mulitset is, so you at the minimum need to study each line an be able to explain how it works and what it does. In doing so, you should also pick up on what you would need to do if you rolled out your own solution without multiset, so if asked to do it again without you should be able to visualize what needs to happen and recreate it.
If you turn it in and can't explain what a mulitset is or how it works, you will be given a bad score at best; some places may even treat it as cheating (most cheating at universities is marked only for major projects and tests, not often the homework which is just practice for tests and projects but they will still frown on just copying an answer you do not understand).

here is a way to do it using just an array:

unsigned int counts[1001]{}; // an array of 0 to 1000 inclusive set to all zeros.
now if you read in the value 42, you can do
counts[42]++;
after reading in all the values and doing the above increment to the appropriate array location,
a simple for loop puts it back out in order:
for(int i = 0; i < 1001; i++)
if(counts[i]) //did you have any of this value
//print it out, probably a loop of 0 to counts[i] cout i so if you had 23 42's in the data it would print 42 23 times, then check 43, 44, ... 1000...
Last edited on
:) :) Quite. In the OP there was no constraints mentioned (like using an array)...

So possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <limits>

int main()
{
	constexpr size_t numSze {1000};

	int nums[numSze] {};
	int no {};

	do {
		do {
			if (std::cout << "Enter a number between 1 - " << numSze << (" (0 to exit): "); !(std::cin >> no)) {
				no = -1;
				std::cin.clear();
				std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			}
		} while ((no < 0 || no > numSze) && (std::cout << "Invalid number\n"));
	} while (no > 0 && ++nums[no - 1]);

	for (size_t n1 = 0; n1 < numSze; ++n1)
		for (size_t n2 = 0; n2 < nums[n1]; ++n2)
			std::cout << n1 + 1 << ' ';

	std::cout << '\n';
}

Last edited on
Topic archived. No new replies allowed.