Click to See Complete Forum and Search --> : algoritm problem


arash
11-30-2003, 03:09 AM
Hi friends , is anybody out there to help me with this code,
thanks



<!--
Design an algorithm that will prompt for and receive 10 integers from an operator
at a terminal, and count the number of integers whose value is less than the average
value of the integers. Your program is to display the average integer value and the
count of integers less than the average.

//-->


<!--
process_integer_array
set integer_total to zero
set integer-count to zero
prompt integer
count
Do index = 1 to 10
integer_total = integer_total + numbers (index)
Enddo
integer_average=integer_total/10
Do index = 1 to 10
If numbers (index)<integer_average Then
add 1 to integer_count
End if
Endo do

Display integer average
Display integer_cou
//-->


<html>
<head>
<title>algoritm</title>
</head>


<script type="text/JavaScript" language="JavaScript">


var arInteger=new Array()
var intTotal=0
var intCount=0
var avg=0


var numberOfIntegers=0
index =0


while (index < 10)
{
arInteger[index]=parseInt(prompt("Enter the integer"))
numberOfIntegers= (numberOfIntegers + arInteger[index])
avg=(numberOfIntegers/10)
index++
}

var myArray = new Array()
myArray = arInteger

document.write("array elements less than average : "+"<br>")

for(var i=0; myArray[i]; i++)
{

if (myArray[i] < avg)

{
document.write(myArray[i]+"<br>")
}
else
{
}
if ("0"==myArray[i])
{
document.write("0"+"<br>")
}

continue;


}

document.write("numbers you have entered "+arInteger+"<br>")
document.write(" average "+avg+"<br>")
</script>
<body>

</body>
</html>
:confused: :confused:

Pittimann
11-30-2003, 05:04 AM
Hi!

Actually you just missed three points in your script:

1. counting the values smaller than the average and display their number,

2. 0, -1, -2 etc. are integers two

3. if the user enters non-numeric stuff your counter will continue counting (the user can even enter **** all the ten times - what will be the result? - ****!).

Maybe something like that helps you:

<html>
<head>
<title>algoritm</title>
</head>
<script type="text/JavaScript" language="JavaScript">
var arInteger=new Array()
var arIntegerString=new Array()
var avg=0
var countSmaller =0
var numberOfIntegers=0
index =0
while (index < 10) {
arIntegerString[index]=prompt("Enter the integer")
if (!isNaN(parseInt(arIntegerString[index]))){
arInteger[index]=eval(arIntegerString[index])
numberOfIntegers= (numberOfIntegers + arInteger[index])
avg=(numberOfIntegers/10)
index++
}
}
for(var i=0; i<10; i++) {
if (arInteger[i] < avg) {
countSmaller++;
}
}
document.write(" average "+avg+"<br>")
document.write(" integers smaller than average: "+countSmaller+"<br>")
</script>
<body>
</body>
</html>

Cheers - Pit

arash
12-01-2003, 12:55 AM
thank you .
:p

Pittimann
12-02-2003, 05:32 AM
Hi!

I stumbled over another thread today dealing with integers.
While reflecting how to handle this it came to my mind that what I proposed to you is not correct.

With the code I posted for you, the user still could enter numbers with decimals.

Here is the correction:

<html>
<head>
<title>algoritm</title>
</head>
<script type="text/JavaScript" language="JavaScript">
var arInteger=new Array()
var arIntegerString=new Array()
var avg=0
var countSmaller =0
var numberOfIntegers=0
index =0
while (index < 10) {
arIntegerString[index]=prompt("Enter the integer")
//beginn modification
var checkDot=0;
for (var i=0; i<arIntegerString[index].length;i++){
if (arIntegerString[index].substring(i,i+1)==".")
checkDot++;
}
//end modification
if (!isNaN(parseInt(arIntegerString[index]))&&checkDot==0){//condition altered
arInteger[index]=eval(arIntegerString[index])
numberOfIntegers= (numberOfIntegers + arInteger[index])
avg=(numberOfIntegers/10)
index++
}
}
for(var i=0; i<10; i++) {
if (arInteger[i] < avg) {
countSmaller++;
}
}
document.write(" average "+avg+"<br>")
document.write(" integers smaller than average: "+countSmaller+"<br>")
</script>
<body>
</body>
</html>

Sorry for the confusion!!!

Cheers - Pit

arash
12-02-2003, 12:21 PM
thank you so much:p

Pittimann
12-02-2003, 12:25 PM
Glad the correction reached you! Could as well have been that after having been satisfied with the replies before, you unsubscribed to the thread - it would have been me to have put a bunch of sh.. on you.

Cheers - Pit ;)