C Programming
For definitions of some important terms
related to arrays, click here:
ARRAYS
AND COMMAND LINE ARGUMENTS
ARRAYS
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Simple
variables allocate memory for the storage of a single value of a specified
data type (int, char, float, etc.) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Arrays
allows for the storage of multiple values of a single data type. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Arrays
use a single variable name to access those multiple values. |
int Num; One memory location
Nums[5]; Contiguous memory allocation
Nums[0]
|
Nums[1]
|
Nums[2]
|
Nums[3]
|
Nums[4]
|
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Represent
a set of values or a list of related objects that is given one name. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Each item
in the array is called an ELEMENT and each element has the same data type. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Character
Arrays hold non-numeric or string data. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Numeric
Arrays hold integer or floating point data. |
char day[7][10] = {“Sunday”, “Monday”, “Tuesday”,"Wednesday”,
“Thursday”, “Friday”, “Saturday”};
float nums[5] = {1.1, 2.3, 94.5, 781.99, 74.3};
Interactive
Input
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Data may
be assigned to an array by prompting the user to enter it at the keyboard. |
/* load day array */
for (i=1; i < 7 ; i++) {
printf(“\nEnter the day: “);
scanf(“%s”, day[i]);
}
File Input
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Data may
be read from a file and loaded into an array.
|
/*LoadDayArray/
i=0;
while(!feof(fileptr)) {
fscanf(fileptr, "%s", day[ i ]);
i = i + 1; /* or i ++ */
|
Printing
an Array
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
We can
verify the contents of an array by printing a copy of the elements |
/* Printing Day Array */
for(i = 0; i < 7; i++) {
printf(“\n %s”, day[i]);
}
Computations
using Arrays
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Very often
the values stored within an array need to be included in calculations. |
total = 0;
for (i = 0; i < 50; i++) {
total = total + grade[i];
}
average = total / 50;
Parallel
Arrays
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Often
your programs will require that a relationship exist between various types
of data. Parallel arrays provide a means of associating lists of
data together.
|
char driver[10][10];
float miles[5];
driver
(0) Joan
(1) Bill
(2) Mary
(3) Harvey
(4) Dorothy |
miles
(0) 78.2
(1) 100.8
(2) 999.0
(3) 1.0
(4) 0.0 |
Two-Dimensional
Arrays
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Two-dimension
arrays are useful for representing data in a table format.
The array is defined in rows
and columns.
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Rows represent
a set of values across a table.
|
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Columns
represent a set of values down a table.
|
|
Table
Example
Creating
a 2d Array
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Examples
of a 2d array declaration are: |
int quantity[7][3];
float mileage[3][5];
Example
Command
Line Arguments
Providing
input arguments to the main() function
Executing
your program
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
DOS |
C:\>edit
C:\>edit myfile
Providing input to the program when executing.
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
UNIX |
%vi
%vi myfile
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Environments
that support “C” provide a method of passing arguments (parameters) to
a program when it begins execution. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
When main
is called it can be called with 2 arguments. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
argc -
argument count (or the number of command line arguments the program was
invoked with). |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
argv
- argument vector ( a pointer to an array of strings that contains one
argument per string). |
argc/argv
Examples
main(int argc, char *argv[ ]) Note: the size of the array
argv depends on the value of argc
argv
is an array of strings.
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
You
create a “C” program (myprog) that accepts arguments that represent text
file names. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
When
you execute your program you include the names of the files. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Example: |
myprog text1.dat text2.dat
The value of argc would be 3:
arg[0] is myprog
arg[1] is text1.dat
arg[2] is text2.dat
![](_themes/sumi-painting-smallfont/sumhorsa.gif)
|