+ 3
Can anyone help me with this one. I am not getting the question nor the logic
An array int emp[20] contains the number of employees joining an organisation every year for a period of 20 years. Write a program to find the total number of years when no new employees joined the organisation. P.S ; I don't want the code. I only want to know the right way to approach this question. Thanks in advance guys!
7 Antworten
+ 2
each position in the array gives the number of employee joining on the corresponding year. let's say emp[0] is the number for the first year and emp[19] the number for the last year.
All you have to do is a loop checking if a position contains a zero value.
And increment a number of year counter for each zero found.
+ 2
@RR2001 there are several methods possible to know if a position is a valid zero :
- the array could be initialized with invalid (eg. negative) numbers
- or you could have a separate variable memorizing the number of valid years....
now, if there was 'holes'in the period, or could not use negative numbers:
- you could have a separate array of 20 booleans showing which position is valid
- or even, having a 20 bits numbers whose binary representation give the same info
+ 2
Now, I'm not sure I understand your doubt fully:in this particular case, if the array is initialized with zeros and a position emp[n-1]is incremented when an employee join on year n, if any other position is null it just means no employee joined on that year?
+ 2
@RR2001 yes i think so.
+ 1
@ifl I understood what you are saying. but how do you know if the original dummy value of the array is overwritten ?
for example
the way I work with arrays is to first initialise the array:
for (int i=0;i <n;i++)
{
a [i] = 0;
}
then, ask for the value from user and overwrite the dummy's value (0 in this case)
and based on your answer you are trying to tell me to go through the array again and if I encounter an element 0, I should count it and then display count. but how do I accept the value from user when I don't know on what basis I should accept the value.
let's say an employee joined newly. so I should say in the output " if you joined enter 1" and store it in the array and if any other number is written I should consider it as the condition no new employee has joined the company or something?
I hope you are able to understand my doubt sir /mam
+ 1
@ifl no I mean on what basis you fill the arrays? so that by filling the arrays, in the end if there are any unfilled array elements (in this case 0) just count and return it.
+ 1
@ifl Thanks for taking your time in answering my questions. After reading your explanation , it's clear. Thanks again