Generating 5x5 matrix with random numbers in each box - random

I need to generate in PHP a matrix(5x5) in which, every box must contain different random number between 1 and 1000. I have tried like that but it generates me only 1 random number in every single box:
$random = rand(1, 1000);
echo '<table border="1" style="width:200px">';
for ($i=0; $i < 5; $i++) {
echo "<tr>";
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo $random;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";

put random inside your loop:
....
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo rand(1, 1000);
echo "</td>";

You are only assigning your variable a random value once.
You can do 1 of the following:
You need to either make a separate function that you call at
echo "<td>";
echo CALL_FUNCTION_HERE
echo "</td>";
or
Create an array and fill it with random numbers.
echo "<td>";
echo $myArray[j];
echo "</td>";
or
Move your declaration:
echo "<td>";
$random = rand(1, 1000);
echo $random;
echo "</td>";

Related

Problem in shell code to find happy prime number

This is my code to find happy prime number, I don't know why it is giving error.
#!/bin/bash
happy(){
rem = $0
sum = $0
while [ $result -gt 0 ];
do
rem = $((result%10))
p = $((rem*rem))
sum = $((sum + p))
result = $((result/10))
done
return $sum
}
echo "Enter a number"
read num
for (( i=1; i<101; i++ ))
do
result=$i
while [ $result -ne 0 && $result -ne 4 ];
do
happy $result
done
if [ $?==1 ]
then echo "happy number"
else
echo "not a happy number"
fi
done
I see lots of syntax errors and some logical errors in your script.
This should be a fixed version (works at least for 13 and 4 :))
Errors I've found:
rem = $0 and similar: the spaces around assignments are not allowed in bash,
rem = $0: you assign a value that is never used,
rem = $0 and sum = $0 the first argument passed to the function is $1 not $0,
the input number is assigned to num but never used,
the exit condition from the for and while loops is broken,
...
#!/bin/bash
happy()
{
result=$1
sum=0
while [ $result -gt 0 ]; do
rem=$(( result % 10 ))
p=$(( rem * rem ))
sum=$(( sum + $p ))
result=$(( result / 10 ))
done
echo "$sum"
}
echo "Enter a number"
read num
result=$num
for (( i=1; i<101; i++ )) do
result=$(happy $result)
if [ $result == 1 ]; then
echo "$num is a happy number"
exit
fi
done
echo "$num is not a happy number"

BubbleSort Bash is not sorting

My bubble sort code in bash script does not swap nor sort. Help me out on what I am doing wrong.
echo "Enter numbers between 4 and 12"
read n
#declare -a numArray #declare array of numbers
if [ $n -le 12 -a $n -ge 4 ]
then
#take input from user
echo "Enter numbers of array"
for (( i = 0; i < $n; i++ ))
do
read numArray[$i]
done
#sorting logic
for (( i = 0; i < $n; i++ ))
do
for (( j = 0; j < $n-1; j++ ))
do
# swapping logic
#read numArray[$j]
if [[ ${numArray[j]} -gt ${numArray[$((j+1))]} ]]
then
#echo "in swapping code"
temp=${numArray[j]}
echo $temp
numArray[$j]=${numArray[$((j+1))]}
numArray[$((j+1))]=$temp
fi
done
done
# print sorted data
echo -e "\nSorted Numbers "
echo "${numArray[*]}"
else
echo "Please enter only values between 4 and 12"
fi
Result:
Enter numbers between 4 and 12
5
Enter numbers of array
51428
51428
51428
51428
51428
Sorted Numbers
51428

How do I take the symbol from user for formatting a pattern

#!/bin/bash
sum=0
echo
echo " Enter the number of rows you want : "
read rows
echo
for row in $(seq 1 $rows)
do
for col in $(seq 1 $row)
do
sum=$(( $sum + 1 ))
echo -n "$sum "
done
echo
done
It prints the output I want,
1
23
456
But I don't want to start my code from 1, I want user to enter the number. So I tried to modify, but it still does not give the desired output.
#!/bin/bash
sum=0
echo
echo " Enter the number of rows you want : "
read rows
echo " Enter the number you want to start with : "
read num
echo
for row in $(seq $num $rows)
do
for col in $(seq $num $row)
do
sum=$(( $sum + 1 ))
echo -n "$sum "
done
echo
done
Even if I enter the num.
This gives me no output at all. It just does not show anything.
Can anyone tell where am I going wrong? I cant figure it out.

Shell script to extract line by line data and provide in variable to use further

File 1 ->
hostname1
hostname2
hostname3
hostname4
.
.
.
.
I want to write a bash script to extract these hostnames and save them in a variable.
Something like below but this does not work
count=3
i=1
j=1
count=`expr $count + 1`
while [ $i -lt $count ]
do
echo The counter is $i
$j=`sed -n "$i,$i p;$i q" file1.txt`
echo $i
i=`expr $i + 1`
j=`expr $j + 1`
echo $j << this should return hostname1 then hostname2
done
Try this :
i=1
while read line ;
do
Var$i=$line
i= ` expr $i + 1 `
done < inputFile
This should create var1, var2 ... and assigns each line to a variable
Else an array can also be used
i = 0
while read line ;
do
arr[$i] = $line
i = `expr $i + 1`
done < inputFile
The expr syntax is not picking up the back ticks on my answer, please check when you use it
i=1
while read line ;
do
i=$line
echo $i
i=i+1
done < file.txt
Above code has resolved my issue . Thanks

Bash script can't figure out how to concatenating white spaces

I want to center a word in X spaces so I wrote this function:
function center {
str=$1
M=$2
N=${#str}
if (( N >= M )); then
echo $str
exit
fi
P=$((M-N))
HP=$((P/2))
left=""
right=""
for i in [1..HP]; do :
left=" $left"
right=" $right"
done
if (( (P-2*HP) == 1 )); then
right=" "$right""
fi
echo "HP is $HP"
echo "Right is |"${right}"|"
echo "Left is |$left|"
echo "str is |$str|"
res=$right$str$left
echo "$res"
}
Problem is not matter what I do can't get right or left to hold on to more than one whitespace. I have tried the suggestions on other answers but I can't seem to make them work. Please help.
Try this (after some variable quoting and some other fixes):
function center {
str=$1
M=$2
N=${#str}
if (( N >= M )); then
echo "$str"
exit
fi
(( P=M-N ))
(( HP=P/2 ))
left=$(printf '%*s' "$HP" "")
right=$left
(( (P-2*HP) == 1 )) && right=" "$right""
echo "HP is $HP"
echo "Right is |${right}|"
echo "Left is |$left|"
echo "str is |$str|"
res="$right$str$left"
echo "$res"
}
The for i in [1..HP] can not work.
A printf '%*s' "$HP" "" is a more idiomatic way to get $HP spaces.
Is better to build one variable $left and copy it to $right than to build the two.

Resources