i have no understanding of how to write an array or use one, please help!

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions i have no understanding of how to write an array or use one, please help!
# 1  
Old 10-16-2010
Java i have no understanding of how to write an array or use one, please help!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script that can check your permission (read/write/execution) on the input filename as the argument.


2. Relevant commands, code, scripts, algorithms:
has to be made using arrays and the declare -a command


3. The attempts at a solution (include all code and scripts):
i have no understanding i can do it using arguements but not with an array


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of michigan, michigan, chang, cse 132

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 10-16-2010
1. declare -a [arrayvariablename] is optional, but good practice
So:
Code:
declare -a myarr
# we now have an empty array called myarr

2. to assign a value to an array element (one of the members of the array:
Code:
myarr[13]=4

This now means that the fourteenth (remember we chose to start counting from 0?)
contains 4.

3. So we want to enter ten numbers right? We need a loop to count from 0 .. 9
Code:
for i in 1 2 3 4 5 6 7 8 9
do
  # put your code here
done

i becomes 0 then 1,2,3..... as the loop goes on.

Code:
for i in 0 1 2 3 4 5 6 7 8 9
do
    echo -n 'Enter a number: '
    read myarr[$i]   # read lets you type in a number then assign it to a variable
done

You now have 10 array element, each of which has a number in it. Let's find the biggest one.
Code:
echo ${myarr[4]}  # NOTE the ${} part -when you want to get the value in an array element

So let us stomp thru the array and assign something to another variable, call it max
Code:
max=-2147483647  # I picked a negative number because your prof will do that to test 
# your code
# poor profs love to to go beyond what they tell you.... they think it somehow teaches (IMO)  

for i in 0 1 2 3 4 5 6 7 8 9
do
   [  $max -lt ${myarr[i] ] && max=${myarr[i]}
done

display the result:
Code:
echo "maximum value is $max"

Have fun....

PS:
Code:
[ $max -lt ${myarr[i] ] && max=${myarr}

is shell shorthand for if [ ] then do something endif.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies

2. Shell Programming and Scripting

Write filenames into an array variable

Hello there, I am a newbie to unix and i have been trying to code a shell script for bash shell which reads the files in a folder using ls and writes them into an array variable. But, i am running into several errors while running the script. The script is given below: #!/bin/bash... (16 Replies)
Discussion started by: amrutha0303
16 Replies

3. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

i have no understanding of how to write an array or use one, please help!

im in a basic unix class and our professor speaks broken engliash so i can never understand what exactly we are doing in class and i have no prior experience with unix. we were given an assignment to make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script... (1 Reply)
Discussion started by: wendyshephard
1 Replies

6. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies

7. Shell Programming and Scripting

Understanding array in array

Hello, I have an array in which output of a query is stored. These values can be accessed by, foreach my $val (@result){ print ("\nValue : @{$val} from result");} Can anybody please explain me, how to delete any particular element in array @result ? (4 Replies)
Discussion started by: gentleDean
4 Replies

8. Programming

Understanding read/write and kernel interaction

Ok, so I'm trying to finalize my understanding of read/write and kernel interaction. read(): You have a library function that has as it's first parameter (what the open file to read from is), second parameter( a pointer to a buffer (is this the location of a buffer in the user area or the... (7 Replies)
Discussion started by: Krothos
7 Replies

9. UNIX for Dummies Questions & Answers

Understanding read/write and kernel interaction

Ok, so I'm trying to finalize my understanding of read/write and kernel interaction. read(): You have a library function that has as it's first parameter (what the open file to read from is), second parameter( a pointer to a buffer (is this the location of a buffer in the user area or the... (1 Reply)
Discussion started by: Krothos
1 Replies
Login or Register to Ask a Question