pls tell me the output for this

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr ();
int a[20];
int n,m,age;
int c1=0, c2=0, c3=0;
cout<<”enter how many employes:”;
cin>>n;
for(int i=1; i<=1;i++)
{
cout<<”enter age”<<i<<”:”;
cin>>a[i];
}
m=a[20];
for(int i=1; i<+n; i++)
{
If(a[i]>=26 && a[i]<=35)
c1++;
else if (a[i]>=36 && a[i]<=45)
c2++;
else if (a[i]>=46 && a[i]>=55)
c3++;
}
cout<<”number of employees between age 26-35 are:”<<c1<<endl;
cout<<”number of employees between age 36-45 are:”<<c2<<endl;
cout<<”number of employees between age 46-55 are:”<<c3<<endl;
getch();
}
Last edited on
There is no output. Void (capitalised) isn't a valid keyword in C++ and void main() is not a valid declaration for main().

But still, i have to ask, why not just compile and run the code yourself - use an online compiler if necessary.
http://ideone.com/
@ OP: You're also forgetting to include the std namespace. That first for loop is almost comical, don't take this the wrong way but are you messing with us?
Last edited on
listen you people
i got this programme from an expert and if u saying this is wrong then what is right
Reporting me was a bit excessive don't you think? I appreciate a good prank and I thought you were setting us up for one that's all. It actually had potential to be pretty good.

The fact of the matter is that your code is mixing and matching C with C++, now we've all done this at some point or another but it was usually for a better reason then "this is the only way I know how to do it". You're hard coding the size of your arrays that you are not populating correctly, you're omitting a namespace that you are relying on, you're clearing the screen with a non-standard function... The list goes on and on like this. Now I can help you and even over look the temper tantrum but you don't provide any context beyond the title of your post.

listen you people
i got this programme from an expert

No you didn't. They might have told you that they are an expert but if what you say is true then the evidence here contradicts that statement.
Well, I admit my initial post was 50% unhelpful.

(though also 50% helpful - the suggestion to try compiling and run it still stands).

But the real issue here is that some teaching is done using a really old Borland Turbo compiler. Students do get a bit stuck when trying to learn since all the help given here will be based on ISO standard C++.
i apologize for my behavior and i should not have reported you.
but the real matter is that i have got a project and we have been learning the old version of c++ and maybe things work differently , so it would be kind of you to help me correct the program , and ya i did get it from an expert.
the question was:
WAP to accept the age of n employees and count the number of persons in the following age
group.
(i) 26-35 (ii) 36-45 (iii) 46-55.
thanks,
apology
Lines 8, 10, 25-27: wrong quotes. You want " not ”

As pointed out earlier, line 10 makes no sense. The for loop will execute exactly once, then terminate. You want:
for(int i=0; i<n; i++)

Line 15: This is an out of bounds reference. There is no element 20 in your array. Arrays are indexed from 0. Legal references are a[0] to a[19].
Not sure what this line is even trying to do. What is m? A more meaningful name would be helpful.

Line 16: Your for loop is faulty.
for(int i=1; i<+n; i++)
Indexes start from 0, not 1. What is +n supposed to mean?

Line 18: if should not be capitalized.

Line 22: This if statement is faulty. Check the operator in the second conditional term.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
I did intend on helping you out OP, AbstractionAnon just beat me to it. I will touch on one point that I mention that he omitted though, your array titled 'a' should be dynamically sized in this context. Now the version of C++ you are using is before my time so I don't know if you have the 'new' operator but I do know that you have access to the malloc() and calloc() functions from the 'stdlib.h' header. I like the later for this use case since it will ensure that you don't have to change any of the logic in our if else block other then what AA has previously indicated. So the first thing to do is the change the allocation of 'a' on Line 5 to a pointer and initialize that to NULL:

 
int* a = NULL;


SEMI-PRO TIP:Normally when you are changing bits of code like this it's helpful to "comment out" the old line instead of deleting it, this is accomplished with a set of double forward slashes, "//" no quotes, at the beginning of the old line. Think of this as a poor mans version control.

The next step will be to allocate your array AFTER prompting the user for the size of his data set. Here is how:

 
a = calloc(n, (sizeof(int) * n);


And then when you are done with the data it's a good habit to call free():

 
free(a);


FINAL NOTE: I would like to re-emphasize AbstractionAnon's point on using meaningful variable names. It's my opinion that comments in code should feel redundant (but not omitted) and that variable and function names should be your primary method of explaining what it is that your program is doing.
thanks
AbstractionAnon wrote:
Indexes start from 0, not 1.
I like to think of it as an offset from the first element or address of the beginning of the allocated block.
Topic archived. No new replies allowed.