c & c++ - arrays

Contact
PC-compilers
last-news
PIC-microcontroller
AVR-microcontroller
Supports
weakly newsletter
c++
=> Visual Studio
=> C++ Language
=> arrays
=> Functions
=> Windows Programming
=> Using AppWizard
=> MFC Model
Counter
DS1821Thermostat Programmer
pic-asm-foundations
free links downloads
free downloads
mikroc library
PWM software
Home



 

Arrays

Like Java C++ allows you arrays  of anything.  All elements must be of the same type.  The starting subscript is zero.  The starting subscript cannot be changed to 1  ( remember VB).  The name of the array also represents the first element in the array.  It is also a constant value during the program execution.  It cannot be  an lvalue.  The array name by itself cannot appear on the left hand side.

lvalues:  represent storage locations that can have their contents altered by the program - they usually appear on the left side of an assignment.

    int  iarray[3];        // array of 3 integers
  char carray[12];   // a string array - note strings in C++ are character arrays

It is recommended practice to set the size of an array using a variable

    #define X_VERTMAX = 7

    double xvert[X_VERTMAX];

Arrays are intialized to zero by default.  Arrays can also be explicitly declared and initialized:

    double xvert[X_VERTMAX]  =  { 0.0, 0.3, 0.5, 1.0};
        //  Note you do not have to initialize all values
        // some compilers will complain if you provide more
        // values then the declared length of array

    char string1[]  = "Bezier Curve";
            // the size of the array is the number of elements
            // initialized with

If you are using an empty size in the declaration you must initialize the array as it will be taken as a zero array.  You will not be able to assign any elements to it later.  It will kill neighboring information.

    int size_xvert ;
  size_vert =  (int) sizeof(xvert);
            // returns number of bytes used by the array xvert

Multidimensional Arrays

Like Java multidimensional arrays are defined by additional squate brackets

    double coeff [4][4];

Multidimensional arrays are stored as a single  dimensional array within the computer.  Multidimensional arrays can be ininitialized in the form defined.

 
 Lets write a snippet to check out program control

In this program we will
 

    read a line of input from screen
    parse them into words
    use program control procedures
    use string manipulation function

ExpressYourself.cpp

/*
//  a snippet to look at program control and
// string/character input and output
// hacked from Pappas/Murray
*/

#include <iostream.h>
#include <string.h>  // contains functions for string handling

#define MAX_LENGTH 80 // good idea to capitalize constants
#define EXIT 0   // define constants

int main()
{
 char cLine[MAX_LENGTH + 1]; // this is a string
 char cWords[MAX_LENGTH + 1]; // another string
 char sWords[20][MAX_LENGTH + 1]; // a string array - note 2d char array
 // string variables need one more space for string terminator \0

 int iChrs = 0;
 int iWords = 1;
 int iW = 0;
 int ivar;

 do
 { cout << "Enter a sentence expressing your feeling about B Curve \n\n";
  cin.getline(cLine, MAX_LENGTH);  // reads a string from console
 } while (cLine[0]  == '\0');  // the prompt will keep appearing
   // until a string is entered
   // example of a do-while
 
 

 while (cLine[iChrs] != '\0')  // just while
 { if (cLine[iChrs] != ' ') // this program assumes words are
         // seperated by one blank character
   {   cWords[iW] = cLine[iChrs];
           iW++;
   }  else
   {  strcpy(sWords[iWords - 1], cWords);  // built in string copy function
      for (ivar = 0; ivar < iW ;   ivar++)
      cWords[ivar] = ' ';
      iWords++;
      iW = 0;
   }
   iChrs++;
 
 } strcpy(sWords[iWords-1],cWords); // copy the last word

 cout << "\nYou wrote: " << cLine << "\n" << endl;
 
 cout << "which included " << iChrs  << " characters\n\n";
 cout << "which included " << iWords << " words\n";

 for (iW = 0; iW < iWords ; iW++)
  cout << " The words are:  " << sWords[iW] << endl;
 cout << "\n\n";

 return EXIT;
}

Today, there have been 24 visitors (45 hits) on this page!
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free