Create a program with an array containing 10 random ages from 18 and 64

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)

how do i do this in c++ i dont understand

Other urls found in this thread:

hackage.haskell.org/package/base-4.9.0.0/docs/Control-Monad.html#v:unless
twitter.com/NSFWRedditGif

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

Too, too kind.

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

how the fuck are you taking a C++ course and don't know how to do this.

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

so they're teaching you c++ as your first language?!?!

some of us learned visual basic.net before, but pre much yeah

it's propably just C with streams

Im one of those faggots that only knows HTML + CSS and Java and I could do this

Java or Javascript?

Java

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.

i honestly forgot how to do many things in vb.net, so i'd be better off doing c++ for this : /

thats a hard af problem dude, not even lying

doing this in C++ isn't any more complicated dude

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.

i know, thats why i said it'd translate across quite closely.

>Yeah C++ aint a beginners language

t.pajeet

Wut? You think it is a beginners language?

I remember harry potter looking bad, but I didn't remember it being THIS bad

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.

I learned C++ as my first language

C++ is the language I started with. There's nothing wrong with a beginner using it.

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.

Well sure, you *can*.

You can also learn to drive a car in a formula one, its just not optimal from a beginners perspective.

desu senpai, starting with a less forgiving language helps make you a better programmer.
But that's just like, my opinion man.

Fuck off! It was my first programming language and learned it on my 2nd year highschool.

Seriously kill yourself for spreading misinformation

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.

>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.

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?

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

>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.

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++".

>He thinks that C++ is the formula 1 of programming languages
>He doesn't even program in Ada
>laughingwhores.png

Learning up subset of a language is in fact learning a language you dense fuck.

>Learning
>Learning to expertise
>to expertise
Not the same thing, gentle user.

I've been been driving my dad's Civic for months so therefore, I'm able to drive a tank in a race.

20 minutes is a bit long for this particular problem, isn't it?

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

coderbyte is good too

>falling for doing this guy's homework

I gave him steps to solve it for a reason

>pre much

The beer cans on my floor say you're a faggot.

#!/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)

>didnt get into pointers and stuff
>learnt c++

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

#include
#include
#include

using namespace std;

#define LEN_ARRAY 10
#define MIN_AGE 18
#define MAX_AGE 65

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

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

#!/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]

>i = [j+x if j+x < 64 else r.randint(18,64) for j in i]

fragile as fuck

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.

if (65 >= ages[i]) ages[i] = rand() % (64-18)+18;
>if (65 >= ages[i])

Thanks Sup Forums

import System.Random
import Data.List

main = mloop => (putStrLn . concat . intersperse "," $ map show xs)
y > getLine :: IO Int
ns = return . randomRs (18,64)

>tfw nobody replies to my god-tier answer code
why even live

1. no real point using >> inside a do block, just use a newline m8

2. having the \n in front of the lines you're printing instead of after them is incredibly tacky

3. your ‘check’ is written really weirdly. rather just use a simple mapM instead of this weird length/==/filter/10- garbage

4. your exit condition is strictly wrong. it should exit when y == 0, not when xs == ns

5. calling newStdGen multiple times is somewhat unnecessary

6. if C then return () else X =: unless C X

7. x >>= return . f = fmap f x

overall pretty firsttimehaskell/10

I did your homework for you OP.

#include
#include

#include
#include
#include
#include

using namespace std;

int main() {
const int MIN_AGE = 18;
const int MAX_AGE = 64;
const size_t NUM_EMPLOYEES = 10;

function getRandomAgeInRange = [] {
return rand() % (MAX_AGE - MIN_AGE + 1) + MIN_AGE;
};

std::array ages;
srand(static_cast(time(NULL)));
for (int &a : ages) {
a = getRandomAgeInRange();
}

for (;;) {
cout > years;
if (years MAX_AGE) {
age = getRandomAgeInRange();
}
cout

now do it with a bash one-liner

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.

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?

Any tips on getting rid of OO from my thinking? Like doing a Haskell course or something?

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.

again. Let me know if you want your C/C++ code critiqued.

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

you want people to reply to your polmeme

>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.

what pol meme

>xs == ns when y == 0,
but not the other way around

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?

>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

#include
#include

#define MIN_AGE 18
#define MAX_AGE 64
#define EMPL_SIZE 10

int rand_age(void)
{
int n = rand() % MAX_AGE;
if (n < MIN_AGE) return rand_age();
}

int fix_input(char c)
{
if (c < 48 || c > 57) return 0;
else return (c - 48);
}

int main(void)
{
srand(time(0));
int empl_ages[EMPL_SIZE]={0};
int i;
for (i=0; i

>3
let check x | x >= 65 = get new random age
| otherwise = return x
...
ns 6
hackage.haskell.org/package/base-4.9.0.0/docs/Control-Monad.html#v:unless

I see, I overlooked that.
Still, it's very unlikely

>mfw I still have that game and played it in the last month

you should probably use atoi instead so users can input >9 years

Are you mentally-challenged?

He should probably use strtol instead of atoi because atoi has no error checking.

OP Here, thank you everybody. Really appreciate this :)

I was literally gonna post If you don't know how pointers work, you sure as shit don't know C++. It's not elitist, it's fucking basic.