Probably the wrong forum!! But I know people here are kind and maybe there's a hidden programmer here somewhere?

Background: Total beginner, doing an introductory C++ course in year 1 of university. I have a thing due tomorrow at 12:00 CET that'll help me tremendously if I manage to solve the problem. The exercise is focused on loops and vectors. But I'm completely stuck!!! I will give my soul to whoever can help me with this (or maybe a cheap game, yes I'm that desperate):

I hope I'm writing this in an understandable way:
The program wants you to put in 10 integers with a value of [1,99]. The program will ask if you want to put in 10 more until you exit the loop. At the end, if every single number from 1 to 99 can be found in the vector containing the values you input (meaning that you have entered all number 1 to 99 sometime) then a score of 1000 will be added.

The vector is done, no problem storing all the values. HOWEVER I don't have a clue how I'm supposed to get the program to check if ALL the values from 1 to 99 are there. I can check if one of them is there, but the condition is that all the numbers have to be there. How???

I'm thinking of some kind of loop inside a for-loop, like this:
https://imgur.com/hLJtMTG

Example of how it's supposed to look like:
Enter your numbers: 80 3 87 62 30 90 10 21 46 27
One more time? 1
Enter your numbers: 12 49 83 9 39 88 95 59 20 37
One more time? 1
Enter your numbers: 80 40 87 67 31 90 11 24 56 77
One more time? 1
Enter your numbers: 11 48 51 42 8 74 64 41 36 53
One more time? 1
Enter your numbers: 52 82 16 72 19 70 44 56 29 33
One more time? 1
Enter your numbers: 54 1 99 14 23 22 94 79 55 2
One more time? 1
Enter your numbers: 60 86 34 4 31 63 84 89 7 78
One more time? 1
Enter your numbers: 43 93 97 45 25 38 28 26 85 49
One more time? 1
Enter your numbers: 47 65 57 67 73 69 32 71 24 66
One more time? 1
Enter your numbers: 92 98 96 77 6 75 17 61 58 13
One more time? 1
Enter your numbers: 35 81 18 15 5 68 91 50 76 12
One more time? 0
Winning amount: 1000

3 years ago

Comment has been collapsed.

I program, but not in C++ specifically. If I was solving a problem like this, the quickest and dirtiest way of doing it would be to take all the values entered, and then loop through from 1-99. If the code can find that value of your current loop integer in that array of saved values, then carry on to the next. Otherwise exit the loop and fail. If it gets to 99 and none have failed, then add the score.

You could also find all the unique values first, do a quick test to make sure there are 99 values there before entering the loop just to avoid wasting time.
As I say, don't code in C++ specifically, but I'm sure the functions to do this are easy enough to find.

3 years ago
Permalink

Comment has been collapsed.

Yep, this is at the level of "I haven't even done programming for a month". I don't even know what to google to find an answer to what I'm looking for (and the results I do find deal with more advanced code than mine). What you've written sounds right though :)

3 years ago
Permalink

Comment has been collapsed.

This sound pretty much a simplest brute force solution.

3 years ago
Permalink

Comment has been collapsed.

This.

Not super-familiar with C++, but the example snippet...

  [...additional code defining a Vector called "fifth"...]

  std::cout << "The contents of fifth are:";
  for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

...found in the cplusplus reference for Vector may point you in the right direction.

Do you know if you can use vector::iterator?

Edit: (hint because you probably haven't encountered pointers/references (yet?) -- the asterisk in "*it" basically means "the value at the memory location it")

3 years ago*
Permalink

Comment has been collapsed.

Make a boolean array with length 99 (all 'false').
Instead of saving all the inputs, and wasting space & looping time - set the corresponding cell in the array to 'true' for each input.
After all inputs are done, go over the array and see if everything's true or not.

3 years ago
Permalink

Comment has been collapsed.

I don't think I can use array since we haven't gone over it yet (but from what I understand they're similar to vectors?). Either way I have trouble visualizing how this is supposed to look like in the code, but I appreciate the help anyway :(

3 years ago
Permalink

Comment has been collapsed.

It's essentially the same (a list of values). If you want to do it that way, make a std::vector<bool>.

3 years ago
Permalink

Comment has been collapsed.

I would think C++ array is more basic than std::vector. But if you can't use arrays, as the person below stated, you could do std::vector<bool>, or even std::vector<int> for less space-efficient code.

3 years ago
Permalink

Comment has been collapsed.

Why no love for std::bitset<99>?

3 years ago
Permalink

Comment has been collapsed.

Whaaa... They're teaching you vectors before arrays? Then you should be allowed to use std::bitset...

    // 1. define this somewhere
    std::bitset<99> bits;

    // 2. when reading user input
    if (value >= 1 && value <= 99)
            bits.set(value - 1, true);

    // 3. after loop
    if (bits.all())
        score += 1000;
3 years ago*
Permalink

Comment has been collapsed.

This solution is perfect and easy to understand if it's okay to process at input time!

3 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

3 years ago*
Permalink

Comment has been collapsed.

I'd do it this way too, except I'd use a counter that increases by 1 every time a number is tried for the first time. That way we'd never have to run through the array to check if we're done - just see if the counter has reached 99 yet.

3 years ago
Permalink

Comment has been collapsed.

^ This

3 years ago
Permalink

Comment has been collapsed.

You'd still have to remember which number has been entered before and check for each input. Why not sort and eliminate duplicates? Then the size of the vector/list/array/whatever tells you how many unique numbers were entered. If it's 99, you've won the video game. ;)

3 years ago
Permalink

Comment has been collapsed.

Sorting 10 members on each new input vs simply storing 10 booleans... hmm :)

Also, these assignments come before they teach sorting.
Honestly, anything we write here will get disqualified due to the OP being unlikely to have come up with the solution themselves.

3 years ago*
Permalink

Comment has been collapsed.

The bool method seems the easiest, no doubt. I like the solution with the bitset above. But sorting is at least better than the thing with the counter. I also wouldn't sort after each input. Just one sort and one unique at the end is enough.

3 years ago
Permalink

Comment has been collapsed.

inb4 1000 rounds of input to sort (in one go or not) :D

And yes, Rosebonbon's bitset implementation looks great (don't know C/C++ to verify)

3 years ago
Permalink

Comment has been collapsed.

With the +1 method - you are going to check each and every time if it's new or not, so it defeats the purpose.

3 years ago
Permalink

Comment has been collapsed.

Checking if a value in a vector is true is very fast, and it means you no longer need to check the entire vector at the end:

// for each input (input has already been checked to be between 1 and 99):
if (!number_used[input-1])
{
    unique_numbers++;
    number_used[input-1] = true;
}

// At the end of the loops:
if (unique_numbers == 99)
    points += 1000;

Yo can actually get rid of the conditional statement using some bitwise math instead, but I wouldn't do it that way in an introductory course.

unique_numbers += number_used[input-1] ^ 1;
number_used[input-1] = 1;
3 years ago*
Permalink

Comment has been collapsed.

3 years ago
Permalink

Comment has been collapsed.

There's one important thing to consider: all the first part of the program is blocked most of the time waiting for user input. So having an extra conditional there will never affect performance in any measurable way.

It's only the final part (when you check if all 99 numbers have been entered by the user) when there's no user input, and where performance may matter (which it won't in this case either, since the vector is too small).

The only meaningful optimization you can do is stopping asking the user for more numbers once the counter reaches 99, since he just won. You save the user some time and typing. But this may not comply with the exercise statement.

3 years ago*
Permalink

Comment has been collapsed.

Check what every time what, so it defeats what purpose?

3 years ago
Permalink

Comment has been collapsed.

You're running through parts of the array each time you're comparing inputs, so you'll end up checking all its elements if all of them are true anyway.
Please see my reply to oginer above, as both solutions are pretty much identical in performance. We're arguing over miniscule optimizations for a total beginner level task :)

3 years ago
Permalink

Comment has been collapsed.

We're arguing over miniscule optimizations for a total beginner level task :)

Not at all. All I said was that I'd have done something similar to what you'd have done, but not exactly the same (because that would be a lie). Why obsess over performance? It really doesn't matter for this.

3 years ago
Permalink

Comment has been collapsed.

(i dont do c++ btw) i would probably go for something like:
kinda similar to MjrPITA but like reverse. set array with 99 length and on input unset inputted values. if array is empty at the end, you are good. this way you kinda won't need to loop

3 years ago
Permalink

Comment has been collapsed.

You could sort it, making things simpler.

3 years ago
Permalink

Comment has been collapsed.

Sorry if I misunderstood your problem:

You put the numbers into a textbox right? Maybe add create an INT named <textbox_value> or something (without the "<>" obv>, and let that have the entered value of the textbox.

Then let the program check the <textbox_value> and if it's within the allowed value range say to add the value of <textbox_value> to the vector and reset the <textbox_value>.

3 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

3 years ago
Permalink

Comment has been collapsed.

Do you only get 1000 points, if you enter all the numbers? What if I enter 98 unique numbers? Do I get 0 points? Or do I get 10 points per unique number?

3 years ago
Permalink

Comment has been collapsed.

You'd get 0 points. (Also you wouldn't be able to enter 98 numbers, when entering your numbers you have to enter exactly 10 at a time for reasons)

3 years ago
Permalink

Comment has been collapsed.

But you ask the user if he wants to input values one more time. If he says 0 (no), then you cancel the input, as I understood it. Else there wouldn't really be reason to ask him. So he can input 10, 20... 100 values.

Anyway, std::list has methods to sort and eliminate duplicates. If you eliminate all duplicates, eliminate the value 0 if it exists and still have 99 values left, then those must be the numbers from 1 to 99. You can do it with std::list, or you can use a std::vector and use std::unique. Hope that helps.

3 years ago
Permalink

Comment has been collapsed.

I made a quick solution for what I think you want. So if you want a solution, try this. If you want to find the solution yourselff, ignore it, please. Again, if you are required to use std::vector, us it instead of std::list and try a similar approach with std::unique.

3 years ago
Permalink

Comment has been collapsed.

but you can enter the same number multiple times, not?

3 years ago
Permalink

Comment has been collapsed.

Yeah, sure. I wasn't able to solve this in time though, my time had to go to fixing some problems with the code :/

3 years ago
Permalink

Comment has been collapsed.

I think you are looking for a frequency vector. It's purpose it's storing the number of appearances of a specific number in a list of numbers.
The flow goes like this:
make a vector the size of 100.(0-99)
1.input numbers(ex.1 , 88, 23)
2.your vector will look like this
3.{0,0,0,0,0,0,0,....,0,0} . You go through it and add +1 on that position
{0,1(position 1),0,0,.....,1(position 23),0,...,1(position 88),0,...}
4.check vector if all numbers are bigger than 0.
5.Repeat if 4 returns 4 from step 1

Here is an example with 2 input numbers for number 0-9

  1. check if vector has all elements >0
    Vector(0,0,0,0,0,0,0,0,0,0)
    input 2,5

2.check if vector has all elements >0
Vector(0,0,1,0,0,1,0,0,0,0)
input 5 ,9
3.check if vector has all elements >0
Vector(0,0,1,0,0,2,0,0,0,1)
input 0,1
4.check if vector has all elements >0
Vector(1,1,1,0,0,2,0,0,0,1)
input 3,9
5.check if vector has all elements >0
Vector(1,1,1,1,0,2,0,0,0,2)
input 9,9
6.check if vector has all elements >0
Vector(1,1,1,1,0,2,0,0,0,4)
input 4,6
7.check if vector has all elements >0
Vector(1,1,1,1,1,2,1,0,0,2)
input 7,8
8.check if vector has all elements >0
Vector(1,1,1,1,1,2,1,1,1,2)
Done

I didn't use vectors in a long time but this might help
https://www.geeksforgeeks.org/program-to-find-frequency-of-each-element-in-a-vector-using-map-in-c/

Feel free to ask for any more help.

3 years ago*
Permalink

Comment has been collapsed.

It would help a lot if you could give us any restrinction: size or time( log(n) or whatever)

3 years ago
Permalink

Comment has been collapsed.

There are no restrictions really, if I get it to work in any way I pass. Thank you for the help, I'm going to look into what you wrote more tomorrow when I've had some sleep (the exercise itself has more requirements and I've been working on this thing all day...)

3 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

3 years ago
Permalink

Comment has been collapsed.

So many helpful people here! I think I'm too tired to understand anything right now. Hopefully I can fix this thing in the 4-5 hours I have when I wake up tomorrow...

3 years ago
Permalink

Comment has been collapsed.

A simple solution:

Create a vector the size of 100. (Since I guess C++ array indexes start with 0. To keep it simple we won't touch that.)
Set every element of it to 0. (In a loop)

Ask for input.
Use the numbers you got as indexes of the vector and set its corresponding elements to 1. (eg: If the entered numbers were 80, 45, 22, etc. Then vector[80]=1, vector[45]=1, etc.)
Repeat this process while you get inputs.

After there are no inputs:
Create an integer and set it 0. (eg int differentnumbers=0)
In a loop (from 1 to 99) check the vector's elements, if vector[i]==1 then differentnumbers++.
In the end, if differentnumbers==99 add 1000 points.

I don't know if the terms I used are correct, I haven't had to speak about programming in English before so I'm not 100% about the terminology.

3 years ago
Permalink

Comment has been collapsed.

https://pastebin.com/z8MRfNbb
UPD: I'm using std::vector<bool> and std::all_of. It's simple and short.
UPD2: It's similar to https://www.steamgifts.com/discussion/Bz8So/c-crisis-help#4wmtWYk.
UPD3: std::vector<bool> v(99); is better than std::vector<bool> v(99); v.resize(99);, of course.

3 years ago*
Permalink

Comment has been collapsed.

Very elegant solution. Didn't even know std::all_of.

3 years ago
Permalink

Comment has been collapsed.

also you wanna put a defensive check for negative inputs, otherwise the vector assignment is out of bound..

UPD: oh never mind, you're using unsigned

3 years ago
Permalink

Comment has been collapsed.

I haven't written a simple C++ program in a while, so here you go:
Solution using array.
Same solution but using Vector.

If you need anything explained in more detail, feel free to add me and ask, though do keep in mind that I haven't done proper programming in years. :/

3 years ago*
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 11 months ago.

3 years ago*
Permalink

Comment has been collapsed.

Looks like others have beat me to it. I program in Java at the enterprise level, but I have not had the immense pain joy of dealing with C++. I wish you the best with your assignment and studies though :)

3 years ago
Permalink

Comment has been collapsed.

My program had a problem with another aspect so I figured out that I had to skip the big ass vector (I used push_back to store the inputted values at the beginning of the loop and then used the vector to calculate values... my god).

Is it good to have a big ass vector in this case though? Right now the thing kind of loops, so every sequence of 10 is not stored in a vector. I'd like to store all the sequences in one vector if that's necessary, but that means to use push_back in a new vector to store the values of the first vector every time... or something?!

The cin for the numbers looks like this from a tip I found, is it possible to store this value in a vector?
https://imgur.com/Y1yb7xo

3 years ago
Permalink

Comment has been collapsed.

Isn't n there a vector?

Also generally best to always post all your code, so we can fully understand what you are doing.

3 years ago
Permalink

Comment has been collapsed.

Yes it is. :)
The problem I'm having now is that only the first number of the sequence gets stored...

https://pastebin.com/viniPeKs
You can probably ignore the lucky numbers part, I've got those fixed. All that's left is to add 1000 to the score if all numbers [1, 99] are there.

3 years ago
Permalink

Comment has been collapsed.

Tbh i dont really understand the question. Do you want to check if the user inputted all numbers from 1-99 in the input? Or just want to check if the user inputted number no more and no less than that number?

3 years ago
Permalink

Comment has been collapsed.

First of all, sorry about my english.

To solve the problem using only loops, without arrays or vectors, i would code a new function called "check" who recives an array as parameter that function iterate over integers between 1 and 99, in each loop check if the number is in the array, if that funcion pass all iterations then meet the condition.
in pseudocode it can be

function check(array) {
for (int i = 1; i <= 99; i += 1) {
if (!array.contains(i)) {
return false
}
}
return true
}

3 years ago
Permalink

Comment has been collapsed.

This is a very rudimentary code. I didn't make anything with fancy library functions like find() and arrays. Purely loops and a single vector. I also haven't made anything that makes it possible to input 10 numbers at once, so you'll have to figure that one yourself. Also check for exceptions like 102 and -2 in the input and make an error if you want to.

So here's the thing, why not check the vector array one by one. It's not optimized but you only need 99 intergers to check, so it shouldn't take too much CPU. The idea is, if your input isn't found in the vector array, it's ignored. Only unique intergers got inserted into it. Once you stop saying yes to the repetition, it checks if your vector is 99 in size. If it is, that means you get 1000 points!

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector<int> vect;
    int inputnum;
    int vectorsize;
    string newline;
    bool userinput = true;
    while(userinput)
    {
        bool numexist = false;
        cout << "Enter your numbers: ";
        cin >> inputnum;
        if(inputnum) //check if inputnum is valid
        {
            for(int i = 0; i<vect.size(); i++)
            {
                if(vect[i] == inputnum)
                {
                    numexist = true;
                }
            }
            if(!numexist)
            {
                vect.push_back(inputnum);
                vectorsize++;
            }
        }

        cout << "One more time? ";
        cin >> newline;
        if(newline == "N" || newline == "n")
        {
            userinput = false;
        }
    }
    int winamount = 0;
    if(vect.size() > 98)
    {
        winamount = 1000;
    }
    cout << "Winning amount: " << winamount<< endl;
}

Hope this code I lazily put together in 10 minutes help.

3 years ago*
Permalink

Comment has been collapsed.

What about, making a boolean to check the final sentence, something like

Make a loop to check number by numer if X value its found, for example

I will try to write code, but i'm very rusty, i hope you can get the idea, i will add notes of what i'm trying to do

if (j=0; j>101; j++){ //a loop to advance in the postition of the vector, self adding a value to advance in position

       if ( i=0; i>101;i++){                                         //a loop to check every number from 0-100

                   if (i=vector[j]) {                                   //this if checks if the new number it's on the vector, with it should, at least one time

                                countBoolean = 1                 //just a flag like i was told, a int variable to check if something it's ok

                    }

          i++

        }

j++
}

if (countBoolean <=2){ //if the sentence found the same number more than 1 time, the score it's 0, you can add
another variable to make sure that are only 100 numbers in the vector
score=0

}
else {

score = 1000 //but if everything it's ok it should give 1000 points
}
:

again, sorry for my crappy english and really poor code, hope it helps

3 years ago
Permalink

Comment has been collapsed.

Sign in through Steam to add a comment.