Create a program with an array containing 10 random ages from 18 and 64.
Your program will perform a continuous loop asking the user how many years have passed. The program will add that many years to the ages of the employees. After adding that number of years, every age in the array that is 65 or over is replaced with a new age from 18 to 64. The program then displays the ages of each employee in the company. When the user enters a value of 0 for the number of years that have passed, the program ends. (You will need to use a do loop to solve this)
Make an array the size of 10. Then make a for loop that uses a custom rand function that makes a number between 18 and 64 that puts the number into array[i]. Then make a do while loop, and inside it make two for loops. One that cin's an int, and adds it to array[i] (check each in that is cin'd, and if it's 0 then return 0. It should also check to check the final sum of each, and if it's over 64, it should do the rand function again to replace the age). The other will cout each element in the array
Dylan Anderson
Too, too kind.
Brody Barnes
If it doesn't get in the way of your schedule, I'd recommend trying and getting good at C++ and other areas of hackerrank.com. It helps get you into the programming mindset, and everything becomes easy
Brandon Moore
how the fuck are you taking a C++ course and don't know how to do this.
Brody Myers
teacher has been away for a while and our new teacher sucks ass so its kind of a learn on your own thing for now
Juan Bell
so they're teaching you c++ as your first language?!?!
Hunter James
some of us learned visual basic.net before, but pre much yeah
Anthony Rodriguez
it's propably just C with streams
David Cox
Im one of those faggots that only knows HTML + CSS and Java and I could do this
Joseph Williams
Java or Javascript?
Dominic Diaz
Java
Noah Barnes
Yeah C++ aint a beginners language. But if you have vb.net then just code the answer to the problem in vb and translate it to c++ line by line. It wont (obv.) be exactly the same, but it will be close.
Bentley Perry
i honestly forgot how to do many things in vb.net, so i'd be better off doing c++ for this : /
Dominic Baker
thats a hard af problem dude, not even lying
Juan Bennett
doing this in C++ isn't any more complicated dude
Joshua Morris
ok, well this dude, helped you out...
>54986752
Take each line/thing and google the answer...
in c++ how do i "Make an array the size of 10."
in c++ how do i "make a for loop"
in c++ how do i "use a rand function"
do that with each thing s/he mentions.
Nathan Sanchez
i know, thats why i said it'd translate across quite closely.
Asher Myers
>Yeah C++ aint a beginners language
t.pajeet
Henry Johnson
Wut? You think it is a beginners language?
John Carter
I remember harry potter looking bad, but I didn't remember it being THIS bad
Brayden Sullivan
I think Ansi C is a good start, then move on to c++, then more on to java.
Starting from java produces some bad habits and im tired of seeing that shit.
Its especially bad because people get stuck in OO design which is not the most optimum paradigm in alot of cases.
Grayson Thomas
I learned C++ as my first language
Liam Diaz
C++ is the language I started with. There's nothing wrong with a beginner using it.
Parker Morales
The question just walked you through the whole program. The solution is sitting right there in your face, sentence by sentence.
Put the functions in, you ass. How much hand holding do you need? Otherwise, consider switching to Native American Womens Studies.
Julian Rogers
Well sure, you *can*.
You can also learn to drive a car in a formula one, its just not optimal from a beginners perspective.
Ryan Cruz
desu senpai, starting with a less forgiving language helps make you a better programmer. But that's just like, my opinion man.
Grayson Hill
Fuck off! It was my first programming language and learned it on my 2nd year highschool.
Seriously kill yourself for spreading misinformation
Charles Richardson
I'd agree that "a less forgiving language helps make you a better programmer".
I just dont think its a good idea to start there.
Elijah Smith
>learned it on my 2nd year highschool
No you didnt, there is no way you learned C++ in one year. Its a massive language with all sorts of hidden complexities.
Jaxson Allen
Is this like an elementary programming class? I guess I could help walk you through some of the logic if you need it, but I'm just a 4th year CS undergrad and I finish that in about 20min. Are you having trouble with the language, or just elements of the given problem?
Matthew Phillips
Sure we didnt get into pointers and stuff you elitist autist are raving about, but that doesnt mean it cannot be taught as the student's first ever programming language.
Kill yourself
Hudson Davis
>we didnt get into pointers and stuff you elitist autist are raving about Pointers are _not_ "elitist" or anything like that at all. There is very little you can do with C/C++ without pointers, including even the absolutely _basic_ concept of dynamic memory allocation, at least without a gigantic fustercluck of references and new function templates.
Luke Davis
You didn't learn C++ to expertise in high school. What you did was learn a tiny subset of C++. I cannot call that "learning C++".
Jack Walker
>He thinks that C++ is the formula 1 of programming languages >He doesn't even program in Ada >laughingwhores.png
Adrian Campbell
Learning up subset of a language is in fact learning a language you dense fuck.
Connor Young
>Learning >Learning to expertise >to expertise Not the same thing, gentle user.
Jack Anderson
I've been been driving my dad's Civic for months so therefore, I'm able to drive a tank in a race.
Michael Rivera
20 minutes is a bit long for this particular problem, isn't it?
Levi Sullivan
No it isn't. Every programming language has variables, conditionals, loops, and print statements. Most have classes. Knowing how to do these things in a given language does not mean you are learning the language; it just means that you're learning how to program.
Learning a language means learning the interesting parts of it, which no high school class will ever touch.
By the way, here's your answer: #include #include #include #include
void doAnonsHomework () { int yearsPast; std::string userResponse;
srand(time(0));
int ages[10]; for (int i = 0; i < 10; i++) { ages[i] = rand() % (64-18) + 18; }
do { std::getline(std::cin, userResponse); yearsPast = std::stoi(userResponse);
for (int i = 0; i < 10; i++) { ages[i] += yearsPast; if (65 >= ages[i]) ages[i] = rand() % (64-18)+18; std::cout
Justin Gutierrez
coderbyte is good too
Christopher Phillips
>falling for doing this guy's homework
I gave him steps to solve it for a reason
Joseph Garcia
>pre much
Carson Kelly
The beer cans on my floor say you're a faggot.
Grayson Harris
#!/usr/bin/env python
import random
people = [ random.randint(18, 64) for age in range(10)] print(people)
add = None while (add != 0): add = int(input("Years to add: ")) for e, age in enumerate(people): people[e] = age + add if (people[e] > 64): people[e] = random.randint(18, 64) print(people)
Evan Lopez
>didnt get into pointers and stuff >learnt c++
Easton Adams
list l = (i) 1:10 >> i = rand() while input != 0 var o = input foreach i in l i += o if i > 65 i = rand() end while foreach i in l print i
int new_employee(void) { int n = 0; if( (n = rand() % MAX_AGE) < MIN_AGE) n += MIN_AGE; return n; }
void employee_array(int* arr) { int n, i; n = i = 0; srand(time(NULL) ); while(i++ < LEN_ARRAY) arr[i] = new_employee(); }
void update_array(int years, int* arr) { int n, i; n = i = 0; while(i++ < LEN_ARRAY) { arr[i] += years; if(arr[i] > MAX_AGE) arr[i] = new_employee(); } }
void print_employees(int years, int* arr) { int i = 0; cout
Leo Turner
After adding, every age over 64 is replaced with what? Do they mean just wrap back around? In that case just do (age - 18 + years) mod 46 + 18
Brandon Evans
#!/usr/bin/env python
import random as r
i = [r.randint(18,64) for i in range(10)] while True: x = int(input("Year to add, zero to exit:" )) if x == 0: exit() i = [j+x if j+x < 64 else r.randint(18,64) for j in i]
Blake Butler
>i = [j+x if j+x < 64 else r.randint(18,64) for j in i]
fragile as fuck
Anthony Fisher
But it works, dunnit 8^)
I'm just trying to lower my code quality to that of Pajeet. If you can code like them, you can take their jobs.
std::array ages; srand(static_cast(time(NULL))); for (int &a : ages) { a = getRandomAgeInRange(); }
for (;;) { cout > years; if (years MAX_AGE) { age = getRandomAgeInRange(); } cout
Hudson Hughes
now do it with a bash one-liner
Michael Campbell
I'm Many issues here. 1) Use the C++ headers instead of C headers. They are in std:: so you don't pollute the namespace. You want ctime instead of time.h and cstdlib instead of stdlib.h . Typically the C and C++ headers are separated bya newline.
2) You don't need the void in func(void). C++ enforces a func() not taking any arguments unlike regular C.
3) Use an autoformatter, your formatting is very inconsistent.
4) Use std::array instead of C style arrays. They know their own size so you don't need to keep track of the size.
5) Use stricter warnings. I see implicit conversions.
6) Organize your code better. srand() doesn't have anything to do with initializing the array.
7) Name your functions better. Sometimes you have employee_x and others y_employee, decide on one.
8) I see you doing: int years; // unrelated line years = 0;
Do "int years = 0" instead.
There's more if you get through all that.
Easton Barnes
9) There's no error handling. What do you think would happen if i typed 'asdf' instead of a number?
10) new_employee(): rethink this function. There's a much more elegant way to do this. Hint: don't be off by 1. Model your distribution on paper.
11) int n, i; n = i = 0; srand(time(NULL) ); while(i++ < LEN_ARRAY) arr[i] = new_employee();
You don't even use 'n'! Turn on stricter compiler warnings. Your compiler should be catching these kinds of issues. These two lines in combination don't do what you think they do: int i = 0; while(i++ < LEN_ARRAY) arr[i] = new_employee();
Think about it. After the while loop does the first check, what is the value of 'i' during the first iteration?
Carson Young
Any tips on getting rid of OO from my thinking? Like doing a Haskell course or something?
Lucas Perez
12) You should be using valgrind on your programs. It would have helped you catch some of your errors (especially memory access related ones).
13) In C++ generally constants are preferred over #defines because of the additional typesafety.
14) I see more places where you declared 'n' but then didn't use it. I also see you misusing the postincrement in the loop condition again.
Nathan Watson
again. Let me know if you want your C/C++ code critiqued.
Adam Miller
fucc I used to play that game when I was like 10 years old. I also beat it like 3 times the last time at 100% oh shit nigga, why are you doing this to me? make it stop
Wyatt Anderson
you want people to reply to your polmeme
Gavin Richardson
>1 saves me a line and bundles together two similar IOs >2
how many years have passed?: input here
vs how many years have passed?: input here
I preferr the former, and doign it that way yields it >3 not really, its straightforward, 10 - length f (10 because length cs == 10) will return how many was filtered, and then it will get that many ages and append it to the filtered list I don't know what you mean by using mapM 4> xs == ns when y == 0, but I can understand why testing y is simpler and more efficient. it's what I came up with at the time. I was probably thinking in explicit terms of just the list, e.g. if its different or not different >5 mkStdgen c will always give me the same list. mkStdGen is only good when I need randomness once. unless you mean doing a lot of dropping, nope, much easier to get a new list
>6 ???????
>7 you got me there, I forgot about that
>comment sorry you feel that way, I gave 7/10 effort.
Nolan Mitchell
what pol meme
Nolan Young
>xs == ns when y == 0, but not the other way around
Parker Taylor
incorrect y must equal zero when xs == ns if they're equal, remember xs == ns when no change, and no change means adding zero (the mzero of the monoid)
also, can you explain what u ment in 6 and 3?
Charles Murphy
>incorrect ns = [60,60,60,60,60,60] y = 10
they all get discarded and replaced by random variables. All of the random variables happen to be 60. therefore xs = [60,60,60,60,60,60] == ns even though y != 0