Dalldorf Design Home Portfolio Pricing Services Resume About Tutorials
 
                                  Arrays

 Source Files  no source files



In this tutorial you will learn how to create an integer and character array. This code was written with Visual Studio 2003 and used the win32 Console application.

First we will create a integer array and show two simple methods of filling the elements. The array will consist of 5 elements and the elements will be filled with numbers 1-5

// Character and Integer Arrays
int intArray[5];

// you can fill each element like this
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5;

// Or use a for loop
for (int i=0;i<5;i++)
{
intArray[i] = i + 1;
}


Now create a character array and fill it with "Hello World"

char charArray[255] = "Hello World";

Now lets put it all together into a running program

#include <iostream>
using namespace std;

 

int main()
{
// Character and Integer Arrays
int intArray[5];
char charArray[255] = "Hello World";

// you can fill each element like this
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5;

// Or use a for loop
for (int i=0;i<5;i++)
{
intArray[i] = i + 1;
}

// Display the elements of the intArray with a for loop then the charArray
for (i=0;i<5;i++)
{
cout << intArray[i] << endl;
}
cout << charArray << endl;



system("pause");
return 0;
 }

The output of this program is
1
2
3
4
5
Hello World


Thats it, I recommend that beginners to experiment with the arrays and try diffrent methods to fill and extract the elements in arrays. Have fun.

Copyright © 2007 Dalldorf Design. All Rights Reserved