Anybody know C++ out there

More
19 years 11 months ago #18309 by They_HunGer
i have a prob on doing some coding...this is the prob: input 10 integer nos. and the program will identify the 'mean'..... i cant get it right[:(][:(][:(]:(!:(!:(!

Ex: num1:10
num2:2
num3:10
num4:10
num5:10
num6:7
num7:8
num8:9
num9:7
num10:3

mean:10

~C.C. Rocks~

Please Log in or Create an account to join the conversation.

More
19 years 11 months ago #12006 by andrewas
Replied by andrewas on topic Anybody know C++ out there
homework? Best not just hand out the answer then.

However - use a for loop. Use cin and cout to get user input. (Or, you could be fancy and use a GUI app. Or read them from the command line. But, prompting for input is the normal way to do it). Keep a running total of the integers so far, after the loop terminates devide by 10 and output. And dont keep the result of that division in an int of any description.

If you still cant get it, what specific problem are you having?

IN the example you give, it looks more like your looking for the mode than the mean, which is a bit trickier to write but still nothing major.

Please Log in or Create an account to join the conversation.

More
19 years 11 months ago #12010 by EricMan64
In your example, 10 is not the mean of those ten numbers. A mean is a group of numbers added together and divided by the number of numbers. With the ten numbers in your example, the mean is 7.6.

If you really mean mean (hehe), andrewas has good advice. I would just like to add that when you divide by ten, you need to make sure not to do integer division. When you divide an integer by an integer, C++ will cut off any decimals to give you an answer as an integer. You want a mean of 7.6, not 7.

Please Log in or Create an account to join the conversation.

More
19 years 11 months ago #12030 by They_HunGer
sorry for the long reply... well i was wrong, its not the mean, but the 'MODE'[8)]hehe.

here's the ex. of the code i was working on:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int A,ARR[10],MODE,list;
main()
{
clrscr();
gotoxy(1,1);cprintf("Cscio2/DA\n\n");
printf("PlEASE INPUT A VALUE\n");
for(A=0;A<10;A++)
{
printf("Num%d is\n",A+1);
scanf("%d",&ARR[A]);
}
printf("\nMODE is equal to");
getch();
}

ps: still have errors..[xx(][xx(]

Its not about you dying for your country... its about making your enemies die for theirs.

~C.C. Rocks~

Please Log in or Create an account to join the conversation.

More
19 years 11 months ago #12032 by andrewas
Replied by andrewas on topic Anybody know C++ out there
1) Your using a deprecated header notation. It still works, but if your learning c++ you want to use the new headers, which have the same name as the old ones but lacking the .h extension. That way, when you come to write serious programs, you will find that the headers are up to date, whereas the .h ones wont be.

2) Indentation. It helps to make loops, functions, if statements etc etc much easier to read. While on the subject of code prettification, use something like the Context editor rather than notepad, DOS edit or whatever. Syntax highlighting and parenthesis matching really helps.

3) Convention is for a generic loop variable to be names i, not A. Trivial, but it makes code easier to understand.

4) Its not finished. It gives no errors (at least not on Borland 5.5) , but AFAICS it dosent even attempt to calculate the mode.

Probably the easiest way to calculate the mode would be to sort the array, then look for the longest chain of duplicates, paying attention to the case where there is no longer chain (ex 1,2,3,4,5,6,7,8,9,10), or where there are two equally long chains (1,1,1,2,3,4,5,6,6,6) or where theres only one really long chain, or any other unusual input condition you can think of.

Please Log in or Create an account to join the conversation.

More
19 years 11 months ago #12040 by EricMan64
Once again, andrewas has beat me with some good advice.

My additional comments:
1) I think you need to go tell your teacher to teach you C++, not C. You should be using cout and cin for your console io. printf() and scanf() are the old way of doing things.
2) I would suggest using local variables instead of global ones. If you declare A, ARR, MODE, and list inside the main() function instead of outside, they will only be accessible within main(). This is good because you only want your data to be accessible where you need it.

Please Log in or Create an account to join the conversation.