Arrays and Cmd Line Args
Home Up ComputingConcepts Definitions Characters & Strings C_Formatted IO C_Structs_Enums_Files FunctionsAndMenus Arrays and Cmd Line Args Arrays & Structures Structs, Enums, Ptr Variables Ptrs. to External Files Miscellaneous Random Files

 

 

C Programming

For  definitions of some important terms related to arrays, click here:

ARRAYS AND COMMAND LINE ARGUMENTS

ARRAYS

Simple variables allocate memory for the storage of a single value of a specified data type (int, char, float, etc.)
Arrays allows for the storage of multiple values of a single data type.
Arrays use a single variable name to access those multiple values.

 
Memory Allocation

 

                        int  Num;       One memory location          
 
Num              

                                

    
Nums[5];        Contiguous memory allocation   

Nums[0]

Nums[1]

Nums[2]

Nums[3]

Nums[4]

 
Represent a set of values or a list of related objects that is given one name.
Each item in the array is called an ELEMENT and each element has the same data type.
Character Arrays hold non-numeric or string data.
Numeric Arrays hold integer or floating point data.

 

Numeric/Character Array   

char Letters[5];    int Nums[5];    

            
 

 

Creating Arrays

 
Numeric Arrays:  Numeric data is stored in Integer or Floating-point arrays.
int  quantity[7];
float mileage[5];
float area[12], AmountDue[36];
int   grades[32],
credits[50],
hours[25];

Character Arrays:  Character or String data is stored in character arrays.
char Letters[26];
char day[7][10];  -- an array of strings; 7 strings of 9 characters
char JobTitle[30][20];  and a Null   ( /0 )
char StudentNames[50][15];

Subscripts

A subscript is an integer value that is used to reference a specific element in an array.
An array is called a subscripted variable.
Each element is referenced by its subscript.
The first element is numbered zero (0).
A subscript may not be negative or exceed the size of the array minus one.

Examples of Subscripted Arrays

char day[7][10];

day[0] = “Sunday”;
day[1] = “Monday”;
  day[2] = “Tuesday”;
      day[3] = “Wednesday”;

   day[6] = “Saturday”;

Examples

   printf("%s", day[3]);

 
   scanf("%s",day[3]); 

 
strcpy(day[3],"noday");

 

Initializing Data


Data may be assigned to an array when it is declared.


      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


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

 
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

 
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

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

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

 

Two-dimension arrays are useful for representing data in a table format.  The array is defined in rows and columns.  
Rows represent a set of values across a table.  
Columns represent a set of values down a table.

 
Table Example


 

Creating a 2d Array


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

DOS
              C:\>edit

                C:\>edit  myfile

Providing input to the program when executing.
 
UNIX
                  %vi

                        %vi myfile
 
Environments that support “C” provide a method of passing arguments (parameters) to a program when it begins execution.
When main is called it can be called with 2 arguments.
argc - argument count (or the number of command line arguments the program was invoked with).
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. 
 
You create a “C” program (myprog) that accepts arguments that represent text file names.
When you execute your program you include the names of the files.
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
  
 

Back Home Up Next