RE: C programming Arrays

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 9 Oct 2007 18:16:30 -0700


You made a small error in your description you said 

int array[4];

Is five variables it is really only 4 0-3.  You should have done

int array[5];

Ken 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Marlon Brandão
de Sousa
Sent: Tuesday, October 09, 2007 3:45 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: C programming Arrays

Hello,
Arrays may be seen as sequential variables, in the seense that they are
stored in contiguous spaces in memory. They allow an indexed access and they
have a specific syntax to do that.
See this and everything should be clearer now.
I have to declare 5 integers. I can do:
int a;
int b;
int c;
int d;
int e;

I can also have
int a, b, c, d, e;

All right, this is very good. Another option is this:
int intArray[4];

This declares an array called intArray (it could be called anything you
want) which contains space for five integers. To be clearer, assuming an
integer is 32 bits, this instruction will alocate 160 bits of memory.

Arrays have the [] syntax which will let you access any one of the array
elements. Still in the given sample, the first element would be accessed as
intArray[0], the second element would be accessed as intArray[1] ... and the
fifth element would be accessed using intArray[4] ...

And why arrays are usefull?

Because of many reazons, but let me show you just a good example of this. I
have five integers and I have to verify if all of them are less than 100.
The code without array use follows:
#include <stdio.h>
int main()
{
// initializing the integers
int a=1, b=102, c=153, d=23, e=43;
if(a < 100 && b < 100 && c < 100 && d <100 && e= 100) { printf("All integers
are less than one hundred\n"; } else { printf("at least one of the integers
are greater or equals to one hundred.\n"; } return 0; }

This code works greatly, but if you need to check one more integer you would
have to change most part of the code, declaring another variable of type
integer type and changing the if statement to include this new integer in
the check list.
Also, if you would put this in a function you would have to create a
function with 5 parameters, and pass all the integers to it. If you wanted
to put one more integer in the check list, you would have to alter the
function declaration, the function definition and still declare one more
integer in the main function and alter the function call line to pass yet
this new created integer to it.
If you didn't understand all this don't worry with it now, I just wanted to
show you that this solution, although useable, will generate trouble if the
code need to be changed. Now, see the same thing achieved with use of array.

#include <stdio.h>
int main()
{
// instead of creating integers a through e we weill create an array of
integers which contains five integers int intArray[4]; // Now, we will
initialize all its members with some value intArray = {1, 3, 133, 153, 4};
/*
Now, intArray[0], the first integer in the array, have the value 1.
IntArray[1], the second integer in the array, have the value 3.
And so on.
Now, we will check each one of the array integers against 100 and we'll see
if it is less than 100. First, we'll create an integer variable which will
act as our index. The index is the number between the [ and ] and will tell
the compiler what element of the array we're interested in.
*/
int count; // index
/*
As our array starts at element 0 and finish at element 4 (thus five
integers) we will make a for loop which will start with count = 0 and
increment it untill it reaches the value 4, then exit */ for(count = 0;
count <= 4; count++) { // now, we check if the element count of the array is
less than 100 if(intArray[count] < 100) { printf("%d %s", intArray[count], "
is less than 100!\n"); } } return 1; }

Ok, if we wanted to add one more integer to the check list, it would be a
mather of changing int intArray[4] to int intArray[5] and include another
value in the initialization list. After this thee second clause of the for
statement should be changed to i <= 5 and it would work.
If we wanted to use a function to do this operation, it wouldn't have to be
changed, cinse it would receive an array only, not five or six integers.
Arrays may be dangerouse and are a little more complex than this in C, but
this is how it work. If you understand this let us know so we can say you
the trickier parts. If you still can't understand it please ask again.
Marlon

2007/10/9, John Miller <n1umj@xxxxxxxxxxx>:
> Hi,
> Is there anyone that can give a good, actually reasonably 
> understandable explanation or example of an array in C programming? In 
> my class, the book's examples are horrible at best and confusing and 
> the teacher should be fired his is so bad. That or I should drop the 
> class which I was actually about to do but I looked and there is no 
> other major completely online that holds my interest so I guess I have 
> to stick it out for another torturous 5 weeks or what ever it is. I'm
getting some of it, but not enough.
> Thanks,
> John Miller N1UMJ
> AIM and yahoo messenger: N1UMJ Skype: n1umjjohn home page:
> http://home.comcast.net/~n1umj/wsb/html/view.cgi-home.html-.html
> myspace: http://www.myspace.com/n1umj
>
> __________
> View the list's information and change your settings at 
> //www.freelists.org/list/programmingblind
>
>


--
When you say "I wrote a program that crashed Windows," people just stare at
you blankly and say "Hey, I got those with the system, for free."
Linus Torvalds
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: