Hi, could someone help me with arrays?

This is a discussion on Hi, could someone help me with arrays? within the C Programming forums, part of the General Programming Boards category; Hi. I am goodn. i hope to contribute helpfully to your board later, but right now I am the one ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Hi, could someone help me with arrays?

    Hi. I am goodn. i hope to contribute helpfully to your board later, but right now I am the one in need of help. In my c class we are starting to go over Arrays. I really have no grasp of them, other than knowing

    int[5] = {4, 6, 7, 2, -1}

    is an array of size 5 with values 4, 6, 7, 2, and negative 1. I don't understand their application into programming at all. The way I tend to learn things is by seeing an example, but my prof. doesn't supply any.

    Anyway, an assignment I have is to make a tone of functions in a header file using arrays such as isum, dsum, iaverage, daverage, imin, imax, dmin, dmax, etc.

    I think if I could get a push in the right direction I would easily be able to do this. If someone could be so kind as to show me how one would do

    Code:
    isum
    This function will take an integer array of numbers and the array size as its parameters. 
    It will return the sum of all the numbers in the array as an integer.
    then I think I could do the rest no problem. I've already done very similar functions without arrays, so if someone could give me a start, I'm sure I could do the rest with arrays. I'll post my progress back here. Thanks.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,669
    Think of an array as a container for objects of the same type. That's it. A word such as "Word" is simply an array of 4 characters. However, arrays of characters are always ended with the '\0' (always considered a single character), and so the above array would actually be 5 cells long.

    Arrays of integers are useful too since you can easily move through them with loops.

    An example of an averaging function would be one that adds up the total of each cell and then divides that by the number of cells. Hence,

    int box[5] = {5,9,10,62,77}; //<---An array of 5 integers

    int i= 0;
    int total = 0;
    int avg = 0;

    for( i=0; i< 5; i++)
    {
    total = total + box[i];
    }

    avg = total / 5;



    Since arrays always start at "0"(not "1"), the adding stops after cell #4 is reached(so not cell# 5); If you accidentally access too many cells, your program will probably crash.


    Anyway, that's one example.



    ITSA
    Socket Library!

  3. #3
    Unregistered
    Guest
    > Think of an array as a container for objects of the same type.
    > That's it. A word such as "Word" is simply an array of 4
    > characters. However, arrays of characters are always ended
    > with the '\0' (always considered a single character), and so the
    > above array would actually be 5 cells long.

    This is not always correct. Yes, you think of strings as being 'null
    terminated', but it is not a requirement. It is if you are using most
    of the 'str*' functions, but it isn't a requirement:
    Code:
    char array[4] { 'w', 'o', 'r', 'd' };   // This is legal.
    
    strncpy( array, 4, "cows" ); // This is legal.
    
    puts( array ); // This is bad in this case.
    Generally speaking, yes, you want all of your 'strings' to be null terminated.

    A 'string' is an array of characters that is terminated by 1 character which has the value of zero. Thus:

    char array[5] = "word";

    This will null terminate it for you becuase you're using a "string". By enclosing the characters in double quotation marks at the point of assignment, it adds the null terminator. In my example, since I assign each character seperately, it will not.

    Quzah.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    a hint on isum is that your function declaration would look like this:

    int isum( int nNum[], int nQty );

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    I knew all about what they meant, and about the 0 thing, and I also knew how to declare functions, but now I understand that about how about the box[i] thing. I see how I can increase the function.

    so I could use

    Code:
    int isum (int iarr[], int num)
    {
       int i, total;
    
       for (i=0, total=0; i < num; i++) 
             {
               total += iarr[i];
              }
       return total;
    }
    As my isum function, would that be correct?

  6. #6
    Unregistered
    Guest
    Right. That'd work. You can think of each element of an array like a drawer in a dresser. Or a row on a shelf. Something like that. Thus, when you want to access an individual element in the whole array, you're looking at 1 drawer in the dresser. Or one row on the bookshelf. So if you want to see what's on the third row, you do:

    shelfContents = myShelf[2]; //since you start at 0 and work up

    Yeah, you've got the hang of it.

    Quzah.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Code:
    int isum (int iarr[], int num)
    {
       int i, sum;
    
       for (i=0, sum=0; i < num; i++) total += iarr[i];
       return sum;
    }
    
    int dsum (double darr[], double num,)
    {
       double d, sum;
    
       for (d=0, sum=0; d < num; d++) total += darr[d];
       return sum;
    }
    
    int iaverage (int iarr[], int num)
    {
       int i, sum, avg;
    
       for (i=0, sum=0, avg=0; i < num; i++) total += iarr[i];
    	avg = total/i;
       return avg;
    }
    
    int daverage (double darr[], double num)
    {
       double d, sum, avg;
    
       for (d=0, sum=0, avg=0; d < num; d++) total += darr[d];
    	avg = total/d;
       return total;
    }
    Ok, here are my other functions so far. I'm having a little problem with my imax, though.

    Code:
    int imax (int darr[], int num)
    {
    	int i, max;
    	
    	for (max = -36667, i = 0; i < num; i++)
    	{
    	if (/*what would go here?*/ > max)
    		max = /*what would go there?*/;
    	}
    	return max;
    }
    What would I put where my comments are?

    Also, since I can't compile a header file, does anyone see any horrible errors with my other functions?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    [code]int imax (int iarr[], int num)
    {
    int i, max;

    for (max = -36667, i = 0; i < num; i++)
    {
    if (iarr[i] > max)
    max = iarr[i];
    }
    return max;
    }

    duh, I don't know why I missed that. One thing I just noticed however is that I never defined num anywhere. I don't see how that could have happened, but how do I know num is the right number for i(or d) to be approaching?

  9. #9
    Unregistered
    Guest
    One thing to consider about all of your function, is numeric overflow. Consider the following:

    int array[3] = { 1000000000, 1000000000, 10000000000 };

    Now try and add all those together in an 'int'.

    Additionally, you could assign initial values for your variables when you declare them, and you wouldn't have to do it in your for loop. Either way works:
    Code:
    nt isum (int iarr[], int num)
    {
       int i=0, sum=0;
    
       for (; i < num; i++) total += iarr[i];
       // or
       // while( i < num ) total += iarr[i++]; 
       return sum;
    }
    For a max function, first off, your value is likely wrong. If you are compiling on a 32 bit compiler, then an integer's max is ~2 billion.

    Anyway, this is the check you would use in your case, assuming 16bit integers:
    Code:
    int imax (int darr[], int num)
    {
                    // I am assuming that this function returns the
                    // integer in the array that is the highest?
    	int i, max;
    	
    	for (max = -36667, i = 0; i < num; i++)
    	{
                       if( num[i] > max ) max = num[i];
    	}
    	return max;
                    // if you want to return the index of 'max', then
                    // you'll need an additional variable to store the
                    // value of 'i' at the time you find 'max'. Set this
                    // when you set max as I've shown above.
                    // { max = num[i]; maxIndex ] i; }
                    // then later, 'return maxIndex;'
    }
    Quzah.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    I also just noticed I was using int for my double functions, and I was using double variables in ones that should be integers.

    for instance darray[d] where d is a double. Where in the world will a location in your array be 9.5? Heh, I changed those to integers(keeping the arrays as doubles, of course).

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Originally posted by Unregistered

    Code:
    int imax (int darr[], int num)
    {
                    // I am assuming that this function returns the
                    // integer in the array that is the highest?
    	int i, max;
    	
    	for (max = -36667, i = 0; i < num; i++)
    	{
                       if( num[i] > max ) max = num[i];
    	}
    	return max;
                    // if you want to return the index of 'max', then
                    // you'll need an additional variable to store the
                    // value of 'i' at the time you find 'max'. Set this
                    // when you set max as I've shown above.
                    // { max = num[i]; maxIndex ] i; }
                    // then later, 'return maxIndex;'
    }
    Quzah.
    I'm not quite sure what you mean, but are you forgetting that the initialazation of the for loop only happens once or something? I don't understand why I need another variable.

    And where did num[i] come from? wouldn't you use iarr[i], the array you declared in the top?

  12. #12
    Unregistered
    Guest
    No, I am aware that initialization happens once. What I'm saying is this:

    Do you want to return the highest value, or the array index (ie: where the highest value is located at in the array)?

    If you want to return the highest value, you can just return it like the function states. If you want to return the _INDEX_ of the value, then you need to keep tabs on that by using another value:
    Code:
    int imax (int darr[], int num)
    {
    	int i, max, index=-1;
    	
    	for (max = -36667, i = 0; i < num; i++)
    	{
                       if( num[i] > max ) { max = num[i]; index = i; }
    	}
    
                    // this would return the index of the highest value
                    return i;
    
                    // this would return the actual highest number
                    return max;
    }
    Thus:

    int array[4] = { 200, 14, 703, -25 };
    int highestValue;
    int highestValueIndex;


    highestValueIndex = imax( array, 4 );
    printf("The highest value is located at array[%d] and is %d",
    highestValueIndex, array[highestValueIndex );

    // assume we used the second return statement instead
    highestValue = imax( array, 4 );
    printf("The highest value is located at array[%d] and is %d",
    //Ooops! We don't know where it's at!
    , array[highestValueIndex );


    See what I was getting at?

    Quzah.

  13. #13
    Unregistered
    Guest
    edit: that first return should be "return index", sorry. I really should find my password so I can sign in...

    Quzah.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    OH. I see now. Yeah, I definitely want to return the highest value, not the index. Thanks for the tip though, I have a function in my next assignment which DOES need the highest index.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by goodn
    [code]int imax (int iarr[], int num)
    {
    int i, max;

    for (max = -36667, i = 0; i < num; i++)
    {
    if (iarr[i] > max)
    max = iarr[i];
    }
    return max;
    }

    duh, I don't know why I missed that. One thing I just noticed however is that I never defined num anywhere. I don't see how that could have happened, but how do I know num is the right number for i(or d) to be approaching?
    Yes, you declared num -- you declare it as a parameter to the function in the function definition.

    So:

    int imax(int iarr[], int num)

    declares two variables -- one an array of ints named iarr, one an int named num. Whatever you pass in as your second parameter becomes num within this function.

    So, if you called it like this:

    imax(array,5);

    num would be 5. If you called it like this:

    imax(myArray,x); //x is an int

    then num would have whatever value x had.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21